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

标签 iphone audio avaudioplayer mixing

我正在尝试使用 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;

这是我的 AudioSession 代码:

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?

谢谢。

最佳答案

我通过使用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];

使用此功能将允许所有声音异步且同步播放。

关于iphone - AVAudioPlayer 声音文件混合时播放不同步 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253925/

相关文章:

r - 从一个大型(350 mb).wav制成的S4对象中输出许多.wav,这些内容分为5秒

ios - 在 iOS 中立即开始播放简短的自制声音

ios - 如何在 audioPlayerDidFinishPlaying 方法中使用多个 play()

iphone - 具有自己的 XIB 的 UIView 子类

iphone - 如何判断 MapKit 的 rightCalloutAccessoryView 已被触摸

PHP readfile 在 Safari 中无法正常工作

iphone - iPhone AVAudioFoundation声音完成循环

iphone - 有没有非私有(private) API 替代方案?

iphone - 使用 Core Text 为给定的字符串范围设置背景颜色/突出显示颜色

ios - 带 SwiftUI 的播放/停止按钮