多媒体-图片、音频、视频的基本实现

多媒体-图片、音频、视频的基本实现_第1张图片

前言

iOS开发中关于多媒体是经常使用的,下面就简单总结下基本的实现方式。当然首先需要获得系统的私有设置访问权限。(info.plist中添加)


选取系统图片

  • 选择相册中图片

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
      imagePickerController.delegate = self;
      imagePickerController.allowsEditing = YES;
      imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
      [self presentViewController:imagePickerController animated:YES completion:nil];
    
  • 拍照

     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
          UIAlertAction *suerAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          }];
          UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"相机不可用" preferredStyle:UIAlertControllerStyleAlert];
          [alertVC addAction:suerAction];
          [self presentViewController:alertVC animated:YES completion:nil];
          
          return;
      }
      UIImagePickerController *picker = [[UIImagePickerController alloc] init];
      picker.delegate = self;
      picker.allowsEditing = YES;
      picker.sourceType = UIImagePickerControllerSourceTypeCamera;
      //设置 前后摄像头  UIImagePickerControllerCameraDeviceFront 前置 UIImagePickerControllerCameraDeviceRear 后置摄像头
      picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
      [self presentViewController:picker animated:YES completion:nil];
      [self.imagePickerController takePicture];
    

音频

Core Audio 是iOS和 MAC 的关于数字音频处理的基础,它提供应用程序用来处理音频的一组软件框架,所有关于IOS音频开发的接口都是由Core Audio来提供或者经过它提供的接口来进行封装的,按照官方的说法是集播放,音频处理录制为一体的专业技术,通过它我们的程序可以同时录制,播放一个或者多个音频流,自动适应耳机,蓝牙耳机等硬件,响应各种电话中断,静音,震动等,甚至提供3D效果的音乐播放。
Core Audio有5个框架:1.Core Audio.framework,2.AudioToolbox.framework,3.AudioUnit.framework ,4.AVFoundation.framework,5.OpenAL.framework。
Core Audio.framework并不提供服务,仅提供其他框架可以使用的头文件和数据类型。这其中AVFoundation 框架 (AVFoundation.framework)提供一组播放、记录和管理声音和视频内容的Objective-C类,因此下面我就简单介绍一下他就可以了。

AVFoundation的录音和播放
音频的录制与播放主要和三个类有关AVAudioSession,AVAudioRecorder,AVAudioPlayer。
AVAudioSession
AVAudioSession类由AVFoundation框架引入,每个iOS应用都有一个音频会话,这个会话可以被AVAudioSession类的sharedInstance类方法访问,如下:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
在获得一个AVAudioSession类的实例后,你就能通过调用音频会话对象的setCategory:error:实例方法,来从IOS应用可用的不同类别中作出选择。

AVAudioRecorder
在使用AVAudioRecorder进行音频录制的时候,需要设置一些参数,下面就是参数的说明,并且写下了音频录制的代码:

//音频开始录制

- (void)startRecordWithFilePath:(NSString *)path{

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
/***
AVFormatIDKey  音乐格式,这里采用PCM格式
AVSampleRateKey 采样率   
AVNumberOfChannelsKey 音乐通道数
AVLinearPCMBitDepthKey,采样位数 默认 16
AVLinearPCMIsFloatKey,采样信号是整数还是浮点数
AVLinearPCMIsBigEndianKey,大端还是小端 是内存的组织方式
AVEncoderAudioQualityKey,音频编码质量   
*/

 NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
[recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];//44100.0
[recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
//[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
[recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];  

//初始化录音
self.recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:path]
settings:recordSettings  error:nil];
_recorder.delegate = self;
_recorder.meteringEnabled = YES;
[_recorder prepareToRecord];
[_recorder record];
}

//音频停止录制
 - (void)stopRecord
{
[self.recorder stop];
self.recorder = nil;
}

AVAudioPlayer
AVAudioPlayer类是音频播放的类,一个AVAudioPlayer只能播放一个音频,如果你想混音你可以创建多个AVAudioPlayer实例,每个相当于混音板上的一个轨道,下面就是音频播放的方法。

 //音频开始播放
- (void)startPlayAudioFile:(NSString *)fileName{
//初始化播放器
 player = [[AVAudioPlayer alloc]init];
player = [player initWithContentsOfURL:[NSURL URLWithString:fileName] error:nil];
self.player.delegate = self;   
[player play];
}

//音频停止播放
- (void)stopPlay{
if (self.player) {
     [self.player stop];     
    self.player.delegate = nil;
    self.player = nil;  
}
}

关于音频的播放

   #播放一个视屏
  self.playerLayer = [[AVPlayerLayer alloc]init];
  self.playerLayer.frame = CGRectMake(Scale_X(10), Scale_Y(10), _resultView.width-Scale_X(20), _resultView.height-Scale_X(20));
  [_resultView.layer addSublayer:self.playerLayer];

  AVPlayerLayer *playerLayer;
  //播放设置
  AVPlayer *myPlayer = [AVPlayer playerWithURL:data];
  _playerLayer.player = myPlayer;
  [myPlayer play];

