This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Video file formats supported in iPhone
I Was Wondering which formats are ok to be inside an iPhone app, there is a view and I want to put this video there to start automatically...
This code should add a MPMoviePlayerController to your view and automatically begin playing using a video that is has been imported into your project.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
player.frame = CGRectMake(0, 0, 1024, 768);
[self.view addSubview:player.view];
player.fullscreen = NO;
[player play];
*for example I used a .mp4 filetype
Related
I managed to create this application that includes an animation in it. But I want to make it stand out and more fun by adding a .wav file that I downloaded from the web. I want the sound to play for the duration of the animation. Only started coding a month ago so any help would be deeply appreciated
You could use the AVAudioPlayer
just create a method and connect it to your button or animation
-(void) playLoopingMusicInApplication {
AVAudioPlayer *audioPlayer;
// set the pathForResource: to the name of the sound file, and ofType: to AAC preferrably.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"WAV"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.delegate = self;
//infinite
player.numberOfLoops = -1;
[player play];
//[player release];
}
PS - You will need to get the AVFoundation framework, and the AudioToolbox framework in your project
hope this helps!
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'm actually using the MPMoviePlayerController for play video in my iPad app.
Actually, I can easly play 1 video but I'm trying to play in the same time 2 video, here is my code :
// Look for the video in the main bundle
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
NSString *urlStr2 = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url2 = [NSURL fileURLWithPath:urlStr2];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,200, 200);
videoPlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:url2];
[self.view addSubview:videoPlayer2.view];
videoPlayer2.view.frame = CGRectMake(0, 300,200, 200);
[videoPlayer2 play];
NSLog(#"Video 1 playing");
[videoPlayer play];
NSLog(#"Video 2 playing");
The first video is correctly launched but not the second. (and btw the second video does not lauch after the first finished)
Here is my output :
2012-06-18 13:47:23.015 testMosaique[2498:11f03] Video 1 playing
2012-06-18 13:47:23.016 testMosaique[2498:11f03] Video 2 playing
Is there a way while using MPMoviePlayerController to play 2 or more videos at the same time?
Thank's
If you want to play more than one video at the same time you have to use AVPlayer frameworks. MPMovie only allowed you play one video at a time.
See AVPlayer documentation.
As Safecase mentioned, MPMovieplayerController will only allow one video to be played at a time. But here is an example to play two simultaneously with the use of AVFoundation:
http://www.sdkboy.com/?p=66
Hope this helps!
What I did is display 4 videos using AVPlayer, but those video are made from 4 another video (I create each videos with AVFoundation). Si I'm able the display thousand and thousand videos in only 4 players, pretty good performances when you play the videos !
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Play YouTube videos with MPMoviePlayerController instead of UIWebView
I am trying to play video from a youtube url and once mpmovieplayer is launched, it is been closed instantly. How to play video from youtube url? note that I am using iOS5. Thanks.
- (IBAction)video1 {
//NSBundle *bundle = [NSBundle mainBundle];
//NSString *moviePath =[bundle pathForResource:#"" ofType:#"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:#"http://www.youtube.com/testVideo"];
MPMoviePlayerController *daMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
daMovie.scalingMode = MPMovieScalingModeAspectFill;
[daMovie play];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
YouTube recommends you use the native YouTube-app, see this link for more info on that: http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
Otherwise, check out this question on StackOverflow: Play YouTube videos with MPMoviePlayerController instead of UIWebView