I tried to play a video in iOS but it just played 5s for any video. This is my code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"videoviewdemo" ofType:#"mp4"];
NSURL *movieURL = [[NSURL alloc]initFileURLWithPath:moviePath];
MPMoviePlayerController *theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.controlStyle = MPMovieControlStyleDefault;
[theMoviPlayer setMovieSourceType:MPMovieSourceTypeFile];
theMoviPlayer.shouldAutoplay = YES;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
[theMoviPlayer.view setFrame:self.view.frame];
[theMoviPlayer prepareToPlay];
[self.view addSubview:theMoviPlayer.view];
Declare an ivar #property (nonatomic,strong) MPMoviePlayerController *myMovieController;
assign theMoviPlayer to it as follows,
self.myMovieController = theMoviPlayer;
[self.view addSubview: self.myMovieController.view];
and then play the movie.
The issue seems to be that theMoviPlayer is not retained.
Related
Is it possible to display AVPlayerLayer inside a UIViewController which is later on displayed in UIView.
The sample code of what I'm trying to do:
...
UIView *parentView = reinterpret_cast<UIView *>(window->winId());
AVPlayer *_player;
AVURLAsset *_asset;
AVPlayerItem *_playerItem;
AVPlayerLayer *m_playerLayer;
_player = [[AVPlayer alloc] init];
NSURL *baseURL = [[NSURL alloc] initWithString: #"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
_asset = [AVURLAsset assetWithURL:baseURL];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
[_player replaceCurrentItemWithPlayerItem:_playerItem];
m_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
// works with AVPlayerViewController
// AVPlayerViewController *playerController = [[AVPlayerViewController alloc] init];
// playerController.player = _player;
UIViewController *viewController = [[UIViewController alloc] init];
[viewController.view.layer addSublayer:m_playerLayer];
viewController.view.frame = CGRectMake(this->x(), this->y(), this->width(), this->height());
[parentView addSubview:viewController.view];
[_player play];
...
It works if I try to add the player to AVPlayerViewController and then display it, but I would want to use only AVPlayerLayer since I don't need video controls as I'll implement the functionality separately.
You can use AVPlayerViewController with
avController.showsPlaybackControls = false;
Or simply add the layer
_player = [[AVPlayer alloc] init];
NSURL *baseURL = [[NSURL alloc] initWithString: #"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
_asset = [AVURLAsset assetWithURL:baseURL];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
[_player replaceCurrentItemWithPlayerItem:_playerItem];
m_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
m_playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:m_playerLayer ];
[_player play];
I subclassed UIView and added on the AVPlayerLayer via SH_Khans method
_player = [[AVPlayer alloc] init];
NSURL *baseURL = [[NSURL alloc] initWithString: #"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
_asset = [AVURLAsset assetWithURL:baseURL];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
[_player replaceCurrentItemWithPlayerItem:_playerItem];
m_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
m_playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:m_playerLayer ];
[_player play];
Then treat the subclassed UIView how you want. Couple of things though, if you resize the AVPlayerLayerUIView you have to resize the AVPlayerLayer as the system does not do that automatically.
Additionally remember to clip to bounds on the AVPlayerLayerUIView or it will not match your frame as you want.
How do I place a label over or "on top of" an AVPlayer playing a video? I have tried adjusting Z-position as suggested in another SO post but it is not working, the video player seems to be in the front of all the labels.
//this is my label
self.commentLabel.layer.masksToBounds = YES;
self.commentLabel.layer.cornerRadius = 15.0;
[self.commentLabel sizeToFit];
self.commentLabel.center = CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/1.4);
self.commentLabel.layer.zPosition=100;
//this is my video player
self.playerViewController = [[AVPlayerViewController alloc] init];
NSString*str3=[self.FRIENDDATA stringByReplacingOccurrencesOfString:#" " withString:#"_"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://my.website.com/%#/%#.mp4", str3,self.RANDOMDATA]];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
self.playerViewController.player = player;
[self.playerViewController.view setFrame:self.referenceImageView.frame];
self.playerViewController.view.layer.zPosition=0;
self.playerViewController.showsPlaybackControls = YES;
[self.view addSubview:self.playerViewController.view];
Have you tried adding the label subview onto the playerViewController ?
[self.playerViewController.view addSubview:self.commentLabel];
From the code I am unsure if commentLabel is not yet initialized or if it initialized somewhere else in the code or from a storyboard. It would be great to initialize the commentLabel on the playerViewController
Like this:
UILabel *commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/1.4, 100, 100)];
[self.playerViewController.view addSubview:commentLabel];
That should resolve your issue
In my app I use, I loop a video over and over again. It will always show the QuickTime logo at the beginning of the video each time. I have checked, and there isn't anything like that in the video. Thoughts to how I can get rid of that?
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"warpspeed" withExtension:#"mov"];
UIView *patternView = [[UIView alloc] initWithFrame:self.view.bounds];
patternView.backgroundColor = [UIColor blackColor];
[self.moviePlayer2.backgroundView addSubview:patternView];
self.moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayer2 setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer2.controlStyle = MPMovieControlStyleNone;
self.moviePlayer2.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayer2 setAllowsAirPlay:YES];
self.moviePlayer2.view.frame = self.view.frame;
I have tried to play mp4 video in a background view.. but this code working fine and giving result in simulators and iPhone 6, and 6+
but when I am running this to iPhone 5 or related these screen devices.. video not playing or running..
I am using this code:
NSString *filepath1 = [[NSBundle mainBundle] pathForResource:#"login" ofType:#"mp4"];
NSURL *fileURL1 = [NSURL fileURLWithPath:filepath1];
vwVideoBg.backgroundColor = [UIColor blackColor];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:filepath1]];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
NSLog(#"%#",filepath1);
avPlayerLayer.frame = self.view.frame;
[vwVideoBg.layer addSublayer:avPlayerLayer];
//[self.view addSubview:vwVideoBg];
[self.avPlayer play];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
There are two video file.When I want to see these two video
1) Only the last video file can play,see in fullscreen mode and also minimise when done button clicked..but first video file can't .
2) After a few time the first video file also see in black screen
- (void)viewDidLoad
{
[super viewDidLoad];
MPMoviePlayerController *moviePlayer;
NSArray *filename=#[#"nissan1",#"nissan5"];
//n![enter image description here][1]issan1,nissan5 are mp4 file
NSURL *fileURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:0] ofType:#"mp4"]];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:fileURL1];
[moviePlayer.view setFrame:CGRectMake(5, 50, 100,100)];
moviePlayer.shouldAutoplay = NO;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.initialPlaybackTime = -1.0;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
/* second video file*/
NSURL *fileURL2 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:1] ofType:#"mp4"]];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:fileURL2];
[moviePlayer.view setFrame:CGRectMake(200, 50, 100,100)];
moviePlayer.shouldAutoplay = NO;
moviePlayer.repeatMode = MPMovieRepeatModeNone;
moviePlayer.initialPlaybackTime = -1.0;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
}
You should try with two instances. As Cristik pointed out, ARC targets it for deallocation once the method ends and you are using same variable.
Since you asked for sample code, try this:
- (void)viewDidLoad
{
[super viewDidLoad];
MPMoviePlayerController *moviePlayer1;
MPMoviePlayerController *moviePlayer2;
//filename is a name array
NSURL *fileURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:0] ofType:#"mp4"]];
moviePlayer1= [[MPMoviePlayerController alloc] initWithContentURL:fileURL1];
[moviePlayer1.view setFrame:CGRectMake(5, 50, 100,100)];
moviePlayer1.shouldAutoplay = NO;
moviePlayer1.repeatMode = MPMovieRepeatModeOne;
moviePlayer1.initialPlaybackTime = -1.0;
moviePlayer1.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer1 prepareToPlay];
[self.view addSubview:moviePlayer1.view];
/* second video file*/
NSURL *fileURL2 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:1] ofType:#"mp4"]];
moviePlayer2= [[MPMoviePlayerController alloc] initWithContentURL:fileURL2];
[moviePlayer2.view setFrame:CGRectMake(200, 50, 100,100)];
moviePlayer2.shouldAutoplay = NO;
moviePlayer2.repeatMode = MPMovieRepeatModeNone;
moviePlayer2.initialPlaybackTime = -1.0;
moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer2 prepareToPlay];
[self.view addSubview:moviePlayer2.view];
}