【问题标题】:session:didReceiveFile: Not Playing Audio File (iPhone to Watch) [duplicate]会话:didReceiveFile:不播放音频文件(iPhone 观看)[重复]
【发布时间】:2016-01-10 19:52:27
【问题描述】:

我在获取我在 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");
    }
}

这是我在 Watch 上从 iPhone 接收的代码。

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

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

【问题讨论】:

  • WatchKit 扩展是否正在接收文件?
  • 我认为 group.myCompany.myApp 在 WatchKit OS2 中不起作用。从主应用获取数据有不同的协议
  • 没有@SamB,我正在尝试传输文件,而不是消息

标签: ios objective-c iphone watchos-2 avaudiofile


【解决方案1】:

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

[self presentMediaPlayerControllerWithURL:url options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error){}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多