i want to play a video in a frame of a view, but the following code does not work.
can you help me?
MPMediaItem *song = [allVideos objectAtIndex:indexPath.row];
NSLog(#"%#",[song valueForProperty:MPMediaItemPropertyAssetURL]);
NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayer *player = [[AVPlayer alloc] init];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
//[player seekToTime:kCMTimeZero];
[player play];
You need to use AVPlayerLayer, add this
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
[self.view.layer addSublayer:playerLayer];
to your code.
Have you try to use MPMoviePlayerController as like below
MPMoviePlayerController * _player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
CGRect frame = self.frame;
frame.origin = CGPointZero;
_player.view.frame = frame;
[self.view addSubview:_player.view];
[_player prepareToPlay];
[_player play];
Try this one ..
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.frame = CGRectMake(0,45, 1024, 723); // take your require frame size of player
[player.view setBackgroundColor:[UIColor clearColor]];
player.repeatMode=MPMovieRepeatModeNone;
[self.view addSubview:player.view];
[player play];
Related
- (void)playVideoInteractionNarration:(NSString *)vNarr{
[self pauseActionExecution];
NSURL *u = [NSURL fileURLWithPath:vNarr];
AVQueuePlayer *player = [[AVQueuePlayer alloc] initWithURL:u];
self.controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.showsPlaybackControls=NO;
[controller.player setActionAtItemEnd:AVPlayerActionAtItemEndAdvance];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(videoNarrationFinishedPlaying:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:player.currentItem];
//NSLog(#"MPVIDEOOOOOOO %#", player);
[controller.player setVolume:1.0];
[controller.player setMuted:NO];
[controller setViewInCurrentController];
[controller.player play];
}
- (void)videoNarrationFinishedPlaying:(NSNotification *) aNotification{
AVPlayerItem * item = [aNotification object];
[self.controller removeViewInCurrentController];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
[self.controller.player pause];
[self.controller release];
[self playActionExecution];
}
the video start and finish correctly but NO sound
why ?
the right answer is this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
#import AVKit;
#import AVFoundation;
#property (nonatomic) AVPlayer *avPlayer;
NSURL *fileURL = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];
[self.avPlayer play];
Try this code and comment your one and check that in your device did sound work or not ?
I have tried the following code but this is not working. only a black screen is coming out as output.
NSURL *url_vdo=[[NSURL alloc] initWithString:#"url"];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url_vdo];
[moviePlayer.view setFrame: self.view.bounds];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle=MPMovieControlStyleFullscreen;
[moviePlayer play];
[moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
NSURL *movieURL = [NSURL URLWithString:#"http://url.m3u8"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
mp.view.frame = self.view.bounds;
[self.view addSubview:mp.view];
// save the movie player object
[mp setFullscreen:YES];
// Play the movie!
[mp play];
self.moviePlayer = mp;
}
Try to change this line:
[moviePlayer.view setFrame: self.view.bounds];
With this line:
[moviePlayer.view setFrame: self.view.frame];
I am trying to replace AVPlayer with MPMoviePlayerController as I want to be able to add an animation with transparent background to a view. The problem is the animation is not displaying with MPMoviePlayerController.
Please find my lines below. The first portion is with AVPlayer and it's work. The second is with MPMoviePlayerController and doesn't.
What the code does is it plays an animation and when this is done it is launching an action.
Code with AVPlayer (that works):
NSString *filepath = [[NSBundle mainBundle] pathForResource:theAnimationFileName ofType:theString;
//NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Pass the AVPlayerItem to a new player
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];
[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];
// Begin playback
[controlledPlayer play];
Code with MPMoviePlayerController (does not display anything):
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:theString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
moviePlayer.controlStyle = MPMovieControlModeDefault;
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 1024);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[thisReplacementView addSubview:moviePlayer.view];
[moviePlayer play];
NSURL *url = [NSURL URLWithString:#"http://iOS.mp4"];
self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayer];
-(void)playbackFinishedCallback:(NSNotification *)notification{
self.moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[self.moviePlayer.view removeFromSuperview];
}
This is the code i am trying to run to show a movie on the ipad.
What happens is i just see loading... on the bottom of the screen.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
mpviewController.view.userInteractionEnabled = YES;
[self.view addSubview:mpviewController.view];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
mp.controlStyle = MPMovieControlStyleDefault;
[mp prepareToPlay];
[mp play];
I am able to use iOS MediaPlayer and play a movie by this way. But I need, seeking a sec of a movie. How can I do this, I play a movie by MediaPlayer like that:
NSURL *videoURL = [NSURL fileURLWithPath:self.filename];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
Thanks in advance..
Sorry for own answer,
NSURL *videoURL = [NSURL fileURLWithPath:self.filename];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
moviePlayer.initialPlaybackTime = 5;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
this worked for me in order to seek a time in a movie...