iOS MPMoviePlayerController在后台播放音频

编程入门 行业动态 更新时间:2024-10-25 14:28:59
本文介绍了iOS MPMoviePlayerController在后台播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有MPMoviePlayerController应该在后台播放视频的音频,应该由多任务播放/暂停控件控制。

I have MPMoviePlayerController that should play video's audio in background and should be controlled by the multitasking play/pause controls.

用所需的背景模式并调用以下内容:

- (void)startBackgroundStreaming { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; NSError *activationError = nil; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:&activationError]; [audioSession setActive:YES error:&activationError];

}

应用图标显示在多任务播放/暂停栏,但这些按钮没有响应。

The app icon appears in the multitasking play/pause bar, but these buttons don't respond.

谢谢!

推荐答案

这个难题的缺失部分是处理你收到的遥控事件。您可以通过在应用程序委托中实现 - (void)remoteControlReceivedWithEvent:(UIEvent *)事件方法来完成此操作。最简单的形式如下:

The missing piece of the puzzle is handling the remote control events you are receiving. You do this by implementing the -(void)remoteControlReceivedWithEvent:(UIEvent *)event method in your application delegate. In its simplest form it would look like:

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{ if (event.type == UIEventTypeRemoteControl){ switch (event.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: // Toggle play pause break; default: break; } } }

但是这个方法被调用应用程序委托,但您始终可以将事件作为对象发布通知,以便拥有电影播放器​​控制器的视图控制器可以获取事件,如下所示:

However this method is called on the application delegate, but you can always post a notification with the event as the object so that the view controller that owns the movie player controller can get the event, like so:

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{ [[NSNotificationCenter defaultCenter] postNotificationName:@"RemoteControlEventReceived" object:event]; }

然后在分配给通知的侦听器方法中获取事件对象。

Then grab the event object in the listener method you assign to the notification.

-(void)remoteControlEventNotification:(NSNotification *)note{ UIEvent *event = note.object; if (event.type == UIEventTypeRemoteControl){ switch (event.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: if (_moviePlayerController.playbackState == MPMoviePlaybackStatePlaying){ [_moviePlayerController pause]; } else { [_moviePlayerController play]; } break; // You get the idea. default: break; } } }

更多推荐

iOS MPMoviePlayerController在后台播放音频

本文发布于:2023-07-11 09:07:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093523.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   音频   iOS   MPMoviePlayerController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!