AVAssestReader停止音频回调

时间:2011-09-24 00:41:19

标签: iphone ios audio audioqueueservices avaudiosession

所以我设置了一个音频会话

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);

UInt32 audioCategory = kAudioSessionCategory_MediaPlayback; //for output audio
OSStatus tErr = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(audioCategory),&audioCategory);

然后设置AudioQueue或RemoteIO设置以直接从文件播放某些音频。

AudioQueueStart(mQueue, NULL);

播放音频后,我可以在应用的状态栏中看到“播放图标”。我接下来设置一个AVAssetReader。

AVAssetTrack* songTrack = [songURL.tracks objectAtIndex:0];

NSDictionary* outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys:

                                    [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                                    //     [NSNumber numberWithInt:AUDIO_SAMPLE_RATE],AVSampleRateKey,  /*Not Supported*/
                                    //     [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,   /*Not Supported*/

                                    [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved,

                                    nil];

NSError* error = nil;
AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:songURL error:&error];

//      {
//          AVAssetReaderTrackOutput* output = [[AVAssetReaderTrackOutput alloc] initWithTrack:songTrack outputSettings:outputSettingsDict];
//          [reader addOutput:output];
//          [output release];
//      }

{   
    AVAssetReaderAudioMixOutput * readaudiofile = [AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:(songURL.tracks) audioSettings:outputSettingsDict];
    [reader addOutput:readaudiofile];
    [readaudiofile release];
}

return reader;

当我打电话给[reader startReading]时,音频停止播放。在RemoteIO和AudioQueue情况下,回调停止被调用。

如果我添加混音选项:

AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (UInt32), &(UInt32) {0});

然后,当调用AudioQueueStart后的音频时,不再显示“播放图标”。我也被锁定了其他功能,因为手机不会将我视为主要音频源。

有没有人知道我可以使用AVAssetReader并保持主音频源的方式?

1 个答案:

答案 0 :(得分:0)

从iOS 5开始,这已得到修复。它仍然无法在iOS 4或更低版本中运行。不需要MixWithOthers(可以设置为false),即使AVAssetReader正在读取,AudioQueue / RemoteIO也会继续接收回调。