【问题标题】:Play Audio File in WatchKit OS2在 WatchKit OS2 中播放音频文件
【发布时间】:2016-04-12 08:08:01
【问题描述】:

从手机发送 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);  
            }  
        }];  
    });  
} 

这是文件的 URL:

file:///var/mobile/Containers/Data/PluginKitPlugin/-------/Documents/Inbox/com.apple.watchconnectivity/------/Files/----/我的音乐.mp3

这是错误:

播放出错:Error Domain=com.apple.watchkit.errors Code=4“在此服务器上找不到请求的 URL。” UserInfo={NSUnderlyingError=0x16d4fa10 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSLocalizedDescription=在此服务器上找不到请求的 URL。}。

如何在 watchOS 2 中播放这个文件?

【问题讨论】:

    标签: path watchkit nsurl watchos-2 watchconnectivity


    【解决方案1】:

    要使手表应用能够播放音频文件,您必须将其移动到共享应用组容器中。

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

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

    这里有一个 tutorial 解释了如何做到这一点,还有一个来自 Apple 的 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);
        }
    }
    

    【讨论】:

    • 我没有意识到手表上需要共享应用程序组。感谢您提供非常有教育意义的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多