Play multiple Audio Files with AVPlayer

10,540

Solution 1

For every sound you want to make make a new AVPlayer.

NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];

Solution 2

I have never answered a question here and I don't know in anyone is still waiting for an answer to this but heres my take... Try this and it should work, I am currently using it to play 12 plus simultaneous audio samples. I apologize if I am doing something newbish..

You press a button and you run this code...

But first you need to:

  1. Need to import AVFoundation to project and #import into .h file as well then we can play sound with this.
  2. Need to put "AVAudioPlayer *myAudio;" without quotation marks of course somewhere on top (usually on top of viewDidLoad).

Then just...

-(IBAction)playButtonPressed:(id)sender {

    NSURL *yourMusicFile;
    yourMusicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"your_Song_Name" ofType:@"mp3"]];

    myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [myAudio play];
    NSLog(@"Button -playButtonPressed- has been pressed!");
}
Share:
10,540
Rouslan Karimov
Author by

Rouslan Karimov

Updated on June 04, 2022

Comments

  • Rouslan Karimov
    Rouslan Karimov almost 2 years

    I'm trying to play multiple sounds at the same time.

    The approach initially I've taken was to create several players , but it seems wrong one.

    What's the best way to play several audio files at the same time.

    Is it through making them AVAssets, but in this case how would I stop and play them whenever I want.

    Really appreciate your help.

    The reason I need AVPlayer is to fetch sounds from the iPod Library.

    I finally got an answer from TechSupport of Apple Dev Team and it seems I'm on the right track when I decided to use several AVPlayer.