Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Video in 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.

like image 991
taimur Avatar asked Nov 26 '25 12:11

taimur


2 Answers

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;
}
like image 71
user3182143 Avatar answered Nov 28 '25 01:11

user3182143


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];
}
like image 33
Moin Shirazi Avatar answered Nov 28 '25 01:11

Moin Shirazi