AVQueuePlayer only plays my sequence of sounds once when clicking on button the first time

StackOverflow https://stackoverflow.com/questions/18677854

  •  28-06-2022
  •  | 
  •  

Question

I've setup an AVQueuePlayer named player in my viewDidLoad method. I also have a UIbutton method which plays that sequence.

here is my view did load method

AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"first" ofType:@"mp3"]]];
AVPlayerItem *secondSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"second" ofType:@"mp3"]]];
AVPlayerItem *thirdSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"third" ofType:@"mp3"]]];

player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstSound, secondSound, thirdSound,  nil]];

and here is my button's method

-(IBAction)sequencePlay:(id)sender {[player play];}

pressing the button only produces the sequence of sounds once, when the view is first loaded. when i click the button again nothing happens. however if i navigate to another view controller and back to this view controller, it will work the first time i press the button again but as before, it only works for the first click.

update: doing the setup in the button instead of the viewDidLoad method was the key as according to the first answer to solve my problem.

My mp3 files are small, each file is just one word being spoken. I have a UIPickerView setup with 3 columns, and depending on which rows are selected, it will play those specific words in sequence when the button is pressed. Any good advice on how i may accomplish that?

i'm currently trying to do this but get an error

-(IBAction)sequencePlay:(id)sender 
{
NSString *sayFirst = _firstRowWord.description;

AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sayFirst.description ofType:@"mp3"]]];

i get the following error * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

the description of the firstRowWord is the same exact name as the mp3 file i want to play

Was it helpful?

Solution

You could try using the sequencePlay: method to setup the AVQueuePlayer there. So:

-(IBAction)sequencePlay:(id)sender {
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"first" ofType:@"mp3"]]];
AVPlayerItem *secondSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"second" ofType:@"mp3"]]];
AVPlayerItem *thirdSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"third" ofType:@"mp3"]]];

player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstSound, secondSound, thirdSound,  nil]];
[player play];
}

If you are concerned about the performance of this you could use the actionAtItemEnd property to repopulate the Player. Hope this helped!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top