I used MPMoviePlayerController in my application.
I got the right URL but MPMoviePlayer does not play video it shows only black screen.
My code is as below:
[video setImage:[UIImage imageNamed:#"video_active.png"] forState:UIControlStateNormal];
NSString *filename=[NSString stringWithFormat:#"%#.mp4",[appdel.TempVideoUrl valueForKey:tag]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:filePath] ;
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(106, 18, 216, 235);
[uploadview addSubview:theMovie.view];
// Play the movie.
[theMovie pause];
I got the URL like this:
file://localhost/var/mobile/Applications/6E1D4CB7-A08D-438B-9FE7-E2FD9B7B5EEC/Documents/136_1355227629.mp4
Add [theMovie prepareToPlay]; before calling [theMovie play];.
prepareToPlay
Prepares a movie player for playback. (required)
- (void)prepareToPlay
Discussion
If a movie player is not already prepared to play when you call the
play method, that method automatically calls this method. However, to
minimize playback delay, call this method before you call play.
Calling this method may interrupt the movie player’s audio session.
For information on interruptions and how to resond to them, see Audio
Session Programming Guide. Availability
Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h
Also you can use isPreparedToPlay for checking whether a movie player is ready to play.
Please refer this link for more.
Related
I am getting video data from server. i am using signalR Framework. first of all i am capturing video from iPhone photo library. Then next i am converting encoding format (base64EncodedStringwithOptions) method. next i am sending video encrypted data to signalR server. and next i am receiving video data and decrypted and play the video data using AVPlayer. but my problem video does not played. but i received video data from signalR. For decrypting i am using this method. (NSDataBase64DecodingIgnoreUnknownCharacters)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *videoFile=[documentDirectory stringByAppendingPathComponent:post.mediaName];
NSURL *url;
if ([[NSFileManager defaultManager] fileExistsAtPath:videoFile]) {
url=[NSURL URLWithString:videoFile];
}
NSLog(#"Video URL is:%#",url);
AVPlayer *player = [AVPlayer playerWithURL:url];
cell.videoPlayerController.player = player;
[player play];
i got Video URL like this:
Video URL is:/Users/Library/Developer/CoreSimulator/Devices/EEC96DE7-6D0F-4D80-82B8-3C24E4F6B3EF/data/Containers/Data/Applicatication video0006.mp4
You can use MPMoviePlayerController to play video from url' but make sure that you have to declare property for that like,
#property MPMoviePlayerController *videoController;
don't create local object of MPMoviePlayerController.
you can use it something like,
self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:tempView.videoURL]; //here your url
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.videoController.view];
// close or cancel button if you want to put
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 20, 60, 30)];
[closeButton addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Cancel" forState:UIControlStateNormal];
[self.videoController.view addSubview:closeButton];
// add observer to notify when playing will be completed if you want to put
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoController];
[self.videoController play];
Update :
You must use file url like,
NSURL *url = [NSURL fileURLWithPath:videoFile];
instead of
[NSURL URLWithString:videoFile];
https://dl.dropboxusercontent.com/u/52719649/69090d547c8d47dd27b6d271649c59ae.mp4
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"SampleVideo.mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
self.controller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self.controller.moviePlayer setControlStyle:MPMovieControlStyleDefault];
[self presentMoviePlayerViewControllerAnimated:self.controller];
[self.controller.moviePlayer play];
you need to recompress the video as the current compression is incorrect:
H.264 Main Profile Level 4.0 video
here is a guidance form Apple:
H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps. (The Baseline profile does not support B frames.)
MPEG-4 Part 2 video (Simple Profile)
the audio might need to be changed according to:
(...) this class supports AAC-LC audio at up to 48 kHz, and MP3 (MPEG-1 Audio Layer 3) up to 48 kHz, stereo audio.
for MPMoviePlayerController (Class Reference in Apple's Documentation).
You should use AVPlayer and AVPlayerViewController instead. Follow this simple guide in order to play videos natively in your app:
http://www.techotopia.com/index.php/IOS_8_Video_Playback_using_AVPlayer_and_AVPlayerViewController
I have code that records the screen in my app, and then I want to be able to play it back. I have gotten so far as to get to the black loading screen with the rewind, play and fast forward buttons, but it hangs at loading without playing anything.
This code plays the clip:
-(IBAction)playMovie:(id)sender
{
MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:locationOfVideoURL];
movieViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:movieViewController];
}
I believe the problem has something to do with the locationOfVideoURL variable. I am able to print it and it seems like it SHOULD work.
Here it is printed out:
file://localhost/Users/myusername/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/[left out, personal info]/Documents/Videoclip.mp4
Any ideas? Any help would be greatly appreciated! thank you
EDIT: Location of video clip:
NSString *outputPath = [[NSString alloc] initWithFormat:#"%#/%#", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], #"Videoclip.mp4"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSLog(#"Completed recording, file is stored at: %#", outputURL);
In my app I use AVAudioPlayer to play some mp3. My code is:
- (void) play{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
[arraysound objectAtIndex:appDelegate.indexSound]];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
soundID.delegate = self;
[soundID prepareToPlay];
[soundID play];
}
but I have 40 sounds to play; I use everytime soundID to play my sounds
example I have sound1.mp3, sound2.mp3, sound3.mp3......
How can I release my soundId?
When my sound change I call "play" method but this method alloc a new AVAudioPlayer everytime
I Know
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
but I don't understand how to release my sound id inside this delegate method, because if I release inside soundId my app crash
after some minutes that I use my app I have with crash this message "shm_open failed: "Apple Audio Queue"
please help me...thanks
You don't want to release soundID in your delegate method. You want to release player. Judging from your code, soundID is an ivar. When the delegate gets called, soundID may have been set to another value. But the player parameter will be the AVAudioPlayer that has just finished playing.
I have a ViewController which plays a movie. If the movie was downloaded (with ASI-HTTP-Request) it takes the local version. Otherwise it streams it from the web. It's the exact same movie.
Streaming does work very well, but playing the local file does not work. Just a black screen and no controls. The file has been downloaded correctly I checked the md5 checksum.
It's a Apple QuickTime Video (.mov) at a size of 1024x5xx pixel.
player = [[MPMoviePlayerController alloc] init];
[player.view setFrame: self.view.bounds];
if (local) {
// DOES not work
// SHOULD be [NSURL fileURLWithString:...]
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
} else {
// does work
[player setContentURL: [NSURL URLWithString: #"http://mydomain.tld/path/to/the/movie.mov"]];
}
[self.view addSubview: player.view];
[player play]
I tried around with the players MediaSourceType. But it didn't help.
Why does it not work?
I don't have any error messages because I dont receive one..., is there a way to get error messages from it?
Solution
I found my error.
I used [NSURL URLWithString: #"/the/path/..."] for the local path.
It works with [NSURL fileURLWithPath: #"/the/path/..."]
Check URL to video file. This line return path to Documents directory.
NSString* docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Your path is not related to Document directory
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
Try this
[player setContentURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#%#", docPath, #"/path/to/the/movie.mov"]]];