MoviePlayer
首先,添加三个系统库AVFoundation.framework、MediaToolbox.framework、MediaPlayer.framework
#import "MainViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@interface MainViewController ()
{
MPMoviePlayerViewController *_pvc;
MPMoviePlayerController *_mpc;
AVAudioPlayer *_player; //播放
AVAudioRecorder *_recorder; //录音
}
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"mp4"];
_pvc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
[_pvc.moviePlayer setShouldAutoplay:YES];
//[_pvc.moviePlayer play];
//_mpc = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 100, 40);
[btn setTitle:@"button" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(100, 140, 100, 40);
[btn2 setTitle:@"点击录音" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(startRecord:) forControlEvents:UIControlEventTouchDown];
[btn2 addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(100, 180, 100, 40);
[btn3 setTitle:@"播放" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
//录音
//录音设置
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];
_recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:path] settings:recordSetting error:nil];
//准备录音
[_recorder prepareToRecord];
// Do any additional setup after loading the view.
}
//开始录音
-(void)startRecord:(UIButton *)btn
{
[_recorder record];
[btn setTitle:@"录音中" forState:UIControlStateNormal];
}
//停止录音
-(void)stop:(UIButton *)btn
{
[_recorder stop];
[btn setTitle:@"点击录音" forState:UIControlStateNormal];
}
//播放
-(void)play
{
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];
NSLog(@"%@",path);
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[_player prepareToPlay];
[_player play];
}
-(void)btnClick
{
[self presentMoviePlayerViewControllerAnimated:_pvc];
[_pvc.moviePlayer play];
// [_mpc play];
}