Search code examples
iosobjective-ciphoneavplayer

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.


Solution

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