开发者

Can I get an audio session / apply audio units to playback from MPMusicPlayerController?

开发者 https://www.devze.com 2022-12-29 04:29 出处:网络
I\'d like to take c开发者_C百科ontrol of the audio coming from MPMusicPlayerController (i.e., playing from the iPod library). For example, I\'d like to apply EQ to it or do DSP, reverb, that kind of t

I'd like to take c开发者_C百科ontrol of the audio coming from MPMusicPlayerController (i.e., playing from the iPod library). For example, I'd like to apply EQ to it or do DSP, reverb, that kind of thing.

Is this possible? Is there an audio session that I can grab a handle on? Or, perhaps is there some way to play back files from the iPod library using an AVAudioPlayer?


MPMusicPLayerController does not work "nicely" with the AV Framework I managed to get some DSP Using the MPMusicPlayerController to get the media item then get url for that item. then use the AVURLAsset and AVAssetReader. something like this:

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 );
0

精彩评论

暂无评论...
验证码 换一张
取 消