iOS音視頻MPMoviePlayerController Delegate Notification

ZYXMoviePlayerController.h

#import <UIKit/UIKit.h>

@protocol ZYXMoviePlayerControllerDelegate <NSObject>
- (void)moviePlayerDidFinished;
- (void)moviePlayerDidCapturedWithImage:(UIImage *)image;
@end


@interface ZYXMoviePlayerController : UIViewController
@property (nonatomic, weak) id<ZYXMoviePlayerControllerDelegate> delegate;
@property (nonatomic, strong) NSURL *movieURL;
@end

ZYXMoviePlayerController.m

#import "ZYXMoviePlayerController.h"

#import <MediaPlayer/MediaPlayer.h>

@interface ZYXMoviePlayerController ()
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end

@implementation ZYXMoviePlayerController

- (MPMoviePlayerController *)moviePlayer
{
    if (!_moviePlayer) {
        // 負責控制媒體播放的控制器
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
       
        //_moviePlayer.view.frame = self.view.bounds;
        //_moviePlayer.fullscreen = YES;
        
        _moviePlayer.view.frame = CGRectMake(10, 80, self.view.frame.size.width-20, 200);
        
        _moviePlayer.controlStyle = NO;
        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor purpleColor];
    [self.moviePlayer play];
    [self addNotification];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.moviePlayer play];
}

#pragma mark - 添加通知
- (void)addNotification
{
    // 1. 添加播放狀態的監聽
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(stateChanged) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    // 2. 播放完成
    [nc addObserver:self selector:@selector(finished) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    // 3. 全屏
    [nc addObserver:self selector:@selector(enterFullScreen) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
    [nc addObserver:self selector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    // 4. 截屏完成通知
    [nc addObserver:self selector:@selector(captureFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
    
    // 數組中有多少時間,就通知幾次
    // MPMovieTimeOptionExact 精確
    // MPMovieTimeOptionNearestKeyFrame 大概
    [self.moviePlayer requestThumbnailImagesAtTimes:@[@(25.0), @(40.0)] timeOption:MPMovieTimeOptionNearestKeyFrame];
}

- (void)captureFinished:(NSNotification *)notification{
    NSLog(@"captureFinished notification = %@", notification);
    if (self.delegate && [self.delegate respondsToSelector:@selector(moviePlayerDidCapturedWithImage:)]) {
        [self.delegate moviePlayerDidCapturedWithImage:notification.userInfo[MPMoviePlayerThumbnailImageKey]];
    }
}

- (void)finished{
    // 1. 刪除通知監聽
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // 2. 返回上級窗體
    // 誰申請,誰釋放!
    if (self.delegate && [self.delegate respondsToSelector:@selector(moviePlayerDidFinished)]) {
       [self.delegate moviePlayerDidFinished];
    }
    NSLog(@"完成");
}

- (void)enterFullScreen{
    self.moviePlayer.fullscreen = YES;
    _moviePlayer.view.frame = self.view.bounds;
}

- (void)exitFullScreen{
    self.moviePlayer.fullscreen = NO;
    _moviePlayer.view.frame = CGRectMake(10, 80, self.view.frame.size.width-20, 200);
}

/**
 MPMoviePlaybackStateStopped,           停止
 MPMoviePlaybackStatePlaying,           播放
 MPMoviePlaybackStatePaused,            暫停
 MPMoviePlaybackStateInterrupted,       中斷
 MPMoviePlaybackStateSeekingForward,    下一個
 MPMoviePlaybackStateSeekingBackward    前一個
 */
- (void)stateChanged{
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暫停");
            break;
        case MPMoviePlaybackStateStopped:
            // 執行[self.moviePlayer stop]或者前進后退不工作時會觸發
            NSLog(@"停止");
            break;
        default:
            break;
    }
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [self.moviePlayer stop];
    self.moviePlayer = nil;
}

@end

/*
 播放
 captureFinished notification = NSConcreteNotification 0x7b6d06f0 {name = MPMoviePlayerThumbnailImageRequestDidFinishNotification; object = <MPMoviePlayerController: 0x7b9674b0>; userInfo = {
    MPMoviePlayerThumbnailImageKey = "<UIImage: 0x7b6d0a40>, {1024, 576}";
    MPMoviePlayerThumbnailTimeKey = "24.3";
}}
 captureFinished notification = NSConcreteNotification 0x7b7e1920 {name = MPMoviePlayerThumbnailImageRequestDidFinishNotification; object = <MPMoviePlayerController: 0x7b9674b0>; userInfo = {
    MPMoviePlayerThumbnailImageKey = "<UIImage: 0x7b74c8c0>, {1024, 576}";
    MPMoviePlayerThumbnailTimeKey = "29.1";
}}
 暫停
 完成
*/

ViewController.m

#import "ViewController.h"

#import "ZYXMoviePlayerController.h"

@interface ViewController () <ZYXMoviePlayerControllerDelegate>
@property (nonatomic, strong) ZYXMoviePlayerController *moviePlayer;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

- (ZYXMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
        _moviePlayer = [[ZYXMoviePlayerController alloc] init];
        _moviePlayer.delegate = self;
        
        NSURL *url = nil;
        //url = [[NSBundle mainBundle] URLForResource:@"promo_full.mp4" withExtension:nil];
        url = [NSURL URLWithString:@"http://localhost/videos/promo_full.mp4"];
        
        _moviePlayer.movieURL = url;
    }
    return _moviePlayer;
}

- (void)moviePlayerDidFinished{
    // 誰申請,誰釋放
    // dismissViewControllerAnimated將當前視圖控制器的模態窗口關閉
    //[self dismissViewControllerAnimated:YES completion:nil];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)moviePlayerDidCapturedWithImage:(UIImage *)image{
    self.imageView.image = image;
}

- (IBAction)click{
    //[self presentViewController:self.playerController animated:YES completion:nil];
    [self.navigationController pushViewController:self.moviePlayer animated:YES];
}

@end
MPMoviePlayerController.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容