在Mpmovieplayer或Mpmusicplayer播放youtube视频ios

问题描述:

我想用Mpmovieplayer或Mpmusicplayer播放youtube视频。我想这样做,因为我有要求让应用继续在后台模式下播放视频,而这在uiwebview中是不可能的。如果你能回答记录背景模式音频的情况,请不吝赐教。在此先感谢:)在Mpmovieplayer或Mpmusicplayer播放youtube视频ios

您可以使用MPMovieplayerviewcontroller &它也看起来像iPhone的默认播放器。

这里是我的回答:how to play live streaming from url for a camera that broadcasting live

FOR BACKGROUND:对于下面的viewDidLoad方法行后台播放写 -

NSError *setCategoryErr = nil; 
    NSError *activationErr = nil; 
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr]; 
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr]; 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; 
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; 

,也使您的info.plist像下面的图像的小变化: enter image description here

+0

谢谢@Vishal + 1为贵 “的直播。” :) – TonyMkenu 2013-04-12 09:14:09

我成功找到了UIWebView的解决方案,看到这个破解后https://gist.github.com/romainbriche/2308668

它在IO5/6和IOS7上有所不同。

您遇到的问题将是当应用程序在后台输入时不停止声音。 我们在这里工作https://github.com/0xced/XCDYouTubeVideoPlayerViewController/issues/10#issuecomment-34252488

我发现了一些解决方案,但并不完善。

写下面给出代码didFinishLaunchingWithOptions

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
NSError *setCategoryError = nil; 

BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; 
if (!success) { NSLog(@"Error"); } else {NSLog(@"Success");} 


NSError *activationError = nil; 
success = [audioSession setActive:YES error:&activationError]; 
if (!success) { NSLog(@"Error"); } else {NSLog(@"Success");}