MediaPlayer won't play video - iphone

I'm implementing a Media player in my iPad app, but the video won't play at all. It shows the video's first frame, but as soon as I press play, it pauses instantly after.
Any idea why this is happening? I think it probably has to do with the iPad being version 3.2.
Below is my code. Thanks in advance!
- (void)viewDidLoad {
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"Reel.m4v" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(30, 180, 964, 493);
[videoPlayer stop];
[super viewDidLoad];
}

I figured it out through this post: Ipad MPMovieplayerController video loads but automatically pauses when played
I had to set this videoPlayer.useApplicationAudioSession = NO;

Related

I Want the Video file to Play repeatedly in my application an I am using the MPMoviePlayerController to play the video file

HI Friends,
I Want the Video File To Play repeatedly in my application and i have used the Following code to play the video file
NSString *tempurl = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Movie.m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:tempurl]];
player.view.frame = CGRectMake(0, 0, 867, 1008);
player.scalingMode = MPMovieScalingModeFill;
[self.view addSubview:player.view];
[player play];
Can I Have The Delegate method that can give the alert for the video file end playing
Thank You In Advance
you can use the property "repeatMode" and set it to MPMovieRepeatModeOne
//Determines how the movie player repeats the playback of the movie.
#property(nonatomic) MPMovieRepeatMode repeatMode
//Discussion The default value of this property is MPMovieRepeatModeNone.
// For a list of available repeat modes, see “MPMovieRepeatMode.”
// Availability Available in iOS 3.2 and later.Declared In MPMoviePlayerController.h
NSString *tempurl = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Movie.m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:tempurl]];
player.view.frame = CGRectMake(0, 0, 867, 1008);
player.scalingMode = MPMovieScalingModeFill;
player.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:player.view];
[player play];

Adjust AVPlayer Frame Rate During Playback [duplicate]

I'm wondering if it's possible to change the video playback speed in an iphone application. we want users to yell in the microphone to speed up the playback and get to the end.
You have to use setCurrentPlaybackRate:
There is a rate property for the AVPlayer.
If you take the example from Apple called "avPlayerDemo" in the resource section, you then simply have to set the mplayer.rate. It worked for me, I created a new slider in the xib files, implemented that slider in the AVPlayerDemoPlaybackViewController and simply set mPlayer.rate to the slider value.
What about the MPMoviePlayerController?
setCurrentPlaybackRate
Here's some code which does not work at that spot
-(IBAction)abspielen:(id)sender
{
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:titleOfButton ofType:#"mov"];
NSURL *movieURL = [ NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *themovie = [[MPMoviePlayerController alloc]initWithContentURL: movieURL];
[themovie play];
[themovie setCurrentPlaybackRate:2.f];
[themovie release];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer release];
}

Audio only with MPMoviePlayerController on iO4

When I play a video doing this:
NSString *videoFilepath = [[NSBundle mainBundle] pathForResource:#"bacon" ofType:#"mov"];
NSURL *videoURL = [NSURL fileURLWithPath:videoFilepath];
MPMoviePlayerController *movie;
movie = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[movie play];
on 3.2, it works pefeectly. But if I switch the base SDK to 4.0 i only hear the sound of the movie.
Any ideas?
I believe that adding this with your play command should improve matters for you:
[movie play]
[movie setFullscreen:YES];
[self.view addSubview:movie.view];
Try testing this property as it sounds like the movie is playing but has not been set to fullscreen:
http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html#//apple_ref/occ/instp/MPMoviePlayerController/fullscreen
This line of code is also of importance:
[self presentMoviePlayerViewControllerAnimated:movie];
try,
//add frame works
// AVFoundation.framework
// MediaPlayer.framework
[movie setFullscreen:YES];
[self.view addSubview:movie.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
moviePlayer.repeatMode = YES;

problem using MPMovieController in iPhone SDK 4.0

I was using MPMoviePlayer to play a short video in my app with no problems in SDK 3.1.3. I made the changes to the code in SDK 4 but the video is not playing. I just get a black screen and audio. The Apple Dev Center doesnt have any sample code for this class for the latest SDK. Following is the code I'm using:
- (void)viewDidLoad {
[super viewDidLoad];
//videoPlayer is a MPMoviePlayerController object defined in the header file of the view controller
if (videoPlayer == nil){
NSString * videoPath = [[NSBundle mainBundle] pathForResource:#"myvideo" ofType:#"mp4"];
if (videoPath == NULL){
return;
}
NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
videoPlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:#"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[videoPlayer play];
[videoPlayer setFullscreen:YES];
[self.view addSubview:videoPlayer.view];
}
}
The above results in just the audio being played with a black screen. The notification is called correctly at the end of the playback.
When the above did not work, then I even tried using the new MPMoviePlayerViewController class as follows:
- (void)viewDidLoad {
[super viewDidLoad];
NSString * videoPath = [[NSBundle mainBundle] pathForResource:#"myvideo" ofType:#"mp4"];
if (videoPath == NULL){
return;
}
NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
//movieController is an MPMoviePlayerViewController object defined in the header file of view controller
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:#"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[movieController.moviePlayer setFullscreen:YES];
[movieController.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated:movieController];
}
Same problem persists - I can hear the audio and the notification at the end of the playback is called as expected. However I just see a black screen instead of the video.
There is nothing wrong in the encoding of the video because the same video plays fine in iTunes as well as on my iPod Touch in the regular videos playlist.
Could anyone help me out with this problem?
Thanks in advance
problem solved - for the benefit of those who are stuck on a similar issue, the solution is to explicitly create the frame for the view of the MPMoviePlayerController as follows:
I changed the lines:
[videoPlayer play];
[videoPlayer setFullscreen:YES];
[self.view addSubview:videoPlayer.view];
to the following:
[videoPlayer prepareToPlay];
[videoPlayer play];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0.0, 0.0, 480.0, 320.0); //this is explicitly added and solves the problem

How to play video locally in objective-c for iphone?

I want to play the video on iphone locally by storing the video in the app.
how can i do?
NSString *path = [[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];
To store video in the app, you can just add it with a copy files phase in the build. For an example of how to play a movie, check out the Media Player docs and especially the MoviePlayer sample code.
Try the following code:
-(IBAction)Videoplay_btn:(id)sender
{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[moviplayer prepareToPlay];
[moviplayer.view setFrame: self.view.bounds];
[self.view addSubview: moviplayer.view];
moviplayer.fullscreen = YES;
moviplayer.shouldAutoplay = YES;
moviplayer.repeatMode = MPMovieRepeatModeNone;
moviplayer.movieSourceType = MPMovieSourceTypeFile;
[moviplayer play];
}
FYI
The MPMoviePlayerController class is formally deprecated in iOS 9.
(The MPMoviePlayerViewController class is also formally deprecated.)
To play video content in iOS 9 and later, instead use the
AVPictureInPictureController or AVPlayerViewController class from
the AVKit framework, or the WKWebView class from WebKit.