【问题标题】:How can I release a sound using AVAudioPlayer?如何使用 AVAudioPlayer 释放声音?
【发布时间】:2011-03-08 21:16:42
【问题描述】:

我正在尝试使用 AVFoundation 框架创建一个带有很多按钮的音板“交互式采访”。音板已经完成,但我有一个问题,当我按下我的一个声音时,当我同时按下一个新的声音时它不会停止。如果按下新声音,是否有一种简单的方法可以停止以前的声音?我的代码是这样的

- (IBAction)playSound0 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];
}

- (IBAction)playSound1 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"chillwhatup" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];
}

- (IBAction)playSound2 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"gethighorgetlow" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];
}

- (IBAction)playSound3 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"icamefromthere" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];
}

- (IBAction)playSound4 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"idoeverythinggood" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];


    [theAudio play];

}

【问题讨论】:

  • 这与 Xcode 无关。此外,它拼写为“Xcode”,而不是“XCODE”、“xCode”、“XCode”或其他任何内容。已经学习了拼写和术语!

标签: cocoa-touch cocoa avaudioplayer


【解决方案1】:

我只想让我的班级跟踪上次开始的声音,然后在我开始播放新声音之前,停止前一个声音,如果它还在播放。

AVAudioPlayer 有一个 stop 方法来停止声音和一个 正在播放 方法来查看它是否正在播放。

【讨论】:

  • 谢谢,但这并不能告诉我太多:(
猜你喜欢
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多