ios中多媒体开发

在ios应用中,我们经常会碰到一些基于ios多媒体的开发使用。
本篇文章中简单介绍一下三个知识点:

1.图片的采集和显示
2.音频的录制和播放
3.视频的录制和播放

首先,我们来看一张框架图,基本了解一下在ios中关于多媒体开发的底层框架的概念。


ios中多媒体开发_第1张图片
多媒体.png

一、图片的采集和显示
1.布置UI部分(UIButton 和 UIImageView)
2.导入关于图片操作的框架

#import 
#import 
#import 

3.创建UIImagePickerController对象,声明代理

-(UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        // 采集源类型
        _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 媒体类型 #import 

        _imagePicker.mediaTypes = [NSArray arrayWithObject:(__bridge NSString*)(kUTTypeImage)];
//        _imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh; //视频质量
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

4.根据数据来源,设置图片采集来源,进行图片采集

- (IBAction)selectImage:(id)sender {
    // 通过摄像头采集
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else // 通过图片库来采集
    {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    }
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

5.图片的显示,实现UIImagePickerDelegate的代理方法,完成图片的选择显示和取消回调

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 获取媒体类型
    NSString * type =info[UIImagePickerControllerMediaType];
    // 如果媒体类型是图片类型
    if ([type isEqualToString:(__bridge NSString*)(kUTTypeImage)]) {
        UIImage * image = info[UIImagePickerControllerOriginalImage];
        self.imageView.image = image;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
// 取消采集图片的处理
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{

    [self dismissViewControllerAnimated:YES completion:nil];
}

二、音频的录制和播放
开发之前的注意事项:
如果是播放本地音频文件的情况需要保证音频文件处在target编译目录下,图示:


ios中多媒体开发_第2张图片
bug.png

如果音频文件没有在编译目录下,音频播放时会找不到文件路径,程序崩溃,出现以下错误日志

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter’

1.1.布置UI部分(UIButton)
2.导入关于图片操作的框架(同上)

#import 
#import 
#import 

3.创建AVAudioRecorder(录音)和AVAudioRecorder(播放)对象。

// 录音
@property (nonatomic,strong) AVAudioRecorder * recorder;
@property (nonatomic,strong) AVAudioPlayer * audioPlayer;
-(AVAudioPlayer *)audioPlayer
{
    if (!_audioPlayer) {
        //如果是录音文件,则在播放的时候把录音文件存入的文件路径拿到
//        NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/record"];
        // 播放本地文件
        NSString * path = [[NSBundle mainBundle] pathForResource:@"黎允文 - 赵子龙" ofType:@"mp3"];
        _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];// 创建播放机对象
        [_audioPlayer prepareToPlay]; // 准备播放
    }
    return _audioPlayer;
}
- (AVAudioRecorder *)recorder
{
    if (!_recorder) {
        // 录音保存路径
        NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/record"];
        // 录音设置
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        //采样率
        [dic setValue:@(44100) forKey:AVSampleRateKey];
        //录音格式
        [dic setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
        //录音通道
        [dic setValue:@(1) forKey:AVNumberOfChannelsKey];
        // 录音质量
        [dic setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
        //
        _recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:path] settings:dic error:nil];
        [_recorder prepareToRecord]; //准备录音
    }
    return _recorder;
}

4.录音按钮的设置

- (IBAction)pickVideo:(UIButton*)sender {
   
    
    if (sender.isSelected ==NO) {
        [self.recorder record];
        sender.selected = YES;
    }
    else
    {
        [self.recorder stop];
        sender.selected = NO;
    }
}

5.开始播放

- (IBAction)playMedia:(id)sender {
    // 播放
    [self.audioPlayer play];
}

三、视频的录制和播放

基本步骤
1.视频文件路径
2.播放窗口大小的设置 全屏/小屏幕
3.播放视频文件

1.布置UI部分(UIButton)
2.导入关于图片操作的框架(同上)

#import 
#import 
#import 

3.创建视频选择对象和视频播放对象,实现协议


{
    NSURL * mediaUrl;
}
@property(nonatomic,strong)UIImagePickerController * imagePicker;
@property (nonatomic,strong)AVPlayerViewController * avPlayer;

创建对象

- (AVPlayerViewController *)avPlayer
{
    if (!_avPlayer) {
        _avPlayer = [[AVPlayerViewController alloc]init];
        //创建Player对象 -- 视频文件路径
        _avPlayer.player = [[AVPlayer alloc]initWithURL:mediaUrl];
   // 播放窗口大小
        self.avPlayer.view.frame = CGRectMake(100, 100, 400, 400);
        [self.view addSubview:self.avPlayer.view];
        //1.全屏播放
        [self presentViewController:self.avPlayer animated:YES completion:nil];

    }
    return _avPlayer;
}
-(UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePicker.mediaTypes = [NSArray arrayWithObject:(__bridge NSString*)(kUTTypeMovie)];
        _imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh; //视频质量
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

4.采集和播放视频,完成回调操作
1.采集视频

- (IBAction)pickVideo:(id)sender {
    // 如果摄像头可用,从摄像头采集视频数据
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }
}

2.回调处理

// 采集媒体数据完成的回调处理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   // 获取媒体类型
    NSString * type =info[UIImagePickerControllerMediaType];
    if ([type isEqualToString:(__bridge NSString*)(kUTTypeMovie)]) {
        // 获取视频媒体的url
       mediaUrl = info[UIImagePickerControllerMediaURL];
        
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}
// 采集媒体数据取消的回调处理
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    // 取消采集视频
    [self dismissViewControllerAnimated:YES completion:nil];
}

3.视频播放

- (IBAction)playMedia:(id)sender {
    // 播放视频
    [self.avPlayer.player play];
}

ios开发中,为方便优化内存,以上创建对象时,均使用懒加载方式。
至此,ios多媒体的简单开发介绍到这里。
END

你可能感兴趣的:(ios中多媒体开发)