2016-04-09 42 views
1

從手機發送一個MP3:播放音頻文件WatchKit OS2

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

我怎麼一直試圖接收和播放對手錶的文件:

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

此文件的網址:

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

這是錯誤:

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.}.

如何在watchOS 2中播放此文件?

回答

3

要啓用監視應用程序播放音頻文件,您必須將其移至共享應用程序組容器。

您需要在WatchKit擴展程序和WatchKit應用程序之間啓用共享應用程序組。

如果您沒有將該文件放入共享容器中,則預計音頻播放將失敗。您應該也不要從回調隊列中派發,直到您移動文件,因爲WCSession將在回調返回時刪除文件。

這是一個tutorial,解釋瞭如何做到這一點,並從蘋果公司some information這個主題。

編輯:添加例子

一旦你啓動應用程序組容器爲您WatchKit應用和擴展這樣的事情應該工作:

- (void)session:(WCSession * __nonnull)session didReceiveFile:(WCSessionFile * __nonnull)file 
{ 
    NSLog(@"received file %@, metadata: %@", file, file.metadata); 

    NSFileManager *fm = [NSFileManager defaultManager]; 
    NSError *error = nil; 
    NSURL *containerURL = [fm containerURLForSecurityApplicationGroupIdentifier:@"<YOUR APP GROUP HERE>"]; 
    NSString *documentsPath = [containerURL path]; 
    NSString *dateString = [[[NSDate date] description] stringByAppendingString:@"-"]; 
    NSString *fileNameWithDate = [dateString stringByAppendingString:file.fileURL.lastPathComponent]; 
    documentsPath = [documentsPath stringByAppendingPathComponent:fileNameWithDate]; 
    if ([fm moveItemAtPath:[file.fileURL.path stringByExpandingTildeInPath] toPath:documentsPath error:&error]) { 
     if ([fm fileExistsAtPath:documentsPath isDirectory:nil]) { 
      NSLog(@"moved file %@ to %@", file.fileURL.path, documentsPath); 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       NSDictionary *options = @{ WKMediaPlayerControllerOptionsAutoplayKey : @YES }; 
       [self presentMediaPlayerControllerWithURL:[NSURL fileURLWithPath:documentsPath] options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable playAudioError) { 
        if (!didPlayToEnd) { 
         NSLog(@"The player did not play all the way to the end. The player only played until time - %.2f.", endTime); 
        } 

        if (playAudioError) { 
         NSLog(@"There was an error with playback: %@.", playAudioError); 
        } 
       }]; 
      }); 


     } else { 
      NSLog(@"failed to confirm move of file %@ to %@ (%@)", file.fileURL.path, documentsPath, error); 
     } 
    } else { 
     NSLog(@"failed to move file %@ to %@ (%@)", file.fileURL.path, documentsPath, error); 
    } 
} 
+1

我沒有意識到,共享應用程序組在手錶上是必要的。感謝您的教育回答! –