0

I have array of QueuePlayer as below,

Print Queue Player array : (
    "<AVQueuePlayer: 0x7fed625a51c0>",
    "<AVQueuePlayer: 0x7fed624baa00>",
    "<AVQueuePlayer: 0x7fed624c43c0>"
)

On button click I am playing audio from above array,

-(IBAction)click_Play:(UIButton *)sender {

    [(AVQueuePlayer *)[arrQueuePlayer objectAtIndex:sender.tag] setVolume:1.0];
    [(AVQueuePlayer *)[arrQueuePlayer objectAtIndex:sender.tag] play];
}

Reason behind generating array of QueuePlayer is, my play button is in UITableView. Playing audio from array on button click works well only for the first time. If I click play button again. It's not playing sound. Don't know what is wrong. I checked it by debugging code, My array is getting nulled or anything. Then why it is not playing again on button click?

Please help me. Thanks in advance!

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
  • Do you allow to play complete track and after that to try to play again? – Tirth Mar 09 '16 at 07:44
  • @Tirth Yess I tried to play it again after playing complete track once. – iGatiTech Mar 09 '16 at 08:42
  • how you add tracks in AVQueuePlayer? – Tirth Mar 09 '16 at 09:25
  • @Tirth That is quite large logic. As I have audio tracks added locally. User allowed to select as many audio tracks as they want. Then after I sequentially added all audio tracks in QueuePlayer and generated this array for UITableView. But I am wondering, If it plays sound first time from the array then why not again? – iGatiTech Mar 09 '16 at 09:28
  • Your problem come from other code but not you given code here. – Tirth Mar 09 '16 at 09:46

1 Answers1

1

As you are playing queue player from array, there may be possibilities that it becomes nil after playing once. I am not sure whether this your actual issue or not. Because I don’t have any idea how you have created these queue player objects and added it in array. But I guess queue player object gets nil after playing should be the issue.

If so then for that you have to recreate your queue player once it is getting played. As AVQueuePlayer don’t have any delegate method such as audioPlayerDidFinishPlaying:(This is the delegate method of AVAudioPlayer). So for AVQueuePlayer you need to add observer for playerItemDidReachEnd: as below,

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:item];

In this observer method recreate your queue player and replace it in your array. Hope it will help you out! Cheers..

Maddy
  • 311
  • 2
  • 11