首先了解一下音频播放的实现级别:

  • 离线播放:这里并不是指应用不联网,而是指播放本地音频文件,包括先下完完成音频文件再进行播放的情况,这种使用AVFoundation里的AVAudioPlayer可以满足
  • 在线播放:使用AVFoundation的AVPlayer可以满足
  • 在线播放同时存储文件:使用AudioFileStreamer + AudioQueue 可以满足
  • 在线播放且带有音效处理:使用AudioFileStreamer + AudioQueue + 音效模块(系统自带或者自行开发)来满足

AVAudioPlayer 播放在线音频,会把在线音频完全下载完之后才会播放。如果音频很大,要等待很长时间 ,所以说嘛,AVAudioPlayer压根是不能播放流媒体的。完全下载后才播放就不能算在线播放了
所有苹果公司提供了功能强大的AVPlayer,AVPlayer存在于AVFoundation中,其实它是一个视频播放器,但是用它来播放音乐是没问题的,当然播放音乐不需要呈现界面,因此我们不需要实现它的界面。支持本地和网链,更加接近底层,定制也更加灵活。

  AVPlayer简单一句初始化:
  AVPlayer *newPlayer = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlStr]]; //在线
  AVPlayer *newPlayer = [[AVPlayer alloc] initWithURL:    [NSURL fileWithString:urlStr]];   //本地
  #或者通过 playItem 进行初始化


#获取在线音频文件的时长,是通过 playItem 的 playItem.asset.duration
#而不是  playItem.duration ,使用后者根本无法获得时长而是得到一个@“nan”的字符。
#每个cell都需要获取时长的话,在 Tb滑动的时候会有明显的卡顿现象。使用GCD多线程可以解决这个问题

   //使用多线程解决每个cell获取时长造成的卡顿现象
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.playItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:interNetUrl]];
        CMTime duration = self.playItem.asset.duration;
        float seconds = CMTimeGetSeconds(duration);
        
        if (seconds) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.timeLabel.text = [NSString stringWithFormat:@"%.2f\"",CMTimeGetSeconds(self.playItem.asset.duration)];
            });
        }
        
    });
}

视屏

  • 录制视屏

    -(UIImagePickerController *)VideoPickerController
    {
    if (!_VideoPickerController)
    {
      _VideoPickerController = [[UIImagePickerController alloc] init];
      _VideoPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
      _VideoPickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
      _VideoPickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
      _VideoPickerController.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720;
      _VideoPickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
      _VideoPickerController.allowsEditing = YES;
      _VideoPickerController.delegate = self;
    }
     return _VideoPickerController;
    }
    [self presentViewController:self.VideoPickerController animated:YES completion:nil];
    
  • 选择系统的视屏

    - (UIImagePickerController *)ZYQPick{
      if (!_ZYQPick)
    {
      _ZYQPick = [[UIImagePickerController alloc] init];
      //sourcetype有三种分别是camera,photoLibrary和photoAlbum
      _ZYQPick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
      //Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"
      NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
      //设置媒体类型为public.movie
      _ZYQPick.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];
      _ZYQPick.delegate = self;//设置委托
    }
        return _ZYQPick;
    }
    [self presentViewController:self.ZYQPick animated:YES completion:NULL];
    
  • 处理选择后的视频或者录制后的视频

    #pragma mark - UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
     #视频
    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
    //        NSLog(@"VIDEO....");
    //        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    //        NSString *urlStr = [url path];
    //        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr))
    //        {
                  #保存到相册
    //            UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    //        }
      
      NSURL *sourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
      NSURL *newVideoUrl ; //一般.mp4
      NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
      [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
      //这个是保存在app自己的沙盒路径里,后面可以选择是否在上传后删除掉。我建议删除掉,免得占空间。
      newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;
      [picker dismissViewControllerAnimated:YES completion:nil];
      # 对视频进行压缩
      [self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil];        
    }
    #图片
    else
    {
        #获取到编辑后的图片
       chosenImage = info[UIImagePickerControllerEditedImage];     
    }
    [self dismissViewControllerAnimated:YES completion:nil]; 
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
      [self dismissViewControllerAnimated:YES completion:NULL];
    }
    
关于视频的播放
#import 
@property (nonatomic,strong)AVPlayerLayer *playerLayer;//播放器layer,用于录制完视频后播放视频

  //视频 
 self.playerLayer = [[AVPlayerLayer alloc]init];
 self.playerLayer.frame = CGRectMake(Scale_X(10), Scale_Y(10), _resultView.width-Scale_X(20), _resultView.height-Scale_X(20));
 [_resultView.layer addSublayer:self.playerLayer];

//播放设置
 AVPlayer *myPlayer = [AVPlayer playerWithURL:data];
 _playerLayer.player = myPlayer;
[myPlayer play];

PS:AVPlayer视频播放完成的通知监听

[[NSNotificationCenter defaultCenter] 
  addObserver:self
  selector:@selector(videoPlayEnd)
  name:AVPlayerItemDidPlayToEndTimeNotification 
  object:nil];

持续更新中

你可能感兴趣的:(多媒体-图片、音频、视频的基本实现)