gpt4 book ai didi

iphone - MPMoviePlayerViewController 在后台运行并使用远程控制

转载 作者:行者123 更新时间:2023-11-29 05:01:14 28 4
gpt4 key购买 nike

我目前在 iOS 4.3.5 上运行,并尝试让我的 MPMoviePlayerViewController 在进入后台后继续播放。

我按照上面的描述实现了所有内容

http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html

我还将 UIBackgroundMode 设置为音频

我的自定义 MPMoviePlayerViewController 类是从 TabBarApplication 中这样调用的:

    NSURL *streamUrl = [NSURL URLWithString:STREAM_URL];

self.playerViewController = [[CustomMoviePlayerViewController alloc] initWithContentURL:streamUrl];

// Register for the playback finished notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerViewController.moviePlayer];
// Present
[self presentMoviePlayerViewControllerAnimated:self.playerViewController];

// Play the movie!
self.playerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.playerViewController.moviePlayer prepareToPlay];
[self.playerViewController.moviePlayer play];

我的CustomMovePlayerController内部如下所示:

-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}

-(void)viewWillAppear:(BOOL)animated {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];

[self resignFirstResponder];
[super viewWillDisappear:animated];
}

-(BOOL)canBecomeFirstResponder {
return YES;
}

-(void)remoteControlReceivedWithEvent:(UIEvent *)event {
[super remoteControlReceivedWithEvent:event];
NSLog(@"remoteControlReceived");
NSLog(@"%d", [[AVAudioSession sharedInstance] isActive]);
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.moviePlayer play];
break;

case UIEventSubtypeRemoteControlPause:
[self.moviePlayer pause];
break;
default:
break;
}
}
}

我的 MPMoviePlayerViewController 的主要问题是,它不响应 remoteControlReceivedWithEvent 消息,这是为什么?我是否子类化了错误的东西?我的基于选项卡栏的应用程序是否会阻止我这样做?

最后但并非最不重要的 - applicationDidFinishLaunchingWithOptions 包含以下内容:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];

我只是不知道缺少了什么...非常感谢所有帮助!

最佳答案

我想我可以回答这两个问题:)

背景问题:

您想播放什么格式的文件?我的应用程序使用 MPMoviePlayerViewController 作为背景音频已有一段时间了,但最近有报告称它不再在后台播放。

原来是AAC文件;它们是带有章节和每章封面艺术的播客... iOS 5 添加了对它们的改进支持,但它必须使 MPMoviePlayerViewController 认为它正在播放视频。据我所知,iOS 4.3 左右背景视频被关闭。

尝试使用普通 MP3 文件,它仍然适用于我。我即将将 AAC 问题记录为 Apple 的错误。

远程控制问题:

锁屏和通知栏遥控按钮都会发送UIEventSubtypeRemoteControlTogglePlayPause事件,而不是单独播放和暂停。所以我处理这样的事件:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if (controller.playbackState == MPMusicPlaybackStatePlaying) {
[controller pause];
} else {
[controller play];
}
break;
//etc
}

关于iphone - MPMoviePlayerViewController 在后台运行并使用远程控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6893323/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com