ios基础知识

iOS 音频

2017-02-10  本文已影响223人  owenqi

iOS 音频播放

iPod 音乐

@interface MPMediaPickerController : UIViewController

@property (nonatomic, readonly) MPMediaType mediaTypes;
@property (nonatomic, weak, nullable) id<MPMediaPickerControllerDelegate> delegate;
@property (nonatomic) BOOL allowsPickingMultipleItems;
@property (nonatomic) BOOL showsCloudItems;
@property (nonatomic) BOOL showsItemsWithProtectedAssets;
@property (nonatomic, copy, mullable) NSString *prompt; 
@protocol MPMediaPickerControllerDelegate <NSObject>
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection;
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker;
@interface MPMediaItemCollection : MPMediaEntity

@interface MPMediaItem : MPMediaEntity

优势

- 接口完全封装, 使用简单
- 不关心播放模式, 播放列表的维护
- 不关心其他的问题, 例如打断

问题

- 只能播放 iPod 歌曲
- 不是自己的 app 在后台播放, 而是调用系统的音乐 app 在播放
- 进入系统选歌界面或者使用 MPMediaLibrary 歌曲会停止, 播放列表会清空

自行实现 iPod 歌曲选择

类型 资源
在线音频流 音频 URL AVURLAsset
本地音频文件 音频文件 AVURLAsset
iPod 歌曲 Music.app 中的歌曲 MPMediaItem

AVPlayer

AVPlayerItem

AVQueuePlayer

初始化


AudioSession

后台播放

  1. Set AudioSession Category & Active
  2. Set App Background Mode
  3. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]

管理输入输出设备

NowPlayingCenter


录音

AudioSession

  1. 获取录音权限 + 填写使用理由
    • AVAudioSessionRecordPermission permission = [[AVAudioSession sharedInstance] recordPermission];
  2. set Category
    • [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
    • [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  3. set Active
    • [[AVAudioSession sharedInstance] setActive:YES error:nil];
  4. 处理打断或设备更换

AVAudioRecorder

#import <AVFoundation/AVFoundation.h>
NSString *const AVNumberOfChannelsKey; // 声道数
NSString *const AVSampleRateKey; // 采样率
NSString *const AVLinearPCMBitDepthKey; // 位声
NSString *const AVFormatIDKey;
/* ------ AVFormatIDKey ------
kAudioFormatLinearPCM     = 'lpcm'
kAudioFormatApplelMA4     = 'ima4'
kAudioFormatMPEG4AAc      = 'aac'
kAudioFormatULaw          = 'ulaw'
kAudioFormatALaw          = 'alaw'
kAudioFormatAppleLossless = 'alac'
*/
NSString *filePath = [NSTemporaryDirectory() stringByAppendingFileComponent:@"tempRecord.aac"];
NSDictionary *settings = @{
    AVNumberOfChannelsKey : @1,
    AVSampleRateKey : @8000,
    AVLinearPCMBitDepthKey : @16,
    AVFormatIDKey : @(kAudioFormatMPEG4AAC)
};
NSError *error = nil;
// 生成 recorder
_recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:filePath] settings:settings error:&error];

创建文件

开始录音

- (BOOL)preparedToRecord;
- (BOOL)record;
- (BOOL)recordAtTime:(NSTimeInterval)time; // 开始时间
- (BOOL)recordForDuration:(NSTimeInterval)duration; // 录音时长
- (BOOL)recordAtTime:(NSTimeInterval)time forDuration:(NSTimeInterval)duration;

录音停止和文件操作

- (void)pause;
- (void)stop;
- (BOOL)deleteRecording;
@property BOOL recording;
@property NSURL *url;
@property NSTimeInterval currentTime; // 当前录音进行的时间

AVAudioPlayer

资源类型

类型 资源
在线音频流 音频 URL AVURLAsset
本地音频文件 音频文件 AVURLAsset
iPod歌曲 Music.app中的歌曲 MPMediaItem

AVURLAsset �: AVAsset

/*  AVAsset  */
@interface AVAsset : NSObject
+ assetWithURL:(NSURL *)URL;
@property (notatomic, readonly) CMTime duration;

@property NSArray *commomMetadata;
@property NSArray *metadata;
/*  AVURLAsset  */
@interface AVURLAsset : AVAsset
+ URLAssetWithURL: options: ;
- initWithURL: options: ;

@property NSURL *URL;
// 继承子 AVAsset 后提供了两个初始化方法
// 提供 URL 属性的访问
// 提供 metadata 的读取

Metadata

创建 AVURLAsset

// 在线音频
NSURL *audioURL = [NSURL URLWithString:@"http://xxx.com/music.mp3"];
AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioURL];

// 本地音频
NSString *filePath = [[NSBundle mainBundle] pathForResources:@"music" ofType:@"mp3"];
NSURL *fileURL = [NSURL fireURLWithPath:filePath];
AVURLAsset *fileAsset = [AVURLAsset assetWithURL:fileURL];

// iPod 音频
MPMediaQuery *query = [MPMediaQuery songsQuery];
if (query.items.count > 0) {
    MPMediaItem *item = query.item[0];
    AVURLAsset *iPodAsset = [AVURLAsset assetWithURL:item.assetURL];
}

AVAudioPlayer

@interface AVAudioPlayer : NSObject

- initWithContentOfURL: error: ;
- initWithData: error: ;

- initWithContentOfURL: fileTypeHint: error: ; // 表示单个歌曲
- initWithData: fileTypeHint: error: ;
上一篇 下一篇

猜你喜欢

热点阅读