Play Video in iOS - ios

I am trying to play a video which is stored on my device trying both AVPlayer and MPMoviePlayerController.
Video URl is: file:///var/mobile/Containers/Data/Application/73695F3E-9351-447B-BB8A-0B4A62CE430F/Documents/08-03-201711:48:50.3gp. Now the problem is I get blank screen.
AVPlayer *player = [AVPlayer playerWithURL:fileURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[player pause];
[player play];
playerViewController.player = player;
[self addChildViewController: playerViewController];
[self.view addSubview: playerViewController.view];
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
I think the problem is in link. What I am doing is getting local directory link and append name with that link.

I will you good solution.It works perfectly.
ViewController.h
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVPlayerViewController *playerViewController;
- (IBAction)actionPlayVideo:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController (){
NSURL *vedioURL;
}
#end
#implementation ViewController
#synthesize playerViewController;
- (IBAction)actionPlayVideo:(id)sender{
NSString *fullpath = [[self documentsDirectory] stringByAppendingPathComponent:#"yourdate.3gp"];
vedioURL =[NSURL fileURLWithPath:fullpath];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:vedioURL];
AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = playVideo;
playerViewController.player.volume = 0;
playerViewController.view.frame = self.view.bounds;
[self.view addSubview:playerViewController.view];
[playVideo play];
}
-(NSString *)documentsDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}

EDIT: MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
You can use MPMoviePlayerController to play local file.
Add Mediaplayer framework and do #import in your viewController.
Drag and drop your video file you created on desktop into the xcode.
Get the path of the local video.
NSStringthePath=[[NSBundle mainBundle] pathForResource:#"yourVideo" ofType:#"MOV"];
NSURLtheurl=[NSURL fileURLWithPath:thePath];
Initialize the moviePlayer with your path.
self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[self.moviePlayer.view setFrame:CGRectMake(40, 197, 240, 160)];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:self.moviePlayer.view];
To control what is to be done after playback:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//playBackFinished will be your own method.
EDIT 2: To handle completion for AVPlayerViewController rather than MPMoviePlayerController, use the following...
AVPlayerItem *playerItem = player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
In this example, I dismiss the AVPlayerViewController after completion:
-(void)playBackFinished:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
[playerViewController dismissViewControllerAnimated:false completion:nil];
}

It may happen because of your file url is incorrect.
Try below codes to get the path of your answer.
If you have the video in your mac, then copy it in to your project firstly, just drug it to Xcode.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"08-03-201711:21:67" ofType:#"3gp"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
And use this path to AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:fileURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[player pause];
[player play];
playerViewController.player = player;
[self addChildViewController: playerViewController];
[self.view addSubview: playerViewController.view];
**As I review your code, a blank screen may caused by you adding AVPlayer it into layer but not as subview/childviewcontroller. So try my code!

Related

AVPlayer not showing FullScreen

