在WatchKit OS2中播放音频文件

时间:2016-04-09 22:08:48

标签: path watchkit nsurl watch-os-2 watchconnectivity

从手机发送MP3:

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

此文件的网址:

  

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

这是错误:

  

播放时出错:Error Domain = com.apple.watchkit.errors Code = 4"在此服务器上找不到请求的网址。" UserInfo = {NSUnderlyingError = 0x16d4fa10 {错误域= NSPOSIXErrorDomain代码= 2"没有此类文件或目录"},NSLocalizedDescription =在此服务器上找不到请求的URL。}。

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

1 个答案:

答案 0 :(得分:3)

要启用观看应用播放音频文件,您必须将其移至共享应用组容器。

您需要在WatchKit扩展程序和WatchKit应用程序之间启用共享应用程序组。

如果您没有将文件放在共享容器中,那么预计音频播放将失败。在移动文件之后,您也不应该从回调队列中调出,因为WCSession将在回调返回时删除文件。

以下是tutorial解释如何执行此操作,以及来自Apple的some information有关此主题的内容。

编辑:添加了示例

一旦你为WatchKit App和Extension启用了应用程序组容器,这样的东西就应该有用了:

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