iOS视频——视频文件、播放视频

视频文件

视频文件格式很多:

1、AVI文件,是将音频与视频同步组合在一起的文件格式。有损压缩方式,压缩比较高,画面质量不是太好。

2、WMV文件,同等质量下,体积非常小,适合网上传输与播放。视频传输有延迟。必须在Windows上。

3、RMVB文件,最大特点是在保证文件清晰度的同时具有体积小巧的特点。

4、3GP文件,新的移动设备标准格式,体积小,移动性强,在PC上兼容性差。

5、MOV文件,苹果开发的一种音频、视频文件格式,用于存储常用数字媒体类型。包含很多轨道,画面效果比AVI要稍微好一些。

6、MP4文件。

7、M4V文件,苹果公司创造,为iOS设备所使用。

播放视频

有很多种方法,主要使用MediaPlayer框架中的MPMoviePlayerController或MPMoviePlayerViewController

使用AVFoundation框架中的AVPlayer和AVQueuePlayer。

使用MediaPlayer框架

一种高层次API,控制不是特别的好。

下面通过一个实例,展示该如何用:

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()

@property (nonatomic, strong) MPMoviePlayerViewController *moviePlayerView;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

- (IBAction)useMPMoviePlayerController:(id)sender;
- (IBAction)useMPMoviePlayerViewController:(id)sender;

-(NSURL *)p_movieURL;
-(void)p_playbackFinished4MoviePlayerController:(NSNotification *)notification;
-(void)p_doneButtonClick:(NSNotification *)notification;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(NSURL *)p_movieURL
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"YY" ofType:@"mp4"];
    if (moviePath)
    {
        return [NSURL fileURLWithPath:moviePath];
    }
    else
    {
        return nil;
    }
}

- (IBAction)useMPMoviePlayerViewController:(id)sender
{
    if (_moviePlayerView == nil)
    {
        _moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[self p_movieURL]];
        _moviePlayerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
        _moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_playbackFinished4MoviePlayerController:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    }
    [self presentMoviePlayerViewControllerAnimated:_moviePlayerView];
}

- (IBAction)useMPMoviePlayerController:(id)sender
{
    if (_moviePlayer == nil)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(p_playbackFinished4MoviePlayerController:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(p_doneButtonClick:)
                                                     name:MPMoviePlayerWillExitFullscreenNotification
                                                   object:nil];
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self p_movieURL]];
        _moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
        _moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
        [self.view addSubview:_moviePlayer.view];
    }
    [_moviePlayer play];
    [_moviePlayer setFullscreen:YES animated:YES];
}

-(void)p_playbackFinished4MoviePlayerController:(NSNotification *)notification
{
    NSLog(@"使用MPMoviePlayerController播放完成。");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_moviePlayer stop];
    [_moviePlayer.view removeFromSuperview];
    _moviePlayer = nil;
}

-(void)p_doneButtonClick:(NSNotification *)notification
{
    NSLog(@"退出全屏");
    if (_moviePlayer.playbackState == MPMoviePlaybackStateStopped)
    {
        [_moviePlayer.view removeFromSuperview];
        _moviePlayer = nil;
    }
}

@end

使用AVFoundation框架

提供更加细粒度的控制。可以添加自己设计的播放控制控件、同时播放多个视频、在动画中夹杂一些视频片段。

一个实例:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

{
    id timeObserver;
    BOOL isPlaying;
}

@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;

@property (nonatomic, weak) AVPlayer *avPlayer;
@property (nonatomic, weak) AVPlayerLayer *layer;
@property (nonatomic, strong) AVPlayerItem *playerItem;

- (IBAction)play:(id)sender;
- (IBAction)seek:(id)sender;
-(void)p_addObserver;
-(void)p_playerItemDidReachEnd:(NSNotification *)notification;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"YY" ofType:@"mp4"];
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];
    self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
    _avPlayer = [AVPlayer playerWithPlayerItem:_playerItem];
    _layer = [AVPlayerLayer playerLayerWithPlayer:_avPlayer];
    float scale = 1.776;
    _layer.frame = CGRectMake(0, -290, self.view.frame.size.width * scale, self.view.frame.size.height *scale);
    
    [self.view.layer insertSublayer:_layer atIndex:0];
    double duration = CMTimeGetSeconds(asset.duration);
    
    _slider.maximumValue = duration;
    _slider.minimumValue = 0;
    
    isPlaying = NO;
}

- (IBAction)seek:(id)sender
{
    float value = _slider.value;
    [_avPlayer seekToTime:CMTimeMakeWithSeconds(value, 10)];
}

- (IBAction)play:(id)sender
{
    UIBarButtonItem *item1;
    if (!isPlaying)
    {
        [self p_addObserver];
        [_avPlayer seekToTime:kCMTimeZero];
        [_avPlayer play];
        
        isPlaying = YES;
        item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(play:)];
    }
    else
    {
        isPlaying = NO;
        item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(play:)];
        [_avPlayer pause];
    }
    NSMutableArray *items = [[NSMutableArray alloc] initWithArray:[_toolBar items]];
    [items replaceObjectAtIndex:0 withObject:item1];
    [_toolBar setItems:items];
}

-(void)p_addObserver
{
    if (timeObserver == nil)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(p_playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:_playerItem];
        timeObserver = [_avPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 10)
                                                               queue:dispatch_get_main_queue()
                                                          usingBlock:^(CMTime time)
        {
            float duration = CMTimeGetSeconds(self->_avPlayer.currentTime);
            self->_slider.value = duration;
        }];
    }
}

-(void)p_playerItemDidReachEnd:(NSNotification *)notification
{
    NSLog(@"播放完成");
    if (timeObserver)
    {
        [_avPlayer removeTimeObserver:timeObserver];
        timeObserver = nil;
        _slider.value = 0;
        isPlaying = NO;
        [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(play:)];
        NSMutableArray *items = [NSMutableArray arrayWithArray:[_toolBar items]];
        [items replaceObjectAtIndex:0 withObject:item1];
        [_toolBar setItems:items];
    }
}

@end

猜你喜欢

转载自blog.csdn.net/run_in_road/article/details/113569390