multi track mp3 playback for iOS application

蓝咒 提交于 2019-12-06 03:44:46

thanks to admsyn's suggestion I was able to come up with a solution.

AVAudioPlayer has a currentTime property that returns the current time of a track and can also be set.

So I implemented the startSynchronizedPlayback as stated by admsyn and then added the following when I stopped the tracks:

-(void) stopAll
{
int count = [tracksArr count];
for(int i = 0; i < count; i++)
    {
    trackModel = [tracksArr objectAtIndex:i]
    if(i = 0)
        {
         currentTime = [trackModel currentTime]
        }
    [trackModel stop]
    [trackModel setCurrentTime:currentTime]
    }
{

This code basically loops through my array of tracks which each hold their own AVAudioPlayer, grabs the current time from the first track, then sets all of the following tracks to that time. Now when I use the startSynchronizedPlayback method they all play in sync, and pausing unpausing keeps them in sync as well. Hope this is helpful to someone else trying to keep tracks in sync.

If you're issuing individual play messages to each AVAudioPlayer, it is entirely likely that the messages are arriving at different times, or that the AVAudioPlayers finish their warm up phase out of sync with each other. You should be using playAtTime: and the deviceCurrentTime property to achieve proper synchronization. Note the description of deviceCurrentTime:

Use this property to indicate “now” when calling the playAtTime: instance method. By configuring multiple audio players to play at a specified offset from deviceCurrentTime, you can perform precise synchronization—as described in the discussion for that method.

Also note the example code in the playAtTime: discussion:

// Before calling this method, instantiate two AVAudioPlayer objects and
// assign each of them a sound.

- (void) startSynchronizedPlayback {

    NSTimeInterval shortStartDelay = 0.01;            // seconds
    NSTimeInterval now = player.deviceCurrentTime;

    [player       playAtTime: now + shortStartDelay];
    [secondPlayer playAtTime: now + shortStartDelay];

    // Here, update state and user interface for each player, as appropriate
}

If you are able to decode the files to disk, then audio units are probably the solution which would provide the best latency. If you decide to use such an architecture, you should also check out Novocaine:

https://github.com/alexbw/novocaine

That framework takes a lot of the headache out of dealing with audio units.

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