2016-01-10 27 views
0

我遇到了一個問題,即獲取我的iPhone上錄製的音頻文件以播放我的手錶揚聲器。會話:didReceiveFile:不播放音頻文件(iPhone要觀看)

這是我從iPhone發送到Watch的代碼。

- (void)sendLiveAudioRecordingToWatch 
{ 
    NSError *error; 
    NSFileManager *files = [NSFileManager defaultManager]; 
    NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
    NSString *record = [docs stringByAppendingPathComponent: @"LiveAudio.m4a"]; 

    NSURL *URL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"group.myCompany.myApp"]; 
    NSURL *outputURL = [URL URLByAppendingPathComponent: @"PhoneToWatch.mp4"]; 

    NSString *path = [NSString stringWithFormat: @"%@", outputURL]; 
    NSLog(@"%@", [NSURL fileURLWithPath: path]); 

    [files removeItemAtPath: [outputURL path] error: &error]; 
    BOOL fileExists = [files isReadableFileAtPath: record]; 
    if (fileExists) 
    { 
     NSLog(@"Live Audio Recording Saved!"); 
     [files copyItemAtPath: record toPath: [outputURL path] error: &error]; 
     if (!error) 
     { 
      NSLog(@"Temporary Recording Copied To Group"); 
      BOOL deleteRecord = [files removeItemAtPath: record error: &error]; 
      if (deleteRecord) 
      { 
       NSLog(@"Temporary Recording Removed"); 
       if ([WCSession isSupported]) 
       { 
        WCSession *session = [WCSession defaultSession]; 
        NSLog(@"WCSession isSupported"); 
        [session setDelegate: self]; 
        [session activateSession]; 
        if ([session isReachable]) 
        { 
         NSLog(@"File Is Being Transferred"); 
         [[WCSession defaultSession] transferFile: outputURL metadata: nil]; 
        } 
        else 
        { 
         NSLog(@"Watch Not Reachable"); 
        } 
       } 
      } 
      else 
      { 
       NSLog(@"Failed To Delete Temorary File"); 
      } 
     } 
     else 
     { 
      NSLog(@"Error Copying Live Audio To Group"); 
     } 
    } 
    else 
    { 
     NSLog(@"Error Locating Temporary Audio Location"); 
    } 
} 

這是我從iPhone手錶上接收的代碼。

- (void)session: (WCSession *)session didReceiveFile: (WCSessionFile *)file 
{ 
    WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL: [file fileURL]]; 
    WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset: asset]; 
    WKAudioFilePlayer *player = [WKAudioFilePlayer playerWithPlayerItem: playerItem]; 
    [player play]; 
} 

的iOS 9.2 watchOS 2.0

有沒有人遇到過,或者是能夠得到這方面的工作?

+0

WatchKit擴展接收到文件嗎? – ccjensen

+0

我以爲group.myCompany.myApp在WatchKit OS2中不起作用。有不同的協議從主應用程序獲取數據 –

+0

no @SamB,我試圖傳輸文件,而不是消息 –

回答

0

我得到了這個工作。 WKAudioFilePlayer用於通過藍牙設備播放,而不是Apple Watch的揚聲器。在WatchOS 2.0及更高版本中,您可以使用:

[self presentMediaPlayerControllerWithURL:url options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error){}];