1

我在获取我在 iPhone 上录制的音频文件以从我的 Watch 扬声器中播放时遇到问题。

这是我从 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 在 Watch 上接收的代码。

- (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

有没有人经历过这个,或者能够得到这个工作?

4

1 回答 1

1

我得到了这个工作。WKAudioFilePlayer用于通过蓝牙设备播放,而不是 Apple Watch 的扬声器。在 WatchOS 2.0 及更高版本中,您可以使用:

[self presentMediaPlayerControllerWithURL:url options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error){}];
于 2016-01-11T01:45:16.217 回答