iGatiTech
iGatiTech

Reputation: 2268

Play/Pause/Stop multiple audio file which are stored locally

I have 20 audio tracks, which are stored locally in my project.

I want to play all that files sequentially . For that I have used AVQueuePlayer. Please check below code for reference.

Declaration :-

@interface ViewController ()
{
    AVAudioPlayer *player;
    NSMutableArray *arrPlayData;
    AVPlayerItem *item;
}
@property (nonatomic, retain) AVQueuePlayer *queuePlayer;

Button Clicks as below,

-(IBAction)click_Play:(id)sender {

            for (int j = 0; j < arrPlayData.count; j++) {
            path =[[NSBundle mainBundle] pathForResource:[[arrPlayData objectAtIndex:j] valueForKey:@"AudioName"] ofType:@"wav"];
            item = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
                 if (_queuePlayer == nil) {
                     _queuePlayer = [[AVQueuePlayer alloc] initWithPlayerItem:item];
                 } else{
                     [_queuePlayer insertItem:item afterItem:nil];
                 }
            }
        [_queuePlayer setVolume:1.0];
        [_queuePlayer play];
}

Then on pause button I have written below code,

-(IBAction)click_Pause:(id)sender {
    [_queuePlayer pause];
}

And on Stop button I have written below code,

-(IBAction)click_Stop:(id)sender {
    [_queuePlayer removeAllItems];
}

Everything is working well, the problem I am facing is with PAUSE button. When I click on pause button it stops playing. Then again when I click on play button it plays from the same place where I pause it. Till here everything looks good. But on playing the audio queue after pausing, it plays remaining part of audio queue and then again it plays one more time the whole audio queue. Don't know how to resolve it. Why it is playing whole audio queue again?

Anyone have idea? Thanks in advance!

Upvotes: 1

Views: 186

Answers (2)

Maddy
Maddy

Reputation: 311

Because of you are initialize AVPlayerItem again in click_Play. Please take one bool value and insert arrPlayData if AVPlayerItem not allocated.

Upvotes: 1

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5436

whole audio queue is playing again because you are inserting item from arrPlayData every time click_Play is clicked.

So, in click_Play check count of items and handle accordingly like insert item only if count of items _queuePlayer is Zero.

@interface ViewController ()
{
  BOOL FromPause;
}

-(IBAction)click_Stop:(id)sender {
    [_queuePlayer removeAllItems];
}

-(IBAction)click_Pause:(id)sender {
    [_queuePlayer pause];
    FromPause=YES;

}

-(IBAction)click_Play:(id)sender {

            for (int j = 0; j < arrPlayData.count; j++) {
            path =[[NSBundle mainBundle] pathForResource:[[arrPlayData objectAtIndex:j] valueForKey:@"AudioName"] ofType:@"wav"];
            item = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
                 if (_queuePlayer == nil) {
                     _queuePlayer = [[AVQueuePlayer alloc] initWithPlayerItem:item];
                 } else{
                     if(FromPause){
                      //don't add again
                        FromPause=NO;
                     }
                     else{
                          [_queuePlayer insertItem:item afterItem:nil];
                     }
                 }
            }
        [_queuePlayer setVolume:1.0];
        [_queuePlayer play];
}

Upvotes: 1

Related Questions