Playing a video from session.outputURL with MPMoviePlayerController - ios

I'm trying to play a captured video that recorded with AVFoundation and saved in the device's library.
I have the url of the video:
NSURL *movieURL = session.outputURL;
output:
file:///private/var/mobile/Containers/Data/Application/F255AC7A-2E0C-40B2-A195-52C03ED5B299/tmp/video.mp4
Now I want to play this video, and I think that the best player for this is the MPMoviePlayerController player (let me know if I'm wrong).
This is my code: but for some reason its not working:
NSURL *movieURL = session.outputURL;
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[player prepareToPlay];
[player.view setFrame: CGRectMake(0, 0, 200, 200)];
[self.view addSubview: player.view];
[player play];

Use below
player = [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"video" ofType:#"mp4"]]];
It will work then

Related

How to play mp3 file from url in iOS?

I am trying to play mp3 file located somewhere on remote server.I have the url of that music fileI have tried it playing it using AVAudioPlayer or AVPlayer but i am not able to play the mp3 file.Please tell me how can i play the mp3 file.
code to play music:
NSString *aSongURL = [NSString stringWithFormat:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
// NSLog(#"Song URL : %#", aSongURL);
AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
[anAudioStreamer play];
// Access Current Time
NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);
// Access Duration
NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);
Please tell me how do i do it?
Try Below Code.
NSURL *url = [NSURL URLWithString:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:url];
[self.player play];
Try use AVAudioPlayer, see below
NSString *aSongURL = [NSString stringWithFormat:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:aSongURL] error:nil];
[audioPlayer play];
Hope it works.
Cheers.

How to play .mp4 video in MPMoviePlayer controller

Kindly tell me how to play a .mp4 format video play in MPMoviePlayerController
NSString *urlStr=[[NSBundle mainBundle]pathForResource:#"Tour.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0,154,720,428);
[moviePlayer play];
Add MediaPlayer Framework
import it to your file
#import <MediaPlayer/MediaPlayer.h>
Create an object of MPMoviePlayerController
MPMoviePlayerController * moviePlayer;
write this code where you wants to play video
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"test.mp4" ofType:nil];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
[moviePlayer play];
try above code...

How to play https:// url video on MPMoviePlayerController

I have a https:// base video url, Please let me know how to play video through MPMoviePlayerController.
I use following code:
MPMoviePlayerViewController * moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
[target presentMoviePlayerViewControllerAnimated:moviePlayer];
But it is not working. and send ssl error.
I think the movieUrl of the MPmovieplayer is "string"
NSURL *url = [NSURL URLWithString:movieurl];
MPMoviePlayerViewController *moviePlayer =
[[MPMoviePlayerViewController alloc] initWithContentURL:url];

MPMoviePlayerController showing white background only

in my app i want to play a video in an MPMoviePlayerController, but when i run the app it shows me only a white frame. here's the code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Problems" ofType:#"MP4"];
NSLog(#"path: %#", moviePath);
//NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
player.view.frame = CGRectMake(0, 0, 700, 700);
player.view.backgroundColor = [UIColor whiteColor];
player.controlStyle = MPMovieControlStyleDefault;
player.shouldAutoplay = YES;
[self.detailView addSubview:player.view];
[player play];
Edit: i tried this code instead and added an #property for the MPMoviePlayerController. i can see now the player but the app crashes and the breakpoint couldn't identify where. here's the code:
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Problems" ofType:#"MP4"]]];
[self.player.view setFrame:CGRectMake(0, 0, 320, 320)];
[self.detailView addSubview:self.player.view];
[self.player play];
You should use the method 'presentMoviePlayerViewControllerAnimated:':
[self presentMoviePlayerViewControllerAnimated:self.player];

iOS play background video in a view

I've added this code to the viewDidLoad method:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"myvid" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.view insertSubview: moviePlayer.view aboveSubview: self.view];
[moviePlayer play];
it doesn't work! how can I play a video in the background of a view in iOS? (using storyboards)
It looks like the instance of MPMoviePlayerController you create is wounded by ARC during the playback.
Try making the strong property of MPMoviePlayerController and assign the instance you create there before starting playback.
So your code will look like this
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"myvid" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
//set the frame of movie player
self.moviePlayer.view.frame = CGRectMake(0, 0, 200, 200);
[self.view addSubView:self.moviePlayer.view];
[self.moviePlayer play];

Resources