【问题标题】:presentMediaPlayerControllerWithURL dismissing immediately without playing the audiopresentMediaPlayerControllerWithURL 立即关闭而不播放音频
【发布时间】:2015-10-29 12:13:27
【问题描述】:

我正在创建一个手表应用程序来录制音频并播放该音频。录制的音频保存在应用程序组中,我使用以下代码播放音频。

- (void)playAudio
{
    // Playing the audio from the url using the default controller
    [self presentMediaPlayerControllerWithURL:self.audioFileURL options:@{WKMediaPlayerControllerOptionsAutoplayKey : @YES} completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * _Nullable error) {
        NSLog(@"Error = %@",error);
    }];
}

不幸的是,我收到以下错误,并且播放器控制器立即被关闭。

Error Domain=com.apple.watchkit.errors Code=4 "操作无法完成" UserInfo={NSLocalizedFailureReason=发生未知错误 (1), NSUnderlyingError=0x16daa810 {Error Domain=NSPOSIXErrorDomain Code=1 "操作not allowed"}, NSLocalizedDescription=操作无法完成}。

然后我从 Apple WatchOS 开发人员库下载了一个示例项目 (WatchKitAudioRecorder Sample code),但他们也遇到了同样的问题。我不知道为什么即使在 Apple 示例代码中这也不起作用:(

【问题讨论】:

    标签: objective-c watchkit watchos-2 audio-player


    【解决方案1】:

    最后我发现数据没有写入url路径。也应该写在以下路径:Library/Caches/filename.extension 这是一个示例代码,可能会对某人有所帮助。

    - (void)writeAudioToAppGroupsWithData:(NSData *)audioData
    {
        // Writing the audio data to the App Groups
        NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_IDENTIFIER];
        containerURL = [containerURL URLByAppendingPathComponent:[NSString stringWithFormat:DIRECTORY_PATH]];
        [audioData writeToURL:containerURL atomically:YES];
    }
    

    在这种情况下,请确保您的 DIRECTORY_PATHLibrary/Caches/filename.extension。例如:Library/Caches/Audio.mp3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多