Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

IOS多媒体之视频播放与录音的简易实现

$
0
0

涉及到的框架


MediaPlayer.framework
AVFoundation.framework
CoreAudio.framework

demo的预览图



简介


提供了指定的区域播放视频以及全屏播放视频的功能, 录音及播放录音的功能。主要用到了MPMovieController,AVAudioRecorder,AVAudioPlayer类。

.h


#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>

@interface ViewController : UIViewController
{
    MPMoviePlayerController *_moviePlayer;
    AVAudioPlayer *_audioPlayer;
    AVAudioRecorder *_audioRecorder;
}

@property (weak, nonatomic) IBOutlet UIView *movieRegion;
@property (weak, nonatomic) IBOutlet UISwitch *toggleFullscreen;
@property (weak, nonatomic) IBOutlet UIButton *buttonForRecord;

- (IBAction)playMovie:(id)sender;
- (IBAction)recordButton:(id)sender;
- (IBAction)playRecord:(id)sender;

@end

.m


初始化

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    NSURL *soundURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"sound.caf"]];
    NSDictionary *soundSetting = @{AVSampleRateKey: @44100.0F,
                                   AVFormatIDKey: @(kAudioFormatMPEG4AAC),
                                   AVNumberOfChannelsKey: @2,
                                   AVEncoderAudioQualityKey:@(AVAudioQualityHigh)};  //设置各种参数 如采集音频频率,格式,声道以及品质
    
    _audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundURL settings:soundSetting error:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"m4v"];
    
    NSURL *movieURL = [NSURL fileURLWithPath:movieFile];
    
    _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    _moviePlayer.allowsAirPlay = YES;	//允许AirPlay
    _moviePlayer.view.frame = self.movieRegion.frame;
}

为什么在ViewDidAppear中初始化?
因为在IOS6以后的autoLayout系统在viewDidLoad方法中还未返回用作占位符的视图movieRegion的frame值,只有等到viewDidAppear才能知道该视图的显示位置。

视频的播放

- (IBAction)playMovie:(id)sender {
    [self.view addSubview:_moviePlayer.view];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playFinished:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:_moviePlayer];
    
    if (self.toggleFullscreen.isOn) {
        [_moviePlayer setFullscreen:YES animated:YES];//如果UISwitch为ON 则为全屏播放
    }
    
    [_moviePlayer play];
}

- (void)playFinished:(NSNotification *)theNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:_moviePlayer];	//删除观察者 观察者不会被ARC自动回收
    [_moviePlayer.view removeFromSuperview];
}

录音及播放


- (IBAction)recordButton:(id)sender {
    if ([self.buttonForRecord.titleLabel.text isEqualToString:@"录音"]) {
        [_audioRecorder record];
        [self.buttonForRecord setTitle:@"停止" forState:UIControlStateNormal];	
    } else {
        [_audioRecorder stop];
        [self.buttonForRecord setTitle:@"录音" forState:UIControlStateNormal];
        
        NSURL *soundURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"sound.caf"]];
        
        _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
    }
}

- (IBAction)playRecord:(id)sender {
    [_audioPlayer play];
}

[NSURL initFileURLWithPath:]: nil string parameter


有时候你把资源,比如这个视频拷入XCode中还是会没有加入到Target,所以运行就会报这个错误。解决方法是在Build Phases里手动添加比较好。




作者:ran0809 发表于2013-7-21 0:34:59 原文链接
阅读:131 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles