Могу ли я получить аудио сеанс / применить аудиоустройства для воспроизведения из MPMusicPlayerController? - PullRequest
8 голосов
/ 03 мая 2010

Я бы хотел взять на себя управление звуком, исходящим из MPMusicPlayerController (то есть, воспроизводить из библиотеки iPod). Например, я хотел бы применить к нему эквалайзер или сделать DSP, реверберацию и все такое.

Возможно ли это? Есть ли аудио сеанс, на котором я могу взять ручку? Или, возможно, есть какой-то способ воспроизведения файлов из библиотеки iPod с помощью AVAudioPlayer?

1 Ответ

6 голосов
/ 26 июня 2011

MPMusicPLayerController не работает "красиво" с AV Framework Мне удалось получить некоторый DSP, используя MPMusicPlayerController, чтобы получить элемент мультимедиа, а затем получить 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 );
...