afplayer not run video in 4 inches devices - ios

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;

Related

Add AVPlayerLayer to UIViewController

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 to mute audio using MPMoviePlayerController?

MPMoviePlayerController * _moviePlayerController = [[MPMoviePlayerController alloc] init];
_moviePlayerController.controlStyle = MPMovieControlStyleDefault;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"]];
_moviePlayerController.contentURL = url;
_moviePlayerController.fullscreen=NO;
_moviePlayerController.backgroundView.backgroundColor = [UIColor whiteColor];
for(UIView *aSubView in _moviePlayerController.view.subviews) {
aSubView.backgroundColor = [UIColor whiteColor];
}
// Rotating the player to landscape position
_moviePlayerController.view.frame = CGRectMake(0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height);
// _moviePlayerController.view.transform = CGAffineTransformMakeRotation(M_PI_2);
_moviePlayerController.view.center = self.view.center;
UIView *playerView = _moviePlayerController.view;
playerView.frame = CGRectMake(0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height);
[videoPlayerView addSubview:playerView];
_moviePlayerController.shouldAutoplay=NO;
[_moviePlayerController prepareToPlay];
[_moviePlayerController pause];
Its not possible to mute your audio player by using MPMoviePlayerController. If you want to mute your audio player you should add AVFoundation framework. Try this code
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:[self localMovieURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
// Mute all the audio tracks
NSMutableArray * allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:0.0 atTime:kCMTimeZero ];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix * audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item
// Create a new Player, and set the player to use the player item
// with the muted audio mix
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
self.mPlayer = player;
[mPlayer play];

Run multiple video file in objective c

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];
}

MPMoviePlayerController stops playing the video after 5s

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.

AVPlayer doesn't show anything

I try to embed different videos from youtube vimeo, dailymotion.
Sadly at the Moment nothing is shown except the backgroundcolor of my containerView:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, 320.0f, 200.0f)];
//item.url is my url which i get fro my webserver, it looks like http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:item.url]];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
NSLog(#"%#",playerItem);
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = self.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self addSubview:containerView];
[avPlayer play];
if (avPlayer.status == AVPlayerStatusReadyToPlay) {
//[playingLbl setText:#"Playing Audio"];
NSLog(#"It works");
} else if (avPlayer.status == AVPlayerStatusFailed) {
// something went wrong. player.error should contain some information
NSLog(#"Not works");
NSLog(#"%#",avPlayer.error);
}
else if (avPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(#"AVPlayer Unknown");
}
containerView.backgroundColor = [UIColor blueColor];
NSLog(#"error: %#", avPlayer.error);
NSLog(#"AVPlayer: %#", avPlayer);
AVPlayer Error is Null and the only Log i always get from the Status is: AVPlayerItemStatusUnknown. Any ideas?
EDIT 1:
Ich changed my Code to:
#implementation VideoView
BlockVideo *list;
- (id)initWithBlock:(GFBlock *)block {
self = [super initWithBlock:block];
if (self) {
if (block.values && block.values.count) {
list = (GFBlockVideo *) [block.values objectAtIndex:0];
for (int i=0; i<list.videos.count; ++i) {
GFBlockVideoItem *item = list.videos[i];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, 320.0f, 200.0f)];
//Like i said item.url = http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player
//#property (nonatomic, strong) NSString* url;
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:item.url]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = containerView.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self addSubview:containerView];
[avPlayer play];
containerView.backgroundColor = [UIColor blueColor];
Sadly the only thing i can see is the blue containerView :/
I think the Problem is not the AVPlayer himself, but the frames and the layer maybe....
You'd have to do the following:
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:item.url]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = self.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self addSubview:containerView];
[avPlayer play];
Hope this helps.
Addendum:
It also depends on your URL; if as you said, you have one such as in this format:
http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player
Then you should use this instead:
[NSURL urlWithString:item.url];
Given that item is your object and url is a property there of object type NSString.
AVPlayer Implementation which working for me:
MP4:
player = [AVPlayer playerWithURL:videoPathUrl];
AVcontroller = [[AVPlayerViewController alloc] init];
[AVcontroller.view setFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.width)];
AVcontroller.player = player;
AVcontroller.showsPlaybackControls = FALSE;
[self addChildViewController:AVcontroller];
[self.view addSubview:AVcontroller.view];
[player play];
MP3 :
playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
player = [AVPlayer playerWithURL:url];
[player play];
For getting thumbnail form video try this
AVAsset *asset = [AVAsset assetWithURL:videoPathUrl];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
float tempTime = CMTimeGetSeconds(player.currentItem.duration);
CMTime time = CMTimeMake(tempTime, 1); // (1,1)
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *aThumbnail = [UIImage imageWithCGImage:imageRef];
For Stop Video
[player pause];
player = nil;
**Setting Play rate/Speed for video **
[player play];
[player setRate:currentRate];
Play From start
[player seekToTime:kCMTimeZero];
[player play];
for checking video is playing or not
if ((player.rate != 0) && (player.error == nil)) {
// playing
}
For seeking video (for some duration ahead)
float tempSeekTime = CMTimeGetSeconds(player.currentItem.duration) + 10;
CMTime targetTime = CMTimeMakeWithSeconds(tempSeekTime, NSEC_PER_SEC);
[player seekToTime:targetTime];
Use the requestPlayerItemForVideo method of PHImageManager to acquire an AVPlayerItem; it is the simplest, sure-fire way to play an AVAsset, performing flawlessly and consistently.
I use it here:
https://youtu.be/7QlaO7WxjGg

Resources