3

I am working on music application, Want to play multiple audio files simultaneously, Found many stackoverflow links to get the solution but still no luck for my scenario,

I have total 8 arrays, In each array there are some number of audio files names. Like,

array1 = @[@"aud1",@"aud2"];
array2 = @[@"aud4",@"aud5",@"aud8",@"aud11"];
....
array8 = @[@"aud3",@"aud6",@"aud7"];

All audio files are stored locally in my project, with .wav file extension.

Now, what I want to achieve is, all the array should play in sequential manner with simultaneous play of all audio files which is in one array. i.e. aud1, aud2 (array1) should play together then instantly in sequential manner aud4,aud5,aud8,aud11 (array2) should play simultaneously and so on..

Also I wanted to implement Pause/Stop functionality as well. I am aware with AVAudioPlayer, AVQueuePlayer..

AVQueuePlayer works well with sequential play. I have implemented this code, Play/Pause/Stop multiple audio file which are stored locally But it is not working with simultaneous play followed by sequential play.

Any help is appreciated!

Thanks in advance!

Community
  • 1
  • 1
iGatiTech
  • 2,306
  • 1
  • 21
  • 45

2 Answers2

1

@Gati, have you tried GCD - Grand Central Dispatch? I think it could be the solution for your problem.

NSArray *array = @[@"aud1",@"aud2"];
    for (id obj in array) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            // Play each aud here
            NSLog(@"%@",obj);

        });
    }

Apple ref.: https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/

Look this for more: https://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1

J. Lopes
  • 1,336
  • 16
  • 27
1
Player * play = [[Looper alloc] initWithFileNameQueue:[NSArray arrayWithObjects: audioFile, audioFile2, nil ]];

Player.h

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface Player : NSObject <AVAudioPlayerDelegate> {
    AVAudioPlayer* play;
    NSArray* fileNameQueue;
    int index;
}

@property (nonatomic, retain) AVAudioPlayer* play;
@property (nonatomic, retain) NSArray* fileNameQueue;

- (id)initWithFileNameQueue:(NSArray*)queue;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)play successfully:(BOOL)flag;
- (void)play:(int)i;
- (void)stop;

@end

Player.m

#import "Player.h"
@implementation Play
@synthesize player, fileNameQueue;

- (id)initWithFileNameQueue:(NSArray*)queue {
    if ((self = [super init])) {
        self.fileNameQueue = queue;
        index = 0;
        [self play:index];
    }
    return self;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)play successfully:(BOOL)flag {
    if (index < fileNameQueue.count) {
        [self play:index];
    } else {
        //reached end of queue
    }
}

- (void)play:(int)i {
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNameQueue objectAtIndex:i] ofType:nil]] error:nil];
    play.delegate = self;
    [play prepareToPlay];
    [play play];    
    index++;
}

- (void)stop {
    if (self.play.playing) [play stop];
}


@end
Anuj J
  • 186
  • 1
  • 11