Search code examples
iostext-to-speechwatchkitapple-watchwatchos-2

presentMediaPlayerControllerWithURL not working with google tts API


I am developing an app for Apple watch using WatchKit. I am trying to play an audio file received using Google's Text-to-Speech API. Following is the code I am using for the same:

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);
                                       }
                                   }];

But the code is returning the following error:

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}

The API is returning an mp3 file which is supposed to be supported in Apple Watch OS2. Why am I getting this error? How can I solve it? I am sure the audio can be played since I have seen a few apps in the store which uses googles TTS and plays sound using WatchKit.


Solution

  • From 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.

    You need to to enable App Groups in capabilities and set up a shared group container. Then use the container url to place the downloaded speech audio and to play using presentMediaPlayerControllerWithURL

    Example:

    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                                           
                                       }
                                   }];