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

发布于 2024-11-14 06:03:06 字数 1459 浏览 3 评论 0原文

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

谢谢。

I am trying to play 6 seperate .mp3 sound files using instances of AVAudioPlayer.
The sounds are playing at the same time but they seem to be playing out of sync or
at slightly different speeds. Does anyone know why this may be?

Here is how I initialize a sound:

 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;

and here is my AudioSession code:

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]; 

Could it possibly have anything to do with the bit rates of the sounds? or that I am using .mp3?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂柯人 2024-11-21 06:03:06

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

使用此方法将允许所有声音异步同步播放。

I found the solution to this problem by using the playAtTime: method of the AVAudioPlayer class:

   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];

Using this will allow all the sounds to play asynchronously and in sync.

神经暖 2024-11-21 06:03:06

您应该查看多媒体编程指南。 “使用音频”部分有大量有用的信息。
http://developer.apple.com/ Library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/Introduction/Introduction.html

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

当使用硬件辅助解码时,
该设备只能播放一个
支持的实例之一
一次格式化。例如,如果您
正在使用播放立体声 MP3 声音
硬件编解码器,第二个
同时 MP3 声音将使用
软件解码。同样,你
无法同时播放 AAC 和
使用硬件的 ALAC 声音。如果
iPod 应用程序正在播放 AAC 或
MP3背景音,有
声明硬件编解码器;你的
然后应用程序播放 AAC、ALAC 和
使用软件解码的 MP3 音频。

以最佳方式播放多种声音
性能,或高效地发挥
iPod 播放时发出声音
背景,使用线性PCM
(未压缩)或 IMA4(压缩)
音频。

这里还有一点声称你正在做的事情应该是可能的,但苹果似乎正在用“处理器效率最高的多重播放”线提出警告。我认为,如果处理器紧张,就会导致无法及时完美地保持事物。

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

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

You should check out the MultiMedia Programming Guide. The "Using Audio" section has tons of helpful information.
http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/Introduction/Introduction.html

This sounds like it relates to your issue:

When using hardware-assisted decoding,
the device can play only a single
instance of one of the supported
formats at a time. For example, if you
are playing a stereo MP3 sound using
the hardware codec, a second
simultaneous MP3 sound will use
software decoding. Similarly, you
cannot simultaneously play an AAC and
an ALAC sound using hardware. If the
iPod application is playing an AAC or
MP3 sound in the background, it has
claimed the hardware codec; your
application then plays AAC, ALAC, and
MP3 audio using software decoding.

To play multiple sounds with best
performance, or to efficiently play
sounds while the iPod is playing in
the background, use linear PCM
(uncompressed) or IMA4 (compressed)
audio.

Here's another bit that claims what you are doing should be possible but it seems like Apple is throwing in a caveat with "the most processor-efficient multiple playback" line. I would think that if the processor is strained that would lend itself to not keeping things perfectly in time.

Starting in iOS 3.0, nearly all
supported audio formats can be used
for simultaneous playback—namely, all
those that can be played using
software decoding, as described in
Table 1-1. For the most
processor-efficient multiple playback,
use linear PCM (uncompressed) or IMA4
(compressed) audio.

In terms of debugging, you should start with two audio sounds and see if there is an issue. Then work your way up to the 6 and figure out if there is a distinct point at which the problem starts to occur. I would also find (or make) some audio tracks in the formats that Apple recommends (PCM or IMA4) and do the same testing with those. Doing these two things should help you narrow down what the actual problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文