【问题标题】:TTS Google api Xcode 4.xTTS 谷歌 api Xcode 4.x
【发布时间】:2012-02-17 08:26:30
【问题描述】:

我对 google TTS api 有疑问。 好吧,我认为使用 api 一切都可以,因为如果我使用桌面的路径并在模拟器中运行应用程序,它会使用 mp3 创建文件。 问题是我无法重现它。 我没有错误,但我什么也听不到。我使用 AVaudioPlayer。 这是我使用的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];

NSString *text = @"You are one chromosome away from being a potato.";
NSString *urlString = [NSString stringWithFormat:@"http://www.translate.google.com/translate_tts?tl=en&q=%@",text];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" forHTTPHeaderField:@"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
                                     returningResponse:&response
                                                 error:&error];
[data writeToFile:path atomically:YES];

AVAudioPlayer  *player;
NSError        *err;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{    
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
              [NSURL fileURLWithPath:path] error:&err];
    player.volume = 0.4f;
    [player prepareToPlay];
    [player setNumberOfLoops:0];
    [player play];    
}

我从另一个 stackoverflow 问题中得到这段代码:Text-to-speech on iPhone

【问题讨论】:

    标签: xcode mp3 avaudioplayer text-to-speech


    【解决方案1】:

    我认为问题在于AVAudioPlayer 不是类变量。尝试推广它,然后再试一次。祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2015-10-16
      • 2012-04-03
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      相关资源
      最近更新 更多