I am working on playing a recorded video recorded by AVCapture.I am saving the video URL in string named outputFileURL. I tried playing back the video using AVPlayerLayer concept. The code I used is
AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
But the video I am getting is not full screen.
Can anyone can help me to solve?
I added the following code and I am able to get the full screen.
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
Hope this may help.
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; playerViewController.delegate = self;
playerViewController.player = player;
[playerViewController.player play];
[self presentViewController:playerViewController animated:YES completion:nil];
Try This
Ok than you have to use MPMoviePlayerController
NSString *path = [[NSBundle mainBundle] pathForResource:#"Video_Intro" ofType:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
moviePlayer.view.frame = ivVideoThumbnail.frame;
//moviePlayer.view.top += 20; // Add to fix 20 pixel diff of moviePlayer view
// Register this class as an observer instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[svIntro addSubview:moviePlayer.view];
moviePlayer.fullscreen = true;
[moviePlayer play];
Also implement Observer method
-(void)movieFinishedCallback:(id)mPlayer
{
[moviePlayer.view removeFromSuperview];
}

Avplayer not playing video in ios 8.0

I have implemented avplayer programatically.
#interface showAskUsVideo ()
{
AVURLAsset *asset;
AVPlayerItem *item;
}
#property (strong, nonatomic) AVPlayer *player;
-(void)viewWillAppear:(BOOL)animated
{
[self playVideo];
}
-(void)playVideo {
[playerViewController.view setFrame:CGRectMake(37, 80 ,495,220)];
asset = [AVURLAsset assetWithURL: url];
item = [AVPlayerItem playerItemWithAsset: asset];
_player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.showsPlaybackControls = NO;
playerViewController.player = _player;
[item addObserver:self forKeyPath:#"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
[_player addObserver:self forKeyPath:#"rate" options:0 context:nil];
[self.view addSubview:playerViewController.view];
}
But when I select deployment target 10.0 then its working fine. But for iOS 8.0 to 9.3 only PlaybackControls is seen on screen not video.
What need to change in my code?
Is playerViewController part of the view controller hierarchy? If not, that could explain it. Try adding it as a child view controller of your view controller:
[self addChildViewController:playerViewController];
[self.view addSubview:playerViewController.view];
Try like that: Add to .m
#property (nonatomic, retain)AVPlayer *player;
#property (weak, nonatomic) IBOutlet AVPlayerClass *playerView;
(playerView is your view where you want to show it)
NSURL *url = [[NSBundle mainBundle]URLForResource:#"video_name" withExtension:#"mp4"];
_player = [AVPlayer playerWithURL:url];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = _playerView.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_playerView.layer insertSublayer:playerLayer atIndex:0];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
[_player play];
-(void)playerItemDidReachEnd:(NSNotification *)notification{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
If you using a ViewController for your player. The best practice is presentingViewController
[self presentViewController:playerViewController animated:YES completion:nil];
if you using xib
PlayerViewController *vc = [[PlayerViewController alloc]
initWithNibName:#"PlayerViewController" bundle:nil];
[self.view addSubview:[vc view]];

Video not play in Objective c

I want to play video on clicking a button. My AVPlayerViewController is open but not playing an video .Please help.
- (IBAction)btnPlay_Click:(id)sender {
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
If you are using vimeo video you can use third party tools which helps you load the vimeo video.
Solution 1: Please try this, It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Solution 2: YTVimeoExtractor
May be it will help you.
Use MPMoviePlayerController and add this line of code:
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self.view addSubview: [player view]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer prepareToPlay];
Add one more line : [self.view addSubview: [player view]];
movieController is an instance of MPMoviePlayerViewController declared in the .h file.
[movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.
--------- If this is not working you can just do that following ----------
player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];

Video closes after playing

Is it possible to make the video not close after it has finished playing? So this would allow the user to press "done" when the video is finished and then the video will close.
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface player1: UIViewController {
}
-(IBAction) playMovie;
#end
ViewController.m
#import "ViewController.h"
#implementation player1
-(IBAction) playMovie {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"2.DragtoopenfileinaVM" ofType:#"mp4"]];
MPMoviePlayerViewController *windowsmac = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:windowsmac];
windowsmac.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[windowsmac.moviePlayer play];
windowsmac = nil;
}
This is the coding I'm using, could someone help?
moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[[NSNotificationCenter defaultCenter] removeObserver:moviePlayer
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer.moviePlayer];
[self presentViewController:moviePlayer animated:YES completion:nil];

MPMoviePlayerViewController -- how to eliminate black flash when video loads?

I'm using MPMoviePlayerViewController to show a video in my app. It works! Only problem is that there's a black flash just before the movie plays.
How can I get rid of the black flash? I've seen other threads, but they don't seem to have an explanation that works with MPMoviePlayerViewController and is sufficiently specific/detailed for a novice like me (most are for MPMoviePlayerController).
Would really appreciate any help!
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpvc.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mpvc.moviePlayer setContentURL:fileURL];
[mpvc.moviePlayer play];
[self presentViewController:mpvc animated:NO completion:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mpvc.moviePlayer];
After endlessly iterating and tweaking, I stumbled my way into a solution using MPMoviePlayerController.
Don't forget to declare the property in the .h file, e.g.
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
Then
// Add default image to smooth transition
UIImage *myImage = [UIImage imageNamed:#"aiw_launch1136_black.png"];
self.videoStartFrame.image = myImage;
// Play the intro video
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"]]];
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
[self.moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayer.view];
self.moviePlayer.view.hidden = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(isMovieReady:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer];
// Detect that the video is ready and unhide the view
-(void)isMovieReady:(NSNotification *)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))
{
self.moviePlayer.view.hidden = NO;
}
}

Resources