【问题标题】:Playing multiple sounds at once?一次播放多个声音?
【发布时间】:2011-03-23 23:26:44
【问题描述】:

我在一个视图中有 6 个声音。

但是我想要它,以便我可以一次播放多个,所以你点击声音 1(声音 1 正在播放)然后声音 2 播放。当声音 1 仍在播放时。

但此刻我按下声音 1(声音 1 播放),按声音 2(声音 2 播放,但声音 1 停止)

这是音频部分的代码。

- (IBAction)oneSound:(id)sender; {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
    if (theAudio) [theAudio release];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];   
    volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats:YES];
}

- (IBAction)twoSound:(id)sender; {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"];
    if (theAudio) [theAudio release];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];   
    volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats:YES];
}

【问题讨论】:

    标签: iphone objective-c avaudioplayer


    【解决方案1】:

    缺少一些重要的代码,但看起来theAudio 是您用来管理声音播放的全局变量。由于您在播放声音时将其销毁,因此当前正在播放的任何内容都会停止以准备下一个声音。

    有几种方法可以解决这个问题,一种是每个声音都有自己独特的AVAudioPlayer 实例。

    【讨论】:

    • 只需在“theAudio”的第一个声明(在每个函数中)前面放置一个“AVAudioPlayer”,使其成为局部变量,而不是全局变量。这将确保您的两个 AVAudioPlayer 不一样,因此一个不会阻止另一个。
    【解决方案2】:

    不要仅仅因为播放器存在就释放它。播放完放开

    您应该在应用程序委托或应用程序委托的属性中实现这一点,以便在您弹出视图时委托引用仍然有效。

    您不需要为每个声音都这样做。每个声音都会用不同的文件名或 URL 初始化播放器。您可以通过确保在完成播放时释放来获得这一点。另一件需要注意的事情是,如果由于中断而导致播放器没有完成,则释放播放器。

    #pragma mark -
    #pragma mark Audio methods
    
    -(void)playNote:(NSInteger)noteNumber {
    
        NSString *soundFilePath =
        [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i", noteNumber ]
                                        ofType: @"caf"];
    
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    
        AVAudioPlayer *playerToPrepare = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
                                                                                error:nil];
    
        [fileURL release];
    
        [playerToPrepare prepareToPlay];
        [playerToPrepare setDelegate: self];
    
        [playerToPrepare play];
    
    }
    
    #pragma mark -
    #pragma mark AVAudioPlayer delegate methods
    
    - (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) playerThatFinished
                            successfully: (BOOL) completed {
        if (completed) {
            [playerThatFinished release];
        }
    }
    

    【讨论】:

    • 什么意思,每个都是独立的 ib 动作?
    • 会为每 6 个声音编写吗?例如。 1.wav、2.wav、3.wav 等 .h 中需要什么?
    • 我更新了答案,但没有。你不应该……只要你创造了一种在奇怪的情况下释放的方法。
    【解决方案3】:

    你可以声明和使用:

    AVAudioPlayer *theAudio1;    // for file 1.wav
    AVAudioPlayer *theAudio2;    // for file 2.wav
    ...
    AVAudioPlayer *theAudio6;    // etc.
    

    而不是只发布和重用一个 AVAudioPlayer。

    【讨论】:

    • 这仍然限制为 6 个声音而不改变你的释放方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多