是否可以在后台使用Avplayer播放视频? [英] Is it possible to play video using Avplayer in Background?
问题描述
我正在使用Avplayer来显示视频片段,当我回去(应用程序在后台)视频停止时。我怎样才能继续播放视频?
I am using Avplayer to show video clips and when i go back (app in background) video stop. How can i keep playing the video?
我搜索了后台任务&后台线程,在后台IOS只支持音乐表演(并非视频)
的 http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
I have search about background task & background thread ,IOS only support music in background (Not video) http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
这里关于在后台播放视频的一些讨论
here is some discussion about play video in background
1) https://discussions.apple.com/thread/2799090?start=0&tstart=0
2)http://www.cocoawithlove.com/2011/04/background-audio-through-ios-movie .html
但AppStore中有很多应用,可以在背景中播放视频
But there are many apps in AppStore, that play video in Background like
Swift Player: https://itunes.apple.com/us/app/swift-player-speed-up -video / id545216639?mt = 8& ign-mpt = uo%3D2
Swift Player : https://itunes.apple.com/us/app/swift-player-speed-up-video/id545216639?mt=8&ign-mpt=uo%3D2
SpeedUpTV: https://itunes.apple.com/ua/app/speeduptv/id386986953?mt=8
SpeedUpTV : https://itunes.apple.com/ua/app/speeduptv/id386986953?mt=8
推荐答案
此方法支持所有可能性:
This method supports all the possibilities:
- 用户锁定的屏幕;
- 列表项目
- 按下主页按钮;
只要你有一个运行iOS的AVPlayer实例就可以防止设备自动锁定。
As long as you have an instance of AVPlayer running iOS prevents auto lock of the device.
首先你需要配置应用程序来支持来自Info.plist的音频背景文件在UIBackgroundModes数组中添加音频元素。
First you need to configure the application to support audio background from the Info.plist file adding in the UIBackgroundModes array the audio element.
然后将你的AppDelegate.m放入
Then put in your AppDelegate.m into
- (BOOL)应用程序:(UIApplication *)application didFinishLaunchingWithOptions:(NSD ictionary *)launchOptions:
这些方法
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
和 #import< AVFoundation / AVFoundation.h>
然后在控制AVPlayer的视图控制器中
Then in your view controller that controls AVPlayer
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
和
- (void)viewWillDisappear:(BOOL)animated
{
[mPlayer pause];
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
然后回复
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if([mPlayer rate] == 0){
[mPlayer play];
} else {
[mPlayer pause];
}
break;
case UIEventSubtypeRemoteControlPlay:
[mPlayer play];
break;
case UIEventSubtypeRemoteControlPause:
[mPlayer pause];
break;
default:
break;
}
}
如果用户需要另一个技巧来恢复复制按下主页按钮(在这种情况下,复制暂停淡出)。
Another trick is needed to resume the reproduction if the user presses the home button (in which case the reproduction is suspended with a fade out).
当你控制视频的再现时(我有播放方法)设置
When you control the reproduction of the video (I have play methods) set
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
以及将启动计时器并恢复复制的相应方法。
and the corresponding method to be invoked that will launch a timer and resume the reproduction.
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
[mPlayer performSelector:@selector(play) withObject:nil afterDelay:0.01];
}
它适用于我在Backgorund中播放视频。
感谢所有。
Its works for me to play video in Backgorund. Thanks to all.
这篇关于是否可以在后台使用Avplayer播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!