视频播放

来源:互联网 发布:影视播放源码 编辑:程序博客网 时间:2024/05/01 22:05

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

#import <MediaPlayer/MediaPlayer.h>//播放

@interface ViewController ()

- (IBAction)btnclick:(id)sender;

@property (nonatomic,strong) MPMoviePlayerViewController *playVc;

@property (nonatomic,strong) MPMoviePlayerController *player;

- (IBAction)btnNoViewClick:(id)sender;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    NSString *path = [[NSBundlemainBundle]pathForResource:@"Alizee_La_Isla_Bonita.mp4"ofType:nil];

    

    NSURL *url = [NSURLfileURLWithPath:path];

    //无界面

    MPMoviePlayerController *player = [[MPMoviePlayerControlleralloc]initWithContentURL:url];

    self.player = player;

    

    // 第三方或者下载demo 封装 自定义控制按钮

    player.controlStyle =MPMovieControlStyleNone;

    [self.viewaddSubview:player.view];

    

    player.view.translatesAutoresizingMaskIntoConstraints =NO;

    self.view.translatesAutoresizingMaskIntoConstraints =NO;

    

    //自动布局约束

    NSArray *consH = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-0-[playerView]-0-|"options:0metrics:nilviews:@{@"playerView" : player.view}];

    NSArray *consV = [NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-70-[playerView]-0-|"options:0metrics:nilviews:@{@"playerView" : player.view}];

    [self.viewaddConstraints:consH];

    [self.viewaddConstraints:consV];



}

- (void)test

{

    //有界面的

    NSString *path = [[NSBundlemainBundle]pathForResource:@"Alizee_La_Isla_Bonita.mp4"ofType:nil];

    

    NSURL *url = [NSURLfileURLWithPath:path];

    

    MPMoviePlayerViewController *playVc = [[MPMoviePlayerViewControlleralloc]initWithContentURL:url];

    self.playVc = playVc;

    //有界面  展示界面

}

- (IBAction)btnclick:(id)sender {

    

    [selfpresentViewController:self.playVcanimated:YEScompletion:nil];

    

    

}

- (IBAction)btnNoViewClick:(id)sender {

    


    

    

    [self.playerprepareToPlay];

    

    [self.playerplay];

    

    //[self.player pause];

    

}

@end

=============================================================================
画中画效果

#import "ViewController.h"

#import <AVKit/AVKit.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVPlayerViewControllerDelegate>

- (IBAction)playClick:(id)sender;

@property (nonatomic,strong) AVPlayerViewController *playerVc;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    NSString *path = [[NSBundlemainBundle]pathForResource:@"Alizee_La_Isla_Bonita.mp4"ofType:nil];

    

    NSURL *url = [NSURLfileURLWithPath:path];

    AVPlayerViewController *playerVc = [[AVPlayerViewControlleralloc]init];

    self.playerVc = playerVc;

    playerVc.player = [[AVPlayeralloc]initWithURL:url];

    

    playerVc.delegate = self;

    //画中画

    //1.开启  2.属性支持画中画  3.iPad air2之后的新的

    playerVc.allowsPictureInPicturePlayback =YES;


    

}

- (IBAction)playClick:(id)sender {

    [selfpresentViewController:self.playerVcanimated:YEScompletion:nil];

    //[self.playerVc.player play];

}


/*!

@method playerViewControllerWillStartPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture will start.

 */

- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerWillStartPictureInPicture");

}


/*!

@method playerViewControllerDidStartPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture did start.

 */

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerDidStartPictureInPicture");

}


/*!

@method playerViewController:failedToStartPictureInPictureWithError:

@param playerViewController

 The player view controller.

@param error

 An error describing why it failed.

@abstract Delegate can implement this method to be notified when Picture in Picture failed to start.

 */

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;{

    NSLog(@"failedToStartPictureInPictureWithError");

}


/*!

@method playerViewControllerWillStopPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture will stop.

 */

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerWillStopPictureInPicture");

}


/*!

@method playerViewControllerDidStopPictureInPicture:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method to be notified when Picture in Picture did stop.

 */

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;{

    NSLog(@"playerViewControllerDidStopPictureInPicture");

}


/*!

@method playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:

@param playerViewController

 The player view controller.

@abstract Delegate can implement this method and return NO to prevent player view controller from automatically being dismissed when Picture in Picture starts.

 */

- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController;{

    return NO;

}


/*!

@method playerViewController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:

@param playerViewController

 The player view controller.

@param completionHandler

 The completion handler the delegate needs to call after restore.

@abstract Delegate can implement this method to restore the user interface before Picture in Picture stops.

 */

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;{

    completionHandler(YES);

}


@end



0 0
原创粉丝点击