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

时光毁灭记忆、已成空白 提交于 2019-12-24 12:11:37

问题


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!


回答1:


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];
}



回答2:


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



来源:https://stackoverflow.com/questions/35864687/play-pause-stop-multiple-audio-file-which-are-stored-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!