gpt4 book ai didi

iphone - 我可以从 MPMusicPlayerController 获取 Audio Session /应用音频单元进行播放吗?

转载 作者:行者123 更新时间:2023-12-03 18:36:11 25 4
gpt4 key购买 nike

我想控制来自 MPMusicPlayerController 的音频(即从 iPod 库播放)。例如,我想对其应用 EQ 或进行 DSP、混响之类的事情。

这可能吗?有我可以掌握的 Audio Session 吗?或者,也许有某种方法可以使用 AVAudioPlayer 播放 iPod 库中的文件?

最佳答案

MPMusicPLayerController 不能与 AV 框架“很好地”工作我设法使用 MPMusicPlayerController 获取一些 DSP 来获取媒体项目,然后获取该项目的 url。然后使用 AVURLAsset和 AVAssetReader。像这样的东西:

MPMediaItem *currentSong = [myMusicController nowPlayingItem];
NSURL *currentSongURL = [currentSong valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:currentSongURL options:nil];
NSError *error = nil;
AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error];

AVAssetTrack* track = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

NSMutableDictionary* audioReadSettings = [NSMutableDictionary dictionary];
[audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
forKey:AVFormatIDKey];

AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:audioReadSettings];
[reader addOutput:readerOutput];
[reader startReading];
CMSampleBufferRef sample = [readerOutput copyNextSampleBuffer];
while( sample != NULL )
{
sample = [readerOutput copyNextSampleBuffer];

if( sample == NULL )
continue;

CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer( sample );
CMItemCount numSamplesInBuffer = CMSampleBufferGetNumSamples(sample);

AudioBufferList audioBufferList;

CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sample,
NULL,
&audioBufferList,
sizeof(audioBufferList),
NULL,
NULL,
kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
&buffer
);

for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < numSamplesInBuffer; i++) {
NSLog(@"%i", samples[i]);
}
}

//Release the buffer when done with the samples
//(retained by CMSampleBufferGetAudioBufferListWithRetainedblockBuffer)
CFRelease(buffer);

CFRelease( sample );

关于iphone - 我可以从 MPMusicPlayerController 获取 Audio Session /应用音频单元进行播放吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2760230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com