Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9300785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:51:52+00:00 2026-06-18T22:51:52+00:00

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

  • 0

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

My .h class:

#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

My .m file

#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.

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

How do I need to modfy my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:51:53+00:00Added an answer on June 18, 2026 at 10:51 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my C# application, I need to play multiple audio files ( WAV and
I have the need to play multiple soundeffects at once in my WP7 app.
I am in need of playing multiple sound files one after the other. The
I have some code that needs adjusting. I need the .play rel to be
We need to play MP3 files in jPlayer without using Flash as a fallback.
I need to play Music Library files using file URL, that I will set
I need to play a background sound all the time as long as my
I need to play a part of an MP3 file in my java code.
What I'm doing : I need to play audio and video files that are
I have an aes encrypted mp3 file that I need to play in iOS;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.