Matthew Gillingham
Matthew Gillingham

Reputation: 3429

Play an Audio File and Pause at Various Times on iOS

Say that I have an audio file and an collection of time information about particular segments of that audio file, namely their startTime and endTime. What I would like to do is play the audio file sequentially, starting at the beginning and moving segment by segment through the file.

So, for example, let's say that I have data like the following:

Segment 1 - startTime: 1.0s, endTime: 3.4s Segment 2 - startTime: 3.4s, endTime: 5.0s Segment 3 - startTime: 7.8s, endTime: 9.5s ...

I want to play through the audio file sequentially, segment by segment. It will start 1.0s into the file, play until 3.4s, then pause and wait for the user to press a button. Once the user presses the button, it will play from 3.4s to 5.0s, then from 7.8s to 9.5s, etc...

The user will also have the possibility to 'scrub' the audio (but not inside the gaps in-between segments), so although I am playing through sequentially, the possibility exists of returning to a previous segment, in which case this process will repeat (ie. if the user scrubs to a previous or later segment it will pause when it reaches the end of that segment).

What is the best way to accomplish this goal? It seems that I can use AVAudioPlayer, but I don't see a way to pause it exactly at the endTime, other than polling the currentTime repeatedly. AVPlayer and AVPlayerItem looks like another possibility, but if I understand the documentation, there is only one player item at a time...so I suppose I will need to handle replacing AVPlayerItems in order to make the scrubbing function work.

Are there any suggestions about how to best accomplish this goal?

Upvotes: 3

Views: 901

Answers (1)

CA Bearsfan
CA Bearsfan

Reputation: 474

Off the top of my head, either breakup the file into individual segments and play them at the desired time, or pause them using the AVAudioPlayer's function [(name of your audio player) pause]; then resume play when the button's touch up inside fires with [(name of our audio player) play];

Upvotes: 0

Related Questions