Want MPMoviePlayerController to active throughout the app - ios

I am using MPMoviePlayerController to stream audio clip from a url. Currently the MPMoviePlayerController working fine on the current ViewController where the MPMoviePlayerController initiate and the play action initiate.
Also the streaming working fine when the app goes background.
But, if I dismiss or go back from the Current ViewController, MPMoviePlayerController stop playing.
But my requirement is MPMoviePlayerController should continue playing/streaming throughout the app regardless of the navigation through view controllers.
In other words, each and every view controller should have MPMoviePlayerController controls visible for user to pause or stop the play manually.
How do I do it?
This is my current code.
// Initiate Audio Player in ViewDidLoad of SecondViewController
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
self.moviePlayer = [[MPMoviePlayerController alloc] init];
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.moviePlayer.view.hidden = NO;
[self.moviePlayer.view setFrame:CGRectMake(0, 0, videoContainer.frame.size.width, videoContainer.frame.size.height)];
[self.moviePlayer setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer.backgroundView.hidden = YES;
[videoContainer addSubview:self.moviePlayer.view];
// Play method in SecondViewController
NSURL *audioUrl = [NSURL URLWithString:strMessageUrl];
self.moviePlayer.contentURL=audioUrl;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
If user go back to FirstViewController, it stops

Put condition to check movie player is null or not.
if(!self.moviePlayer)
{
self.moviePlayer = [[MPMoviePlayerController alloc] init];
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.moviePlayer.view.hidden = NO;
[self.moviePlayer.view setFrame:CGRectMake(0, 0, videoContainer.frame.size.width, videoContainer.frame.size.height)];
[self.moviePlayer setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer.backgroundView.hidden = YES;
[videoContainer addSubview:self.moviePlayer.view];
}

Related

Video not play in Objective c

I want to play video on clicking a button. My AVPlayerViewController is open but not playing an video .Please help.
- (IBAction)btnPlay_Click:(id)sender {
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
If you are using vimeo video you can use third party tools which helps you load the vimeo video.
Solution 1: Please try this, It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Solution 2: YTVimeoExtractor
May be it will help you.
Use MPMoviePlayerController and add this line of code:
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self.view addSubview: [player view]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer prepareToPlay];
Add one more line : [self.view addSubview: [player view]];
movieController is an instance of MPMoviePlayerViewController declared in the .h file.
[movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.
--------- If this is not working you can just do that following ----------
player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];

MPMediaPlayback no audio ios 8

My application capture video. I have checked that video file has a sound but in application it is not playing with MPMoviePlayerController. I have noticed that for short video, less than 5 seconds, sound is playing.
-(void) showMovie:(NSString*)moviePath{
[self initializeMoviePlayer:moviePath];
[self setMoviePath:moviePath];
[movieController play];
}
-(void) initializeMoviePlayer:(NSString*)moviePath{
if (movieController == nil) {
movieController = [[MPMoviePlayerController alloc] initWithContentURL:[[NSURL alloc] initFileURLWithPath:moviePath]];
movieController.repeatMode = MPMovieRepeatModeOne;
[movieController prepareToPlay];
[movieController setUseApplicationAudioSession:NO];
[movieController setShouldAutoplay:YES];
[movieController setControlStyle:2];
movieController.fullscreen = YES;
movieController.scalingMode =MPMovieScalingModeAspectFill;
movieController.controlStyle = MPMovieControlStyleNone;
movieController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
[movieController.view setFrame: self.frame];
[self insertSubview:self.movieController.view atIndex:0];
}
}
Update:
Have added a bug in apple bug reporter with id 18823656

Playing .m3u8 file on iOS

I have .m3u8 link which I need to play on iOS which supports the HLS Protocol.
When I assign URL directly to the MPMoviePlayerController and play, video is not visible but I can hear the audio.
NSURL *movieURL = [NSURL URLWithString:#"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:self.moviePlayer.view];
if (mp)
{
// save the movie player object
self.moviePlayer = mp;
[self.moviePlayer setFullscreen:YES];
// Play the movie!
[self.moviePlayer play];
}
What additional stuff do I need to do on iOS side?
Import:
#import <MediaPlayer/MediaPlayer.h>
Then do:
NSURL *movieURL = [NSURL URLWithString:#"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp) {
mp.view.frame = self.view.bounds;
[self.view addSubview:mp.view];
// save the movie player object
[mp setFullscreen:YES];
// Play the movie!
[mp play];
self.moviePlayer = mp;
}

ipad movie paused rather play when page is loaded

I have a movie that loads fine and plays fine but it plays as soon as the page loads. I would like it to load in a paused state requiring the user to hen press play to watch.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController pause];
}
The pause on the last line does not seem to do anything. Any help? Thanks
Put your code blurb you've written above in viewwillappear.
Then add play at the end like so:
-(void) viewWillAppear: (BOOL) animated {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
}
at the end of it.
In a viewdidappear do a
-(void) viewDidAppear: (BOOL) animated {
[moviePlayerController pause];
}
Remember to remove code from viewDidLoad
You might want to try setting the MPMoviePlayerController property shouldAutoplay to NO.
Add the following line before your prepareToPlay call;
moviewPlayerController.shouldAutoplay = NO;
From the reference:
shouldAutoplay
A Boolean that indicates whether a movie should begin playback automatically.
#property (nonatomic) BOOL shouldAutoplay
Discussion
The default value of this property is YES. This property determines
whether the playback of network-based content begins automatically
when there is enough buffered data to ensure uninterrupted playback.
Availability
Available in iOS 3.2 and later.
Declared In

ShouldAutoplay on MoviePlayerController

I am a new developer, therefore I'm still learning and know I'm doing some things wrong. I have a segmented control object in which when the user presses one of the segments, it goes to play a video. I have it setup to where they press the segment, then have to press a play button to get the video to play. I want to cut out the play button and have it play automatically. This is where I'm having trouble. I found the shouldAutoplay option but when I use it and cut out the button, it won't take me to the video at all. I'm sure I'm not using the shouldAutoplay option correctly. Was hoping for some help or at least a point in the right direction.
- (IBAction)playMovie:(id)sender;
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"mytestimony" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.shouldAutoplay = YES;
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie autorelease];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
Have you tried [theMovie play];?
UPDATE: I just noticed you set shouldAutoplay in theMovie, but what you presented is another instance of MPMoviePlayerViewController, which is moviePlayer. That's why your movie did not autoplay. You should instead have:
[self presentMoviePlayerViewControllerAnimated:theMovie];
Don't play the video, just prepare it for playing and put the shouldAutoPlay as NO, because you don't want to video player to play it automatically.
-(void) SetVideoFile:(NSString *)fileName {
videoFileName=fileName;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[orientationHandler VideoStart];
[self.view addSubview:moviePlayer.view] ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
}
-(void)moviePlaybackDidFinish:(NSNotification *)notification {
// your custom code which you want to play after the player did finish playing
}

Resources