I have a UIView which contains an AVQueuePlayer to show a videos. UIViewAnimationOptionTransitionCrossDissolve not working in this avqueue player
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url1 = [NSURL URLWithString:#"http:fff.mov"];
first = [AVPlayerItem playerItemWithURL: url1];
NSURL *url2 = [NSURL URLWithString:#"http:hgh.mov"];
second = [AVPlayerItem playerItemWithURL: url2];
NSURL *url3 = [NSURL URLWithString:#"http:jhk.mov"];
third = [AVPlayerItem playerItemWithURL: url3];
NSURL *url4 = [NSURL URLWithString:#"http:dsdfd.mov"];
fourth = [AVPlayerItem playerItemWithURL: url4];
player = [AVQueuePlayer queuePlayerWithItems:[NSArrayarrayWithObjects:first,second,third,fourth,nil]];
lists = [NSMutableArray arrayWithObjects:first,second,third,fourth, nil];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
_displayview = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_displayview];
[player play];
for (int m;m<lists.count;m++)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:lists[m]];
}
}
-(void)itemDidFinishPlaying:(NSNotification *) notification
{
End=YES;
//
NSLog(#"view will Appear");
[UIView transitionWithView:_displayview
duration:3.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void)
{
NSLog(#"IT REACHED THE END");
}
completion:nil];
}
If anyone can help, I'd really appreciate any advice.
Thanks guys.
Related
I need to write an application where there are different short video loops in the background of a View. These loops should typically have a length of 10-30 seconds. I was wondering what would be better in terms of performance and memory economics (the loops should be bundled with the app when downloaded from the appstore)
a) Use a UIWebview as Background where an animated .gif of the video is looped
b) Use some VideoPlayerView (AVPlayer) as Background with an m4v-like file
c) Some alternative that I haven't considered yet
There will also be audio, however audio is not linked to the video
- (void)playIntroVideo
{
// NSBundle *bundle = [NSBundle mainBundle];
// // NSString *moviePath = iPadDevice ? [bundle pathForResource:#"IntroVideo" ofType:#"mp4"] : [bundle pathForResource:#"IntroVideo_iPhone" ofType:#"mp4"];
// NSString *moviePath = [bundle pathForResource:#"IntroVideo" ofType:#"mp4"];
// NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
CGRect frame = self.movieView.frame;
frame.origin.x=-200;
frame.size.width+=200;
AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:avAsset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *selectedImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
UIImageView *img = [[UIImageView alloc] initWithImage:selectedImage];
img.frame = frame;
[self.movieView addSubview:img];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResize];
if (iPhoneDevice)
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[avPlayerLayer setFrame:frame];
[self.movieView.layer addSublayer:avPlayerLayer];
[self.avplayer seekToTime:kCMTimeZero];
// Not affecting background music playing
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
// [self.avplayer setVolume:0.0f];
[self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self playIntroVideo];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.avplayer play];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.avplayer pause];
// self.avplayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avplayer currentItem]];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification
{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
I'm using AVPlayer to play videos. I have an issue on fullscreen mode. Video player comes from top left corner. It looks odd.
NSString *filePath = [self.video_Array objectAtIndex:index];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: nil];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
currentItem = [AVPlayerItem playerItemWithURL:url];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.videoGravity = AVLayerVideoGravityResize;
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = NO;
_video=[AVPlayer playerWithURL:url];
_video = [AVPlayer playerWithPlayerItem:currentItem];
_playerViewController.player = _video;
[_playerViewController.player play];
[self.view addSubview:_playerViewController.view];
Please refer my code which is working fine in my application,
-(void)videoPlayer:(NSString *)filePath{
playerViewController = [[AVPlayerViewController alloc] init];
self.viewPlayerContainer.frame = CGRectMake(0, 74, self.view.bounds.size.width, self.view.bounds.size.width);
NSURL *url = [NSURL fileURLWithPath:filePath];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;
[playerViewController.view setFrame:CGRectMake(0, 0, self.viewPlayerContainer.bounds.size.width, self.viewPlayerContainer.bounds.size.height)];
[playerViewController addObserver:self forKeyPath:#"videoBounds" options:NSKeyValueObservingOptionNew context:nil];
playerViewController.showsPlaybackControls = YES;
[self.viewPlayerContainer addSubview:playerViewController.view];
[player play];
}
You can use AVPlayerViewController to play fullscreen video
AVPlayerViewController+Fullscreen.h
#import <AVKit/AVKit.h>
#interface AVPlayerViewController (Fullscreen)
-(void)goFullscreen;
#end
AVPlayerViewController+Fullscreen.m
#import "AVPlayerViewController+Fullscreen.h"
#implementation AVPlayerViewController (Fullscreen)
-(void)goFullscreen {
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:self animated:YES completion:nil];
}
#end
I created avplayer like this but controlls are not showing.can any one explain is there any playback controls are we need to create our custom playbacks.
NSString *path = [[NSBundle mainBundle] pathForResource:#"sample2" ofType:#"mp4"];
_videoURL = [NSURL fileURLWithPath: path];
_videoAsset = [AVURLAsset assetWithURL:_videoURL];
_videoDuration = (int)CMTimeGetSeconds(_videoAsset.duration);
_playerItem = [AVPlayerItem playerItemWithAsset:_videoAsset];
_player = [AVPlayer playerWithPlayerItem:_playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = CGRectMake(0, self.frame.size.height*0.2, self.frame.size.width, self.frame.size.height*0.3);
[self.layer addSublayer:_playerLayer];
[_player play];
_isPlaying = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerItem];
I try to add a video to Viewcontroller but always fails : some one can help?
-(id) init
{
// always call "super" init // Apple recommends to re-assign "self" with the "super's" return value
if(self=[super init])
{
//play // [[[CCDirector sharedDirector] view] addSubview:viewController.view];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[viewController.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieDone:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; [moviePlayerController play];
//play
}
return self;
}
In ViewController.h
MPMoviePlayerController *moviePlayerController;
In ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"try" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 10, 320,300)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
[moviePlayerController play];
}
I am trying play a video on device. Video is this
video
and my code:
NSString *urlStr = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 400);
[moviePlayer play];
When i start app an movie screen is seen but movie doesnt play. Can you help me where is the mistake ?
Try use
NSURL *url = [NSURL URLWithPath:urlStr];
instead of
NSURL *url = [NSURL fileURLWithPath:urlStr];
It is a web url not a file url.
Check this video for reference. This works for me -
- (IBAction)playMovie:(id)sender
{
NSString *filepath = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}