Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AVAudioPlayer混音时播放的声音文件不同步-iPhone_Iphone_Audio_Avaudioplayer_Mixing - Fatal编程技术网

AVAudioPlayer混音时播放的声音文件不同步-iPhone

AVAudioPlayer混音时播放的声音文件不同步-iPhone,iphone,audio,avaudioplayer,mixing,Iphone,Audio,Avaudioplayer,Mixing,我正在尝试使用AVAudioPlayer实例播放6个独立的.mp3声音文件。 声音在同一时间播放,但似乎播放不同步或 以稍微不同的速度。有人知道为什么会这样吗 以下是我初始化声音的方式: NSURL *musicurl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource: @"SoundLoop" ofType: @"mp3"]]; music = [[AVAudioPlayer alloc]

我正在尝试使用AVAudioPlayer实例播放6个独立的.mp3声音文件。 声音在同一时间播放,但似乎播放不同步或 以稍微不同的速度。有人知道为什么会这样吗

以下是我初始化声音的方式:

 NSURL *musicurl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource: @"SoundLoop" ofType: @"mp3"]];
music = [[AVAudioPlayer alloc] initWithContentsOfURL: musicurl error: nil];
[musicurl release];
music.numberOfLoops = -1; // Loop indefinately
music.currentTime = 0; // start at beginning
music.volume = 1.0;
music.meteringEnabled = YES;
这是我的音频会话代码:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];     
[[AVAudioSession sharedInstance] setPreferredHardwareSampleRate:44100 error:nil];
[[AVAudioSession sharedInstance] setPreferredIOBufferDuration:30 error:nil];

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
Float32 hardvol = 1.0;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, sizeof(hardvol), &hardvol);
UInt32 doSetProperty = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive:YES error: nil]; 
这可能和声音的比特率有关吗?或者我正在使用的.mp3


谢谢。

您应该查看多媒体编程指南。“使用音频”部分有大量有用的信息。

这听起来似乎与您的问题有关:

使用硬件辅助解码时, 该设备只能播放一段视频 其中一个受支持的 一次格式化。例如,如果你 正在使用播放立体声MP3声音 硬件编解码器,第二个 将使用同步MP3声音 软件解码。同样地,你 不能同时播放AAC和 使用硬件的ALAC声音。如果 iPod应用程序正在播放AAC或 在MP3声音的背景下,它有 硬件编解码器;你的 应用程序然后播放AAC、ALAC和 MP3音频使用软件解码

以最佳方式播放多个声音 表现,或有效地发挥 在播放iPod时发出声音 背景,使用线性PCM (未压缩)或IMA4(压缩) 音频

这里还有一点,声称你所做的应该是可能的,但似乎苹果是在用“处理器效率最高的多重播放”这一行发出警告。我认为,如果处理器过于紧张,就会导致无法及时完美地完成任务

从iOS 3.0开始,几乎所有 可以使用支持的音频格式 对于同步播放,即所有 那些可以使用 软件解码,如中所述 表1-1。最多 处理器效率高的多重播放, 使用线性PCM(未压缩)或IMA4 (压缩)音频


在调试方面,您应该从两个音频开始,看看是否有问题。然后一直到第6步,找出是否有一个明显的问题开始出现的点。我还会找到(或制作)一些苹果推荐格式的音频曲目(PCM或IMA4),并对它们进行同样的测试。做这两件事可以帮助你缩小实际问题的范围。

我通过使用AVAudioPlayer类的playAtTime:方法找到了这个问题的解决方案:

   NSTimeInterval shortStartDelay = 0.5;            // seconds
    NSTimeInterval now = player.deviceCurrentTime;

    [player playAtTime:now + shortStartDelay];  //these players are instances of AVAudioPlayer
    [player2 playAtTime:now + shortStartDelay];
    [player3 playAtTime:now + shortStartDelay];
使用此选项将允许所有声音以异步和同步方式播放