PYAudioKit
一个简单的 iOS 录音和播放器。
这是一个 CocoaPods 项目。
Player
播放器支持 HLS 音频和本地文件音频。通常,我们只需要调用 [_player playUrl:[NSURL URLWithString:@"..."]]
来播放特定的音频流。对于 http 实时流(HLS),播放器将使用 AVPlayer
和 AVPlayerItem
来加载数据并播放。它将使用 KVO 检查项目的状态。对于本地音频文件,播放器将使用 AVAudioPlayer
来播放,这非常简单。
我们可以使用 [prepareUrl:seekFrom:autoPlay:]
并将 autoPlay
设置为 NO
来仅加载流缓冲区而不启动音频会话。或者,我们可以将 seekFrom
设置为告诉播放器从音频流的指定位置开始播放。
HLS 播放器将检查缓冲区状态,并将在网络断开时自动恢复。
Recorder
录音器使用 AudioQueueRef
进行录音。应初始化时设置音频格式和文件格式。当启用表棒提取时,录音器将每秒调用代表 60 次,并返回第一频道的音频幅度。您可以使用 [startToGatherEnvorinmentSound]
和 [startMeterFetching]
来构建背景环境动态图。
安装
- 在Xcode项目的根目录下创建'Podfile'或修改文件
- 在
end
之前添加以下行
pod "PYAudioKit", "~> 0.3"
- 运行
pod install
或pod update
以获取源代码 - 打开*.xcworkspcade
示例
#import <PYAudioKit/PYAudioKit.h>
@interface MyViewController {
PYAudioPlayer *_audioPlayer;
PYAudioRecorder *_audioRecorder;
UIButton *_recordBtn;
}
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize the player and recorder
_recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_recordBtn setBackgroundColor:[UIColor blueColor]];
[_recordBtn setFrame:CGRectMake(80, 80, 160, 88)];
[_recordBtn addTarget:self action:@selector(actionTouchDown:)
forControlEvents:UIControlEventTouchDown];
[_recordBtn addTarget:self action:@selector(actionTouchUpInside:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:_recordBtn];
}
- (void)actionTouchDown:(id)sender {
// the recorder use m4a format, default with 2 channels, 8000 sample rate
_audioRecorder = [PYAudioRecorder
audioRecorderWithFormat:aqPYAudioRecorderFormatMPEG4AAC
fileType:kAudioFileMPEG4Type];
[_audioRecorder startToRecord];
}
- (void)actionTouchUpInside:(id)sender {
if ( _audioRecorder == nil ) return;
NSString *_savedAudioPath = [_audioRecorder stopRecordAndSaveWithFileName:@"example.m4a"];
// Play the audio
_audioPlayer = [PYAudioPlayer object];
[_audioPlayer playUrl:[NSURL fileURLWithPath:_savedAudioPath]];
}
@end