首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用这段代码来播放更多的声音?

如何使用这段代码来播放更多的声音?
EN

Stack Overflow用户
提问于 2014-02-09 04:02:13
回答 3查看 73关注 0票数 0
代码语言:javascript
运行
复制
   //Action to play Audio//
 -(IBAction)playAudio:(id)sender {
[self.loopPlayer play];
}

 //Action to stop Audio//
-(IBAction)stopAudio:(id)sender {
if (self.loopPlayer.isPlaying) {
    [self.loopPlayer stop];
    self.loopPlayer.currentTime = 0;
    self.loopPlayer.numberOfLoops = -1;
    [self.loopPlayer prepareToPlay];
    }
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//Code that gets audio file "trap synth"//
NSURL* audioFileURL = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
self.loopPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];

}

这是我和一个按钮一起使用的代码,当按钮被点击时播放声音,当按钮被释放时停止声音。我该如何为更多的按钮添加更多的声音呢?我想有更多的按钮,播放和停止不同的声音,就像这样。

代码语言:javascript
运行
复制
property (nonatomic, strong) AVAudioPlayer *loopPlayer;

此代码也在我的ViewController.h文件中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-09 05:34:55

好的,虽然Miro提供的答案在写轨道上,但是给出的代码示例有问题。

应该是在viewDidLoad里-

代码语言:javascript
运行
复制
- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL* audioFileURL1 = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
    self.loopPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL1 error:nil];

   NSURL* audioFileURL2 = [[NSBundle mainBundle] URLForResource:@"other_audio_file" withExtension:@"wav"];
    self.loopPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL2 error:nil];
}

另外,stopAudio:方法应该是

代码语言:javascript
运行
复制
-(IBAction)stopAudio:(id)sender {
    if (self.loopPlayer1.isPlaying && (sender.tag == 1)) {
        [self.loopPlayer1 stop];
        self.loopPlayer1.currentTime = 0;
        self.loopPlayer1.numberOfLoops = -1;
        [self.loopPlayer1 prepareToPlay];
    }
    if (self.loopPlayer2.isPlaying && (sender.tag == 2)) {
        [self.loopPlayer2 stop];
        self.loopPlayer2.currentTime = 0;
        self.loopPlayer2.numberOfLoops = -1;
        [self.loopPlayer2 prepareToPlay];
    }
}

最后,对于playAudio:

代码语言:javascript
运行
复制
-(IBAction)playAudio:(id)sender {
    if([sender tag] == 1){
        [self.loopPlayer1 play];
    }
    if([sender tag] == 2){
        [self.loopPlayer2 play];
    }
}
票数 0
EN

Stack Overflow用户

发布于 2014-02-09 04:06:56

如果您想同时播放不同的声音,您应该考虑创建单独的AVAudioPlayers --如果您为每个声音创建了一个不同的声音,那么您可以通过一个特定的按钮轻松地分别控制(播放/停止)每个声音。

票数 0
EN

Stack Overflow用户

发布于 2014-02-09 04:14:58

在最简单的层面上,您可以这样做,这允许您对所有音频使用相同的按钮处理程序。playAudio检查按下的Play按钮的标记(确保将IB中的标记值设置为1,2等)。真的只需要一个停止按钮。

您可以通过多种方式增强这一点,比如尝试以某种方式重用AVAudioPlayer,并在开始时加载音频而不是所有音频。或者将您的音频文件信息存储在一个数组中,创建一个用于管理的AVAudioPlayers数组,等等。但这是一个开始。

代码语言:javascript
运行
复制
-(IBAction)playAudio:(id)sender {
    // first, stop any already playing audio
    [self stopAudio:sender];

    if([sender tag] == 1){
        [self.loopPlayer1 play];
    } else if([sender tag] == 2){
        [self.loopPlayer2 play];
    }
}

-(IBAction)stopAudio:(id)sender {
    if (self.loopPlayer1.isPlaying) {
        [self.loopPlayer1 stop];
        self.loopPlayer1.currentTime = 0;
        self.loopPlayer1.numberOfLoops = -1;
        [self.loopPlayer1 prepareToPlay];
    } else if (self.loopPlayer2.isPlaying) {
        [self.loopPlayer2 stop];
        self.loopPlayer2.currentTime = 0;
        self.loopPlayer2.numberOfLoops = -1;
        [self.loopPlayer2 prepareToPlay];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL* audioFileURL1 = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
    self.loopPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];

   NSURL* audioFileURL2 = [[NSBundle mainBundle] URLForResource:@"trapsynth" withExtension:@"wav"];
    self.loopPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
}

在.h文件中;

代码语言:javascript
运行
复制
property (nonatomic, strong) AVAudioPlayer *loopPlayer1;
property (nonatomic, strong) AVAudioPlayer *loopPlayer2;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21655095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档