2010-05-03 2 views
8

저는 MPMusicPlayerController에서 오는 오디오를 제어하고 싶습니다 (예 : iPod 라이브러리에서 재생). 예를 들어, EQ를 적용하거나 DSP, 리버브 등을하고 싶습니다.MPMusicPlayerController에서 오디오 세션을 가져 오거나 오디오 장치를 재생할 수 있습니까?

이것이 가능합니까? 처리 할 수있는 오디오 세션이 있습니까? 아니면 AVAudioPlayer를 사용하여 iPod 라이브러리에서 파일을 재생할 수있는 방법이 있습니까?

답변

6

AVM 프레임 워크에서 MPMusicPLayerController가 "멋지게"작동하지 않습니다. MPPlayerController를 사용하여 미디어 항목을 가져 와서 해당 항목에 대한 URL을 가져온 일부 DSP를 얻을 수있었습니다. 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); 
+0

그래서 AVURLAsset을 사용하여 파일에 직접 액세스 할 수 있었습니까? –

+0

예, 사운드 데이터에 대한 전체 액세스 권한을 얻습니다. 나머지 코드에 대한 답변을 편집하여 실제 데이터를 봅니다. – ugiflezet

+1

굉장해! 감사합니다 –