2

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.

Mamun
  • 66,969
  • 9
  • 47
  • 59
taimur
  • 54
  • 1
  • 8

3 Answers3

9

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;
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
3

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.

  1. Add Mediaplayer framework and do #import in your viewController.

  2. Drag and drop your video file you created on desktop into the xcode.

  3. Get the path of the local video.

    NSStringthePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"]; NSURLtheurl=[NSURL fileURLWithPath:thePath];

  4. 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];

  5. 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];
}
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
  • its not necessary to use MPMoviePlayerController for play video . you can also play video in avplayer – Himanshu Moradiya Feb 09 '17 at 06:51
  • Without MPMoviePlayer also answer is mentioned you can check. Its an alternative – Moin Shirazi Feb 09 '17 at 06:53
  • My Video url is : file:///var/mobile/Containers/Data/Application/B3B93026-5CA4-4119-A206-06BF06A0DD75/Documents/08-03-201711:38:25.mp4 – taimur Feb 09 '17 at 06:53
  • only get blank screen – taimur Feb 09 '17 at 06:54
  • I think you are using direct this link and this link change always whenever you built the app. So don't use directly instead take the link from local document directory runtime – Moin Shirazi Feb 09 '17 at 06:56
  • i investigate the video in my mac, video is present but it is not playing in AVPlayer and MPMoviePlayer – taimur Feb 09 '17 at 06:57
  • @MoinShirazi yes i m taking link from local directory and append video name with that link. NSArray *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docPath = [documentsDirectory objectAtIndex:0]; NSString *videoPath = [docPath stringByAppendingPathComponent:_nameVideo]; _output = [NSURL fileURLWithPath:videoPath]; this is my code of getting link. – taimur Feb 09 '17 at 07:00
  • But as you said your video is in mac so it won't get with document directory link. So please take direct link of the mac, means add video to your project and give name of video direct and then check is video playing or not. – Moin Shirazi Feb 09 '17 at 07:03
  • i mean i download container files of my project in device to my mac, followed the link and find video there. – taimur Feb 09 '17 at 07:08
1

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!

KTang
  • 340
  • 1
  • 17