iphone - AVAssestReader 停止音频回调

标签 iphone ios audio audioqueueservices avaudiosession

所以我设置了一个 Audio Session

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

最佳答案

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

关于iphone - AVAssestReader 停止音频回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536194/

相关文章:

iOS int names[] = {CTL_HW, HW_PHYSMEM} 返回的HW_PHYSMEM值为负

ios - CGRectMake 返回零?

ios - 回合制多人 iOS 游戏(如 Words With Friends)的模板?

ios - 在 iOS 8 模拟器上操作应用程序权限

c# - 使用FMOD查找以Hz为单位的频率

iphone - UINavigationItem -title 是否提供辅助功能?

iphone - 将 blender 模型导出到 opengles 并在 iPhone 上渲染

ios - UIButton高亮显示的图像未显示

swift 4 : Detecting strongest frequency or presence of frequency in audio stream.

javascript - 如何使用停止功能停止视频?