2015-10-08 67 views
1

我正在使用WatchKit開發Apple Watch應用程序。我正試圖播放使用Google的文本到語音API接收的音頻文件。以下是我使用相同的代碼:presentMediaPlayerControllerWithURL不適用於google tts API

NSURL *myUrl =[NSURL URLWithString:@"https://translate.google.com/translate_tts?key={my_private_key}&ie=UTF-8&tlen&q=Hello%20testing&client=t"]; 
[self presentMediaPlayerControllerWithURL:myUrl options:nil 
            completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) { 
             if (error){ 
              NSLog(@"%@",error.description); 
             } 
            }]; 

但代碼返回以下錯誤:

Error Domain=com.apple.watchkit.errors Code=4 "Cannot Open" UserInfo={NSLocalizedFailureReason=This media format is not supported., NSUnderlyingError=0x15d506f0 {Error Domain=NSOSStatusErrorDomain Code=-12847 "(null)"}, NSLocalizedDescription=Cannot Open} 

該API返回這是應該在Apple關注被支持的MP3文件OS2。爲什麼我得到這個錯誤?我該如何解決它?我確信音頻可以播放,因爲我看到商店中的一些應用程序使用谷歌TTS並使用WatchKit播放聲音。

回答

1

WatchOS 2 Documentation:

Place media files that you download from the network (or transfer from your iOS app) in a shared group container. A shared group container provides common storage for your Watch app and WatchKit extension. In your extension code, create URLs for any media files inside the container, and use them to configure the media interfaces.

您需要啓用應用組在功能和設置共享組容器。然後用容器的網址來放置下載的語音音頻,並使用presentMediaPlayerControllerWithURL

實例玩:

NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xxxxx.xxx"]; 

             containerURL = [containerURL URLByAppendingPathComponent:[NSString stringWithFormat:@"Caches/tts.mp3"]]; 

             [data writeToURL:containerURL atomically:YES]; 
             //data you received from Google TTS response 

[self presentMediaPlayerControllerWithURL:containerURL 
            options:nil 
           completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) { 
            if (error){           
             //error handling           
            } 
           }];