I have some code that sets up an MPMoviePlayerController to play back a video stored in the app.
I follow the example code in Apple's documentation. However, the video plays even if I don't call -play on it as long as I have called -prepareToPlay.
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_videoName ofType:#"mp4"]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[player setMovieSourceType:MPMovieSourceTypeFile];
[player setScalingMode:MPMovieScalingModeAspectFit];
[[player view] setFrame:[[self view] bounds]];
[[self view] addSubview:[player view]];
[player prepareToPlay];
//[player play];
Will play the video. I uncomment the -play and the same thing happens. It does not matter if I have -play in or not. And I had -prepareToPlay earlier in the code as well (before the view setup) and it did not make a difference.
This is on iOS8 of some sort. I have not tried other versions of iOS.
Why does it work like this? The Apple docs make it sound like it should not play until -play is called.
Try adding this line of code
player.shouldAutoplay = NO;
By default it is set to YES.
Related
It will showing black screen with loading activity indicator When i click the video in full screen and touch up the forward or Backward button.
I have using the following code for movie player
NSURL *fURL = [NSURL URLWithString:[self.winnersCircleDictionary objectForKey:#"videoname"]];
self.players=nil;
self.players=[[MPMoviePlayerController alloc]initWithContentURL:fURL];
[self.players setContentURL:fURL];
self.players.scalingMode = MPMovieScalingModeAspectFit;
self.players.repeatMode=MPMovieRepeatModeOne;
[[self.players view] setFrame:_webviewWinnerCircle.frame];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.players];
object:nil];
self.players.shouldAutoplay=NO;
self.players.currentPlaybackTime=0.0;
[scrollViewWinnerCircle addSubview:self.players.view];
Forward and Backward button working fine in iOS 6 and this issue only have in iOS 7.
Thank You.
I just tried your code and got the issue , And solved like this.
Prepared the MoviePlayer like [self.players prepareToPlay]; for playing the selected video after adding the self.player.view as subview to the scrollViewWinnerCircle.
The code is as follows.
self.players = [[MPMoviePlayerController alloc]initWithContentURL:fURL];
[self.players setContentURL:fURL];
self.players.scalingMode = MPMovieScalingModeAspectFit;
self.players.repeatMode = MPMovieRepeatModeOne;
[[self.players view] setFrame:_containerView.frame];
self.players.shouldAutoplay = NO;
self.players.currentPlaybackTime = 0.0;
[_containerView addSubview:self.players.view];
[self.players prepareToPlay]; // Prepares a movie player for playback.
[self.players pause]; // Pause playback of the loaded movie
It was the problem with the source, also didn't specified the source type.
Do like the following, its working for me with the url i used here.
NSURL *fURL = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.players = nil;
self.players = [[MPMoviePlayerController alloc]initWithContentURL:fURL];
[self.players setContentURL:fURL];
// Specifying media source type
self.players.movieSourceType = MPMovieSourceTypeStreaming;
It take some time to load initially.
you have to use this code instead of MPMovieplayerController and import these library in you class:
AVKit
AVFoundation
NSURL *videoURL = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[player play];
[self presentViewController:playerViewController animated:YES completion:nil];
after using above code you will be able to forward and back forward video.
I'm new in iPhone Application Development, and now i faced a problem, can any one tell me how can i play a video randomly without pressing the play button. The video should start automatically while the application launch and play randomly without stopping. I don't have any idea how to do this. I don't have any code also. Please help me any one...
Thanks in advanced.
You could implement it like this:
You need a movie player configured with e.g. a file url and add it to your view
NSURL *url = [[NSBundle mainBundle] URLForResource:movieName withExtension:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = // set the frame
[self.view addSubview:moviePlayer.view];
Start the movie player
[moviePlayer play];
Listen to the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleMoviePlayerStatChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
Start the video again
- (void)handleMoviePlayerStatChange:(NSNotification *)note {
NSLog(#"mp playback state %i", moviePlayer.playbackState);
if (moviePlayer.playbackState == MPMoviePlaybackStatePaused) {
[moviePlayer play];
}
}
To hide the video controls set the controlStyle property of the movie player instance:
moviePlayer.controlStyle = MPMovieControlStyleNone;
I'm developing a iPhone app and need to play the animation video in the background. This video should run until the user touch's the screen of the app. Not exactly the screensaver but this should happen for the first time when the app is opened.
How can play the video file in the background and screen buttons on top of the video in the app?
Add video player in your view of subview and your view must be a transparent layer makes.
your app delegate check the user coming first time or not.
NSString *path = [[NSBundle mainBundle] pathForResource:#"testVideo" ofType:#"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];
Simply add MPMoviePlayerController as background and play the video,
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: video_url];
[player prepareToPlay];
[player.view setFrame: requiredFrame];
[self.view addSubview: player.view];
After that, add tap gesture to whole view and remove player on tap gesture method..
I have multiple videos on one view, when one video has finished playing, the user taps a button and the next video starts to play.
The video that has just played goes to a black screen.
I want the video to stop (pause) where it is, so the image is still there.
this is my code;
}
-(IBAction)play {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"origami_panel01" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playercontroller.moviePlayer.controlStyle = NO;
playercontroller.view.frame = CGRectMake(18, 20, 732.5, 360);
[self.view addSubview:playercontroller.view];
[playercontroller.moviePlayer play];
}
-(IBAction)play_3 {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"origami_panel02" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playercontroller.moviePlayer.controlStyle = NO;
playercontroller.view.frame = CGRectMake(18, 395, 233, 233);
[self.view addSubview:playercontroller.view];
[playercontroller.moviePlayer play];
}
It continues like this for a total of five videos.
Please can someone tell me how to make the video pause when another video is played. Thanks!
You can not have more than one playback active when using MPMoviePlayerController. Whereas being paused also counts as active.
You got at least these two options:
Use AVPlayer as that baby supports multiple active video playbacks concurrently.
Stick with MPMoviePlayerController but fake that pause-state by taking a snapshot image from the movie (thumbnailImageAtTime:timeOption:). Then use that image and display it instead of the player.
I am trying to get a videostream to work.
My Code works super, it starts playing (fetches the Video From an url provided by Apple Support Sites - the player plays the first four seconds of the Video and then when this little status bar thing wants to automatically hide the player goes blackscreen (with Status bar I mean the indicator of Time, with the fullscreen Button etc.).
I cant Post sourcecode right now because I am writing with my iPhone, so I will do that later...
Hope That my description helps you to understand and maybe you can help me.
In my .h File:
MPMoviePlayerController *mp;
In my .m File:
NSURL *mediaURL = [NSURL URLWithString:#"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"];
mp = [[MPMoviePlayerController alloc] init];
[mp setShouldAutoplay:YES];
[mp setMovieSourceType:MPMovieSourceTypeStreaming];
[mp setContentURL:mediaURL];
[mp setControlStyle:MPMovieControlStyleEmbedded];
[mp setFullscreen:NO];
[mp setScalingMode:MPMovieScalingModeNone];
[mp prepareToPlay];
mp.view.frame=CGRectMake(0, 0, 320, 220);
[self.view addSubview:mp.view];
this is working fine :)