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].
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
After the app starts, and I press the start button, it s "lagg" but I used this code into viewdidload and viewdidappear too:
gombhang = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"button4" ofType:#"mp3"]];
gombha = [[AVAudioPlayer alloc] initWithContentsOfURL:gombhang error:nil];
[gombha prepareToPlay];
gombha.delegate = self;
How can I fix this lagg? If I can t fix it apple will reject it?
As I know Audioplayer takes few seconds to load the audio file .
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"button4" ofType:#"wav"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile]; AudioServicesCreateSystemSoundID((CFURLRef) CFBridgingRetain(soundFileURL), &_MySound); AudioServicesPlaySystemSound(_MySound);
And another way: I made an empty/ muted 0.1 sec sound file, and played in viewdidload, and this method loaded the avaudioplayer before I clicked on the start button, so it doesn t lagg anymore.
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.
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
Hey guys how can I make an app keep playing an mp3 after pressed the hold/power button.
Here is the code I use for preparing the AVAudioPlayer:
- (void)PrepareAudio:(int)index
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#",[_Audio objectAtIndex:Game]] ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
MusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: newURL error: nil];
[newURL release];
[MusicPlayer setVolume: 1.5];
}
And this is the code when I press the button play:
- (IBAction)PushPlay: (id)sender
{
if(!MusicPlayer.playing)
[MusicPlayer play];
}
Best Regards
Carlos Vargas
If you are holding long enough to turn the device off, then there is no way. If you are pressing the button to lock the screen, the music can be played. Why don't you put some code up and let us see what you have tried. Hint: The answer lies within the Audio playback categories.