presentMediaPlayerControllerWithURL 不适用于 google tts API
presentMediaPlayerControllerWithURL not working with google tts API
我正在使用 WatchKit
为 Apple Watch 开发应用程序。我正在尝试播放使用 Google 的文本转语音 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 Watch OS2 应该支持的 mp3 文件。为什么会出现此错误?我该如何解决?我确信可以播放音频,因为我在商店中看到一些使用 googles TTS 并使用 WatchKit 播放声音的应用程序。
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.
您需要在功能中启用 App Groups 并设置共享组容器。然后使用容器 url 放置下载的语音音频并使用 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
}
}];
我正在使用 WatchKit
为 Apple Watch 开发应用程序。我正在尝试播放使用 Google 的文本转语音 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 Watch OS2 应该支持的 mp3 文件。为什么会出现此错误?我该如何解决?我确信可以播放音频,因为我在商店中看到一些使用 googles TTS 并使用 WatchKit 播放声音的应用程序。
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.
您需要在功能中启用 App Groups 并设置共享组容器。然后使用容器 url 放置下载的语音音频并使用 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
}
}];