多个AVAudioPlayer - 没有声音

时间:2013-05-06 11:17:36

标签: ios objective-c avaudioplayer

我是相当新的Xcode和ObjC,我在通过多个AVAudioPlayers播放多个音频文件时遇到了麻烦。

理论上我的应用程序相当简单,它需要汽车RPM和负载值并根据这些值播放声音。

所以,我有一系列RPM值的13个声音样本。我的代码确定应该播放的音量和速率,以便在应用程序中复制正确的引擎声音 - 注意它们都在同时播放,它是控制声音的音量和速率。

所以,我想出了这样的音量和速率(这是一个片段):

if ((rpm > 4989) && (rpm < 5339)) {

    av1Pitch  = 0.0f;
    av2Pitch  = 0.0f;
    av3Pitch  = 0.0f;
    av4Pitch  = 0.0f;
    av5Pitch  = 0.0f;
    av6Pitch  = 0.0f;
    av7Pitch  = 0.0f;
    av8Pitch  = 0.0f;
    av9Pitch = rpm / 4989.0f;
    av10Pitch = rpm / 5338.0f;
    av11Pitch  = 0.0f;
    av12Pitch  = 0.0f;
    av13Pitch  = 0.0f;

    av1Volume  = 0.0f;
    av2Volume  = 0.0f;
    av3Volume  = 0.0f;
    av4Volume  = 0.0f;
    av5Volume  = 0.0f;
    av6Volume  = 0.0f;
    av7Volume  = 0.0f;
    av8Volume  = 0.0f;
    av9Volume = av9Pitch - 1.0f;
    av10Volume = 1.0f - av9Volume;
    av11Volume  = 0.0f;
    av12Volume  = 0.0f;
    av13Volume  = 0.0f;
}

然后我通过13个AVAudioPlayers并设置球员的体积和比率:

if (audioPlayer2000.isPlaying) {
    //already playing, ready for volume / pitch adjustments

    audioPlayer2000.volume = av2Volume;
    audioPlayer2000.rate = av2Pitch;   
}
else{
    //nothing playing - play the file
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/1052.wav", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    //NSData *songFile2 = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
    audioPlayer2000 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    //audioPlayer2000 = [[AVAudioPlayer alloc] initWithData:songFile2 error:&error];
    audioPlayer2000.numberOfLoops = -1;
    audioPlayer2000.enableRate = YES;
    audioPlayer2000.rate = av2Pitch;
    audioPlayer2000.volume = av2Volume;

    if (audioPlayer2000 == nil) {
        NSLog([error description]);
    }
    else{
        [audioPlayer2000 play];
    }   
}

这在播放1个文件时工作正常,但是一旦我更改RPM值以便它应该播放另一个文件,无论RPM值是什么,所有声音都会停止。

我发现停止此操作的唯一方法是在玩家身上调用setCurrentTime - 但这会导致“抖动”播放。

我认为可能是ARC播放,所以我为所有玩家创建了属性。

我错过了一些基本的东西吗?或者我没有采取正确的方法?

0 个答案:

没有答案
相关问题