Play audio File in WatchKit OS 2

i have one code like this in my iPhone Part


    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [pathArray objectAtIndex:0];

    NSString *yourSoundPath = [documentsDirectory stringByAppendingPathComponent:@"MyMusic.mp3"];
    NSURL *url = [NSURL fileURLWithPath:yourSoundPath isDirectory:NO];
   [self.session transferFile:url metadata:nil];




i great send to apple watch for playing





-(void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file {
    dispatch_async(dispatch_get_main_queue(), ^{

   

        NSLog(@"URL%@" , file.fileURL.filePathURL);
   
 NSDictionary *options = @{
                                  WKMediaPlayerControllerOptionsAutoplayKey : @YES
                                  };
   
        [self presentMediaPlayerControllerWithURL:file.fileURL.filePathURL options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) {
            if (!didPlayToEnd) {
                NSLog(@"The player did not play all the way to the end. The player only played until time - %.2f.", endTime);
            }
       
            if (error) {
                NSLog(@"There was an error with playback: %@.", error);
            }
        }];
   
   
    });
}


This my NSLog URL :


file:///var/mobile/Containers/Data/PluginKitPlugin/-------/Documents/Inbox/com.apple.watchconnectivity/------/Files/----/MyMusic.mp3



this is my Error :
There was an error with playback: Error Domain=com.apple.watchkit.errors Code=4 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x16d4fa10 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSLocalizedDescription=The requested URL was not found on this server.}.



What can i play this file in Watchkit OS 2

Did you ever get an answer to this? i am trying to do the same thing with the same result. Tried the WKAudioFilePlayer and it also won't play.

When session:didReceiveFile: returns the system deletes the file, so before your dispatch_async you should make a copy of the file and use that copy.


See the important note at the end of:


https://developer.apple.com/reference/watchconnectivity/wcsessiondelegate/1615651-session

For anyone else trying to play an audio or video file with the WKAudioFilePlayer. The API documentation doesn't mention this, but you must save the file to a shared group container. Then play from their. Now the problem is fogiurong out why bluetooth play/pause and other buttons don't work with Apple Watch unless you play from music app ok watch and then play from your own watch app. UGH!!!

I am trying to play audio from last two days but not play in the watch. Below my code is written in WKInterfaceController. I have added "BeeepTestAudio.mp3" file in Watchkit Extention.



let soundPath =  NSBundle.mainBundle().pathForResource("BeepTestAudio", ofType: "mp3")
let soundPathURL =  NSURL(fileURLWithPath: soundPath!)
let audioFile = WKAudioFileAsset(URL: soundPathURL)
let audioItem = WKAudioFilePlayerItem(asset: audioFile)      

var soundPlayer = WKAudioFilePlayer(playerItem: audioItem)
     
if soundPlayer.status == .ReadyToPlay
{
     soundPlayer.play()
}
else
{
     print("Not ready!!")
}



Everything is fine but not play sound in watch.

Play audio File in WatchKit OS 2
 
 
Q