1

電話から 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/----/MyMusic.mp3

これはエラーです:

再生でエラーが発生しました: エラー ドメイン=com.apple.watchkit.errors コード=4 「要求された URL はこのサーバーで見つかりませんでした。」UserInfo={NSUnderlyingError=0x16d4fa10 {エラー ドメイン=NSPOSIXErrorDomain Code=2 "そのようなファイルまたはディレクトリはありません"}, NSLocalizedDescription=要求された URL がこのサーバーで見つかりませんでした。}.

このファイルを watchOS 2 で再生するにはどうすればよいですか?

4

1 に答える 1

3

Watch アプリでオーディオ ファイルを再生できるようにするには、ファイルを共有アプリ グループ コンテナーに移動する必要があります。

WatchKit拡張機能とアプリの間で共有アプリ グループを有効にする必要がありますWatchKit

ファイルを共有コンテナーに入れていない場合、オーディオの再生が失敗することが予想されます。また、コールバックが戻ったときに WCSession がファイルを削除するため、ファイルを移動するまでコールバック キューからディスパッチしないでください。

これを行う方法を説明するチュートリアルと、このトピックに関する Apple からの情報を次に示します。

編集:例を追加

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);
    }
}
于 2016-04-10T15:16:52.240 に答える