问题描述
好的,所以我的应用播放视频,我希望声音继续在后台播放.但是,当我按下主页"按钮时,播放停止.我正在使用iOS 5.0.1的 iPhone 4s
Ok, so my app plays videos, I want the sound to continue playing in the background. But when I hit the "Home" button the playback stops. I'm testing on iPhone 4s with iOS 5.0.1
正在做什么?
- 如果视频正在播放并且我锁定屏幕,它会继续播放,
- 我可以通过锁定屏幕控件控制播放:下一个,上一个,播放,暂停.
- 如果我按"主页",然后从多任务栏中转到"远程控制",则有效.
- If the video is playing and I lock the screen, it keeps playing,
- I Can control the playback from the lock screen controls: next, previous, play, pause.
- If I press "home", then go to the "remote controls" from the multitask bar, it works.
怎么了?
- 我希望应用程序在按主页"按钮时继续播放,因为当我这样做时,音频会停止.
我做了什么:
- 在我的app.plist中的必需的背景模式"中添加了音频"
将以下代码添加到应用程序委托的 application:didFinishLaunchingWithOptions:
Added the following code to app delegate's application:didFinishLaunchingWithOptions:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
在我的视图控制器中,我已经实现了用于接收远程控制的方法:
In my view controller I've implemented the methods to receive remote controls:
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//End recieving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self play:nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self actionPlaybackPreviousItem];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self actionPlaybackNextItem];
break;
default:
break;
}
}
}
一切正常,但是,这是什么?
How come everything works but, this?
推荐答案
每次,堆栈溢出的响应都越来越少了……希望对所有人来说都还不错.我找不到任何回应,为什么在按下主页"按钮后视频会暂停,但是,如果有任何帮助,我会提供一种解决方法:
Stack Overflow is becoming less and less responsive each time... Hopefully it's not so bad for everybody out there. I've found nothing to respond why is it that the video is paused after hitting the Home button, however, I've come with a workaround if it's of help to anyone:
由于播放器一直在播放,直到应用程序进入后台,所以我创建了一个计时器,以检查应用程序是否在前台播放,如果是,则恢复播放:
Since the player is playing until the app goes to the background, i've created a timer, to check if the app was playing while it was in the foreground, if so, resume playback:
- (void)applicationWillResignActive:(UIApplication *)application
{
MainViewController* mainController=self.viewController;
playerWasPlaying=mainController.player.rate!=0;
}
-(void)resumePlayback {
MainViewController* mainController=self.viewController;
if (mainController.player.rate==0&&playerWasPlaying)
[mainController play:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(resumePlayback) userInfo:nil repeats:NO];
}
我认为这不是最好的选择,但是现在它可以正常工作了,在恢复播放的过程中略有跳跃.
Not the best option in my opinion, but for now it's working, with a little jump in the playback while it resumes.
这篇关于AVPlayer:按下主屏幕按钮后,背景音频会停止,但会在锁定屏幕的情况下播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!