iPhone开发之音视频的播放——MPMoviePlayerViewController和AVAutoPlayer的使用

新建工程,添加音视频框架 

编辑控制器的.h文件如下:

<span style="font-size:18px;">//
//  ViewController.h
//  音视频预习
//
//  Created by apple on 15/9/4.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController

@property (nonatomic, strong) AVAudioPlayer * player;

@end
</span>

编辑控制器的.m文件如下:

<span style="font-size:18px;">//
//  ViewController.m
//  音视频预习
//
//  Created by apple on 15/9/4.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    self.view.backgroundColor = [UIColor redColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, 200, 213, 40);
    [btn setTitle:@"播放MV" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void) play
{
    // 播放本地文件视频
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"];
//    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
//    MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//    [self  presentMoviePlayerViewControllerAnimated:playVC ];
//    
//    playVC.view.frame = CGRectMake(100, 100, 200, 300);
//    [self.view addSubview:playVC.view];
    
    // 播放网路视频文件
//    NSURL *url= [NSURL URLWithString:@"http://10.50.129.30/mp4files/52070000013B61EC/www.dcjyxwzx.cn/data//news/video//2015/06/20150609134637_60491.mp4"];
//    MPMoviePlayerViewController  *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//    playVC.view.frame = CGRectMake(0, 100, 320, 200);
//    [playVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
//    playVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
//    [self.view addSubview:playVC.view];
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFull) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
//    [self presentMoviePlayerViewControllerAnimated:playVC];
    
    //播放音频文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"一江水" ofType:@"mp3"];
    if (path) {
        NSLog(@"LLLL");
        NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [self.player prepareToPlay];
        self.player.volume = 1.0f;
        self.player.numberOfLoops = 0;
        if (self.player) {
            if (![self.player isPlaying]) {
                [self.player play];
            }
        }
        
    }
}

-(void) changeFull
{
    NSLog(@"+++++++++");

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
</span>

1、播放器类的介绍
 (1)IOS内置了视频播放器,我们可以通过使用MPMoviePlayerController或者MPMoviePlayerViewController类(视图控制器)来播放视频(含流媒体视频播放)。
(2)需要引入MediaPlayer.framewrok 库
(3)MPMoviePlayerController播放器可以任意修改播放页面的尺寸。
(4)MPMoviePlayerViewController类是一个特殊的视图控制器类,它包含了一个播放器(MPMoviePlayerController)。也能播放MP3。功能不够强大。
(5)适用于非专业的播放器。
MPMoviePlayerController使用
// 初始化url
NSURL  *url = [NSURL URLWithString:@“http://,,,,,,,”;
// 初始化视频播放
MPMoviePlayerController *moviePlayer = [[MPMoviePlayer alloc] initWithContentURL: url];
// 将视频播放视图加入
[self.view  addSubview: moviePlayer.view];
moviePlayer.view.frame = self.view.bounds;
// 开始播放
[moviePlayer play];
2、比较常用的几个通知
// 视频播放结束的通知
MPMoviePlayerPlayBackDidFinishNotification
//视频播放状态改变的通知
MPMoviePlayerPlayBackStateDidChangeNotification
//视频加载状态改变的通知
MPMoviePlayerPlayerLoadStateDidChangeNotification
发送通知的方法:在视频所在的控制器的
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  方法中获取通知并发送消息
具体代码如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidChangeNotification:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    }
    return self;
}

- (void)playDidChangeNotification:(NSNotification *)notification {
    MPMoviePlayerController *moviePlayer = notification.object;
    MPMoviePlaybackState playState = moviePlayer.playbackState;
    if (playState == MPMoviePlaybackStateStopped) {
        NSLog(@"停止");
    } else if(playState == MPMoviePlaybackStatePlaying) {
        NSLog(@"播放");
    } else if(playState == MPMoviePlaybackStatePaused) {
        NSLog(@"暂停");
    }

}


控制横竖屏的方法如下:

在控制器中添加如下方法

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}


//6.0之前用于控制横、竖
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        return YES;
    }
    return NO;
}












你可能感兴趣的:(iPhone开发之音视频的播放——MPMoviePlayerViewController和AVAutoPlayer的使用)