2010-05-03 11 views
8

Me gustaría tomar el control del audio procedente de MPMusicPlayerController (es decir, reproducir desde la biblioteca del iPod). Por ejemplo, me gustaría aplicarle ecualización o hacer DSP, reverb, ese tipo de cosas.¿Puedo obtener una sesión de audio/aplicar unidades de audio para reproducir desde MPMusicPlayerController?

¿Esto es posible? ¿Hay una sesión de audio que pueda manejar? O, ¿hay alguna forma de reproducir archivos de la biblioteca del iPod con un AVAudioPlayer?

Respuesta

6

MPMusicPLayerController no funciona "muy bien" con el Marco AV Logré obtener algo de DSP usando el MPMusicPlayerController para obtener el elemento multimedia y luego obtener la URL para ese artículo. luego use el AVURLAsset y AVAssetReader. algo como esto:

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

Así que con el AVURLAsset, ¿pudiste acceder al archivo directamente o qué? –

+0

Sí, tienes acceso completo a los datos de sonido. Editaré la respuesta para el resto del código para ver los datos reales. – ugiflezet

+1

¡increíble! gracias –

Cuestiones relacionadas