如何播放多个声音? [英] How to play multiple sound?

查看:121
本文介绍了如何播放多个声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要播放多个声音文件.我有以下代码:

I need to play multiple sound files. I have the following code:

我的.h课:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController : UIViewController {
    AVAudioPlayer *musicPlayer;
}

- (IBAction)music1:(id)sender;
- (IBAction)music2:(id)sender;
- (IBAction)music3:(id)sender;
- (IBAction)music4:(id)sender;
- (IBAction)music5:(id)sender;

@end

我的.m文件

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)music1:(id)sender {

    if(musicPlayer.isPlaying)
    {
        [musicPlayer pause];
        return;
    }
    if(musicPlayer == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music1" ofType:@"wav" ]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [musicPlayer play];
}

- (IBAction) music2:(id)sender {

    if(musicPlayer.isPlaying)
    {
        [musicPlayer pause];
        return;
    }
    if(musicPlayer == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music2" ofType:@"wav" ]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [musicPlayer play];
}

- (IBAction) music3:(id)sender {

    if(musicPlayer.isPlaying)
    {
        [musicPlayer pause];
        return;
    }
    if(musicPlayer == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music3" ofType:@"wav" ]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [musicPlayer play];
}

- (IBAction) music4:(id)sender {

    if(musicPlayer.isPlaying)
    {
        [musicPlayer pause];
        return;
    }
    if(musicPlayer == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music4" ofType:@"wav" ]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [musicPlayer play];
}

- (IBAction) music5:(id)sender {

    if(musicPlayer.isPlaying)
    {
        [musicPlayer pause];
        return;
    }
    if(musicPlayer == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music5" ofType:@"wav" ]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [musicPlayer play];
}
@end

但是此代码不起作用:仅播放一个音乐文件.其他文件不是.

But this code does not work: only one music file is played. Other file is not.

还有一个例子:
正在播放音乐.

Also, there is an example:
Music1 playing.

我按下Music2按钮-> Music1结束,Music2开始.

I press the Music2 button -> Music1 end, Music2 start.

我该如何修改我的代码?

How do I need to modfy my code?

推荐答案

每种声音都需要AVAudioPlayer:

You need AVAudioPlayer for each sound:

.h:

     #import <UIKit/UIKit.h>
            #import <AVFoundation/AVFoundation.h>
            #import <AudioToolbox/AudioToolbox.h>

            @interface ViewController : UIViewController {
                AVAudioPlayer *musicPlayer1;
                AVAudioPlayer *musicPlayer2;
                AVAudioPlayer *musicPlayer3;
                AVAudioPlayer *musicPlayer4;
                AVAudioPlayer *musicPlayer5;

NSMutableArray playingMusicArray;

            }

            - (IBAction)music1:(id)sender;
            - (IBAction)music2:(id)sender;
            - (IBAction)music3:(id)sender;
            - (IBAction)music4:(id)sender;
            - (IBAction)music5:(id)sender;

        @end

.m:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    playingMusicArray = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)stopPlayingMusic {
    for(AVAudioPlayer *audioPlayer in playingMusicArray)
    {
        [audioPlayer pause];
        [playingMusicArray removeObject:audioPlayer];


    }
}
- (IBAction)music1:(id)sender {


    [self stopPlayingMusic];
    if(musicPlayer1 == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music1" ofType:@"wav" ]];
        musicPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [playingMusicArray addObject:musicPlayer1];
    [musicPlayer1 play];
}

- (IBAction) music2:(id)sender {

    [self stopPlayingMusic];
    if(musicPlayer2 == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music2" ofType:@"wav" ]];
        musicPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [playingMusicArray addObject:musicPlayer2];

    [musicPlayer2 play];
}

- (IBAction) music3:(id)sender {

    [self stopPlayingMusic];

    if(musicPlayer3 == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music3" ofType:@"wav" ]];
        musicPlayer3 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [playingMusicArray addObject:musicPlayer3];

    [musicPlayer3 play];
}

- (IBAction) music4:(id)sender {

    [self stopPlayingMusic];

    if(musicPlayer4 == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music4" ofType:@"wav" ]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [playingMusicArray addObject:musicPlayer4];

    [musicPlayer4 play];
}

- (IBAction) music5:(id)sender {

    [self stopPlayingMusic];

    if(musicPlayer5 == nil)
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music5" ofType:@"wav" ]];
        musicPlayer5 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    [playingMusicArray addObject:musicPlayer5];

    [musicPlayer5 play];
}
@end

这篇关于如何播放多个声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