【问题标题】:AVAudioPlayer sound files playing out of sync when mixed - iPhone混合时播放不同步的 AVAudioPlayer 声音文件 - iPhone
【发布时间】:2011-06-06 14:55:58
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: iphone audio avaudioplayer mixing


    【解决方案1】:

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

    使用这将允许所有声音异步和同步播放。

    【讨论】:

      【解决方案2】:

      您应该查看多媒体编程指南。 “使用音频”部分有大量有用的信息。 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(压缩) 音频。

      还有一点声称您正在做的事情应该是可能的,但似乎 Apple 提出了“处理器效率最高的多重播放”系列的警告。我认为,如果处理器紧张,这将导致无法及时完美地保持一切。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多