我正在尝试从 iPod 库中的 MP3 中提取原始 PCM 样本,以便我可以播放歌曲并操纵音高、速度和应用音效(例如过滤器)。我已经走上了 AVPlayer 和 AVAudioPlayer 的路线,它们都不允许对播放进行太多控制。
下面的代码是据我所知。我现在不知道如何处理我的 while 循环中的 CMSampleBufferRef,因为我不知道使用哪个框架来播放音频并应用此类效果。
知道实现这一目标的最佳方法是什么吗?我已经查看了使用 AVAssetWriter 转换文件的情况,但这对我来说不会削减它,因为该过程太耗时。当然,我可以将 PCM 样本读入内存进行播放,而无需先将它们写入磁盘?
注意:我知道下面的代码引用了项目中的 mp3,但我知道这种方法的工作方式与我从 MPMediaPropertyAssetURL 中提取 NSURL 相同
-(IBAction)loadTrack:(id)sender {
NSString *songPath = [[NSBundle mainBundle] pathForResource:@"Smooth_Sub Focus_192" ofType:@"mp3"];
NSURL *assetURL = [[NSURL alloc] initFileURLWithPath:songPath];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
NSError *assetError = nil;
AVAssetReader *assetReader = [[AVAssetReader assetReaderWithAsset:songAsset
error:&assetError] retain];
if (assetError) {
NSLog (@"Error: %@", assetError);
return;
}
AVAssetReaderOutput *assetReaderOutput = [[AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
audioSettings: nil] retain];
if (![assetReader canAddOutput:assetReaderOutput]) {
NSLog (@"Incompatible Asser Reader Output");
return;
}
[assetReader addOutput: assetReaderOutput];
[assetReader startReading];
CMSampleBufferRef nextBuffer;
while (nextBuffer = [assetReaderOutput copyNextSampleBuffer]) {
/* What Do I Do Here? */
}
[assetReader release];
[assetReaderOutput release];
}