I have been browsing through Google for various explanations but I STILL couldn't figure out when this code fires the screen is pitch black. Anyone able to spot a mistake?
UPDATE
- (IBAction)playVideo:(id)sender {
NSURL *videoUrl = [[DataStore singletonInstance] getVideoUrl:self withUuid:self.eventDetailVC.event.uuid];
if ([videoUrl checkResourceIsReachableAndReturnError:nil] == NO) {
NSLog(#"Video doesn't not exist.");
return;
}
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[previewView addSubview:player.view];
player.view.frame = previewView.bounds;
player.controlStyle = MPMovieControlStyleDefault;
[player play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
NSLog(#"moviePlayBackDidFinish: called");
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
// Checking for errors
NSDictionary *notiUserInfo = [notification userInfo];
if (notiUserInfo != nil) {
NSError *errorInfo = [notiUserInfo objectForKey:#"error"];
if ([[errorInfo domain] isEqualToString:#"MediaPlayerErrorDomain"]) {
UIAlertView *notice = [[UIAlertView alloc] initWithTitle:#"Error"
message:[errorInfo localizedDescription]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[notice show];
return;
}
}
// Remove from view
[player.view removeFromSuperview];
[player stop];
}
FYI moviePlayBackDidFinish is NOT called at all. I don't know why.
Create property for MPMoviePlayerController, because you retain view after adding it as subview, but not retain controller.
#property (strong, nonatomic) MPMoviePlayerController *player;
...
#synthesize player = _player;
...
- (IBAction)playVideo:(id)sender
{
NSURL *videoUrl = [[DataStore singletonInstance] getVideoUrl:self withUuid:self.eventDetailVC.event.uuid];
if ([videoUrl checkResourceIsReachableAndReturnError:nil] == NO)
{
NSLog(#"Video doesn't not exist.");
return;
}
self.player = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[previewView addSubview:_player.view];
_player.view.frame = previewView.bounds;
_player.controlStyle = MPMovieControlStyleDefault;
[_player play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
NSLog(#"moviePlayBackDidFinish: called");
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Checking for errors
NSDictionary *notiUserInfo = [notification userInfo];
if (notiUserInfo != nil)
{
NSError *errorInfo = [notiUserInfo objectForKey:#"error"];
if ([[errorInfo domain] isEqualToString:#"MediaPlayerErrorDomain"])
{
UIAlertView *notice = [[UIAlertView alloc] initWithTitle:#"Error"
message:[errorInfo localizedDescription]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[notice show];
return;
}
}
// Remove from view
[_player.view removeFromSuperview];
[_player stop];
self.player = nil;
}
Related
The problem video can be seen in :
http://app.bowerchat.com/images/118_1435046739658.mp4
MPMoviePlayerViewController Dismissing immediately without play.
What can be reason?Some videos are loaded nicely and playing.
How can i check the video can be played or not?
I tried
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
Results always 0 - 0
MY Code to play video
self.mpController = [[MPMoviePlayerViewController alloc] initWithContentURL:moveUrl];
[self.mpController.moviePlayer prepareToPlay];
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
[parent presentMoviePlayerViewControllerAnimated:self.mpController];
The reason could be that movie that has extention *.mp4 is actually of different type.
Also try to add the following:
self.mpController.moviePlayer.shouldAutoplay = YES;
[self.mpController.moviePlayer play];
Try this snippet :
-(void)setupMovie
{
NSURL *movieURL = [NSURL fileURLWithPath:movieURL];
self.mpController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self. mpController.scalingMode = MPMovieScalingModeAspectFill;
self. mpController.controlStyle = MPMovieControlStyleNone;
self.mpController.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:nil];
[self.view addSubview:self.mpController.view];
[self.mpController prepareToPlay];
}
- (void)moviePlayBackDidFinish:(MPMoviePlayerController *)player
{
[self.mpController.view removeFromSuperview];
}
- (void)moviePlayerPlaybackStateDidChange:(NSNotification*)notification
{
if (self. mpController.isPreparedToPlay) {
[self. mpController play];
}
}
And make sure movieURL is correct and video exist by this provided url
Im trying to play a local video using AVFoundation. I don't know whats going on here that only if I play a video on the web and then play the local video it works. If I just play the local video it gives me the error:
Item cannot be played
he assets tracks were loaded, but could not be made playable.
And Here's my code:
- (void)setURL:(NSURL*)URL
{
if (mURL != URL)
{
[mURL release];
mURL = [URL copy];
NSLog(#"Im here %#",mURL);
}
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:mURL options:nil];
NSArray *requestedKeys = [NSArray arrayWithObjects:kTracksKey, kPlayableKey, nil];
/* Tells the asset to load the values of any of the specified keys that are not already loaded. */
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:
^{
dispatch_async( dispatch_get_main_queue(),
^{
/* IMPORTANT: Must dispatch to main queue in order to operate on the AVPlayer and AVPlayerItem. */
[self prepareToPlayAsset:asset withKeys:requestedKeys];
});
}];
}
- (void)prepareToPlayAsset:(AVURLAsset *)asset withKeys:(NSArray *)requestedKeys
{
if (!asset.playable)
{
NSString *localizedDescription = NSLocalizedString(#"Item cannot be played", #"Item cannot be played description");
NSString *localizedFailureReason = NSLocalizedString(#"The assets tracks were loaded, but could not be made playable.", #"Item cannot be played failure reason");
NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:
localizedDescription, NSLocalizedDescriptionKey,
localizedFailureReason, NSLocalizedFailureReasonErrorKey,
nil];
NSError *assetCannotBePlayedError = [NSError errorWithDomain:#"StitchedStreamPlayer" code:0 userInfo:errorDict];
[self assetFailedToPrepareForPlayback:assetCannotBePlayedError];
return;
}
if (self.mPlayerItem)
{
[self.mPlayerItem removeObserver:self forKeyPath:kStatusKey];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.mPlayerItem];
}
self.mPlayerItem = [AVPlayerItem playerItemWithAsset:asset];
[self.mPlayerItem addObserver:self
forKeyPath:kStatusKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.mPlayerItem];
seekToZeroBeforePlay = NO;
if (!self.mPlayer)
{
[self setPlayer:[AVPlayer playerWithPlayerItem:self.mPlayerItem]];
[self.player addObserver:self
forKeyPath:kCurrentItemKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext];
[self.player addObserver:self
forKeyPath:kRateKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AVPlayerDemoPlaybackViewControllerRateObservationContext];
}
if (self.player.currentItem != self.mPlayerItem)
{
[self.mPlayer replaceCurrentItemWithPlayerItem:self.mPlayerItem];
[self syncPlayPauseButtons];
}
[self.mScrubber setValue:0.0];
[self play:nil];
loading.hidden = YES;
}
NSLog gives the local URL that im using:
Im here file:///var/mobile/Containers/Data/Application/E806EC2A-2C5D-4B86-AD05-D9FD29E8FDDD/Documents/downloads/VVV%20-%20One%20Direction,%20Band%20Aid%2030,%20Mark%20Ronson,%20Bruno%20Mars,%20OneRepublic,%20DSCVR%20Ones%20To%20Watch%202015.m4v
Thanks in Advance!!
//You can use MPMoviePlayerController for Playing a Video.
Add MediaPlayer.framework (#import )
MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc] init];
movieController.controlStyle = MPMovieControlStyleDefault;//MPMovieControlStyleNone;
[movieController setContentURL:#"Your local Url Path"];
[movieController.view setFrame:CGRectMake (0,0,214,186)];
[self.view addSubview:movieController.view];
[movieController.view bringSubviewToFront:self.view];
[movieController prepareToPlay];
[movieController play];
I have a view within my view controller and have it declared as a property _movieSubview. I add a MPMoviePlayerViewController to _movieSubview and the video plays nicely.
-(void)moviePlay{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"High Rope" ofType:#"mov"]];
_playerController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
//[self presentMoviePlayerViewControllerAnimated:_playerController];
_playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[_playerController.moviePlayer prepareToPlay];
_playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
_playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_playerController.moviePlayer play];
/*[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_playerController];*/
//---play partial screen---
_playerController.view.frame = CGRectMake(0, 0, 320, 460);
[_movieSubview addSubview:_playerController.view];
}
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == _playerController) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[_playerController.moviePlayer play];
}
}
}
My 2 questions are:
My status bar disappears when I add the MPMoviePlayerViewController inside my subview. How do I keep the status bar?
How can I have the video play twice on repeat, then wait for user to play after that?
You should present _playerController instead of adding as subview. Use this code while creating the instance of MPMoviePlayer:
[moviePlayer setFullscreen:YES animated:YES];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:#selector(moviePlayerEvent:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
[notificationCenter addObserver:self selector:#selector(playBackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
-(void)moviePlayerEvent:(NSNotification*)aNotification{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
}
-(void)playBackStateChanged:(id)sender{
MPMoviePlaybackState playbackState = [moviePlayerViewController.moviePlayer playbackState];
switch (playbackState) {
case MPMoviePlaybackStateStopped :
//play again
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
break;
case MPMoviePlaybackStatePlaying :
break;
case MPMoviePlaybackStateInterrupted :
break;
}
}
I am using the following code to play video files in MPMoviePlayerController
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"one" ofType:#"mp4"];
NSURL* url = [NSURL fileURLWithPath:filePath];
_movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
[_movie.view setFrame:self.view.bounds];
[self.view addSubview:_movie.view];
_movie.fullscreen=YES;
_movie.controlStyle=MPMovieControlStyleFullscreen;
[_movie prepareToPlay];
[_movie play];
and
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(close:)name:MPMoviePlayerPlaybackDidFinishNotification object:_movie];
and
- (void) close:(NSNotification *)notification {
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if(reason == MPMoviePlaybackStateStopped) {
NSLog(#"Stop");
}
else if (reason == MPMovieFinishReasonPlaybackEnded) {
NSLog(#"Playback Ended ");
}
else if (reason == MPMovieFinishReasonUserExited) {
NSLog(#"Exited");
[_movie.view removeFromSuperview];
}
else if (reason == MPMovieFinishReasonPlaybackError) {
//error
NSLog(#"Error");
}
}
I am able to get the Notification , and the Movieplayer is not removing from the superview.
What could be the problem ??
Try this follow instructions:
when I listen to MPMoviePlayerWillExitFullscreenNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:_movie];
And selector method:
-(void)doneButtonClick:(NSNotification*)aNotification{
[_movie.view removeFromSuperview];
}
(or)
Better way to use mpmovieplayerviewcontroller in below tutorial
http://mobiledevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html
I want to play a video by the URL. I see some sample,the codes like below:
NSString *movieFile= [[NSBundle mainBundle] pathForResource:#"android" ofType:#"mp4"];
videoURL=[[NSURL alloc] initFileURLWithPath:movieFile];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
It play only the local resource.I write some code like:
NSString* strurl =#"https://s3.amazonaws.com/adplayer/colgate.mp4";
videoURL=[NSURL fileURLWithPath:strurl];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
but there is nothing ... why..and how to play video by the url ?
In following code, I am playing a video over the internet from a movie file located on a web server. Dont forget to add MediaPlayer framework and include "MediaPlayer/MediaPlayer.h" in ViewController.h file.
On a button click use following code:
-(IBAction) playVideo:(id)sender
{
NSURL *url=[[NSURL alloc] initWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
Notification method:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
Use the following code:
- (IBAction)playBtnPressed {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
//moviePlayer.shouldAutoplay=NO;
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void)moviePlayBackDonePressed:(NSNotification *)notification {
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
moviePlayer = nil;
}
- (void)moviePlayBackDidFinish:(NSNotification *)notification {
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
}
Use following line in .h file and add MediaPlayer Framework in your project
import
Try
NSURL *url = [NSURL URLWithString: strurl];