0

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

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 并仍然保持主要音频源的方法?

4

1 回答 1

0

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

于 2011-11-01T16:06:56.610 回答