Najeebullah Shah
Najeebullah Shah

Reputation: 3709

Playing a youtube video in Mpmovieplayer or Mpmusicplayer ios

I want to play a youtube video with Mpmovieplayer or Mpmusicplayer. I want to do this because i have a requirement to make the app keep playing the video in background mode and that is quite not possible in uiwebview. Kindly if you could answer keeping the background mode audio playing in mind. Thanks in advance :)

Upvotes: 0

Views: 1183

Answers (3)

Ga Ne Sh
Ga Ne Sh

Reputation: 59

Write Given Below code in 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");}

Upvotes: 0

Damien Romito
Damien Romito

Reputation: 10055

I successfully found a solution for the UIWebView, After see this hack https://gist.github.com/romainbriche/2308668

It's different on IO5/6 and IOS7.

The problem you will encounter will be to not stop the sound when the app enter in background. We work on it here https://github.com/0xced/XCDYouTubeVideoPlayerViewController/issues/10#issuecomment-34252488

I found some solutions but not perfects.

Upvotes: 0

Vishal
Vishal

Reputation: 8256

You can use MPMovieplayerviewcontroller & also it looks like default player of iPhone.

Here is my answer : how to play live streaming from url for a camera that broadcasting live

FOR BACKGROUND: For background play write below lines in ViewDidLoad method -

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];

And also make a small change in your info.plist like below image: enter image description here

Upvotes: 3

Related Questions