I am trying to play a short 6 second video in my view using MPMoviePlayerController and would like it to loop indefinitely. I used the following code to achieve this:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"MyVideo" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.view.frame = CGRectMake(10, 240, 300, 163);
moviePlayerController.controlStyle = MPMovieControlStyleNone;
moviePlayerController.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
The problem is that the video starts playing in a loop as desired, but then eventually stops after a random number of iterations. Note that the file size of the video is very small (less than 500 KB).
After some thought, I speculated that the behavior may be attributed to the fact that I am running the above code inside the main thread. In an effort to run the above code in its own thread, I tried the following:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSThread detachNewThreadSelector:#selector(playVideo) toTarget:self withObject:nil];
}
-(void) playVideo
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"MyVideo" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.view.frame = CGRectMake(10, 240, 300, 163);
moviePlayerController.controlStyle = MPMovieControlStyleNone;
moviePlayerController.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
Now, the problem is that the MPMoviePlayerController's view's frame just appears as a black rectangle where the video should be but no video plays.
I would really appreciate any help into how to get this to work as desired (I don't care if its with or without additional threads). All I need is to have a video play and loop continuously.
Thanks in advance!
I have the same issue. But using the separate thread for this is not a good solution.
1. Working with UIKit (addSubview method) in not main thread could give you unexpected behavior.
2. It looks like that MPMoviePlayerController has its own background thread. So all this stuff working in other thread already.
Probable solution could be in using MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerPlaybackStateDidChangeNotification notifications and manual restoring playback.
Related
I am using following code to show video file in initial state.But when i launch app it start playing without giving me option to play.how i can achieve that
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"En_annan_resa_Master_ENG_PC" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movie_obj = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[movie_obj.view setFrame:CGRectMake(1024*i, 0, 1024, 748)];
movie_obj.scalingMode=MPMovieScalingModeAspectFit;
[self.scrollView addSubview:movie_obj.view];
[movie_obj prepareToPlay];
I am expecting when i launch app it show mw video view and show control to play do not play automatically.
thanks for your help
check below answer
https://stackoverflow.com/a/15701076/1713478
just replace NO to En_annan_resa_Master_ENG_PC
try this your problem will solve
I use MPMoviePlayerController because I want to play movie.
One question. When I create MPMoviePlayerController, the app stops a little.
This is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.scalingMode=MPMovieScalingModeFill;
moviePlayer.controlStyle=MPMovieControlStyleNone;
moviePlayer.shouldAutoplay=NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieRestartCallback:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[moviePlayer.view setFrame:CGRectMake(20, 20, 280, 200)];
[self.view addSubview:moviePlayer.view];
`
Why does the app stop?
Is this problem resolution the way?
If you look at the docs for MPMoviePlayerController you'll see that in the example they call prepareToPlay before even adding the movie player to the view.
I would add [mediaPlayer prepareToPlay] where you're setting up the player.
MPMediaPlayback Protocol Reference says:
"to minimize playback delay, call this method before you call play"
I think that you can use multi-threading functions.
NSOperationQueue is thread safe (you can add operations to it from different threads without the need for locks) and enables you to chain NSOperation objects together.
You can use following code snap to use them.
NSOperationQueue *queue = [NSOperationQueue mainQueue];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
// your code here
}];
[queue addOperation:operation];
I hope that is helpful to you.
I need to play a simple video on my app. I been looking on the internet on how to play a video and I found out that I need to import the MediaPlayer.framwork which I did. I have a Video named:
and the code that I have in order to play it:
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Final_Valores_Pacific"
ofType:#"m4v"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
I am missing the code to add it to a view but just that code creates a leak:
what is the right way of playing a video? how can avoid that memory leak?
When you alloc an object, you have to release it at the end:
// ...some code
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Final_Valores_Pacific"
ofType:#"m4v"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
// ...use player
[player release];
Are you getting this leak on the simulator? I got this on an app that I worked on in the past only on the simulator, but not on the iPhone.
There are similar questions about this too:
iPhone: OpenAL & AudioToolbox leak
I have an application where I need to play a video. I used following code. There is a problem with Home button, if I press the Home button during video playing I'm getting EXC_BAD_ACCESS. I even have no idea why it may happens. Please help.
NSString * videoString = [[NSBundle mainBundle] pathForResource:#"video.mp4" ofType:nil];
NSURL * url = [NSURL fileURLWithPath:videoString];
MPMoviePlayerController * videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,768, 1024);
[videoPlayer play];
In your application controller's applicationWillResignActive method, call [videoPlayer stop].
-(IBAction)playSound{ AVAudioPlayer *myExampleSound;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound play];
}
I want to play a beep sound when a button is clicked. I had used the above code. But it is taking some delay in playing the sound.
Anyone please help.
There are two sources of the delay. The first one is bigger and can be eliminated using the prepareToPlay method of AVAudioPlayer. This means you have to declare myExampleSound as a class variable and initialize it some time before you are going to need it (and of course call the prepareToPlay after initialization):
- (void) viewDidLoadOrSomethingLikeThat
{
NSString *myExamplePath = [[NSBundle mainBundle]
pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound prepareToPlay];
}
- (IBAction) playSound {
[myExampleSound play];
}
This should take the lag down to about 20 milliseconds, which is probably fine for your needs. If not, you’ll have to abandon AVAudioPlayer and switch to some other way of playing the sounds (like the Finch sound engine).
See also my own question about lags in AVAudioPlayer.
AudioServicesPlaySystemSound is an option. Tutorial here, sample code here.