【问题标题】:Can I get an audio session / apply audio units to playback from MPMusicPlayerController?我可以从 MPMusicPlayerController 获取音频会话/应用音频单元播放吗?
【发布时间】:2010-05-03 18:23:50
【问题描述】:

我想控制来自 MPMusicPlayerController 的音频(即从 iPod 库播放)。比如我想给它加个EQ或者做DSP、混响之类的。

这可能吗?有没有我可以掌握的音频会话?或者,也许有什么方法可以使用 AVAudioPlayer 播放 iPod 库中的文件?

【问题讨论】:

    标签: iphone audio avaudioplayer ipod mpmusicplayercontroller


    【解决方案1】:

    MPMusicPLayerController 不能“很好地”与 AV 框架一起工作 我设法获得了一些 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 );
    

    【讨论】:

    • 所以使用 AVURLAsset 您可以直接访问文件还是什么?
    • 是的,您可以完全访问声音数据。我将编辑其余代码的答案以查看实际数据。
    • 好的,这是可能,但是有没有人遇到过更好的方法来做到这一点,可能使用Audio Units
    猜你喜欢
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多