适用于iOS应用的多轨mp3播放 [英] multi track mp3 playback for iOS application

查看:86
本文介绍了适用于iOS应用的多轨mp3播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个涉及播放多轨格式的歌曲(鼓,人声,吉他,钢琴等)的应用程序。我不需要对每个音轨进行任何精美的音频处理,我所需要做的就是播放,暂停和静音/取消静音每个音轨。

I am doing an application that involves playing back a song in a multi track format (drums, vocals, guitar, piano, etc...). I don't need to do any fancy audio processing to each track, all I need to be able to do is play, pause, and mute/unmute each track.

我一直在使用AVAudioPlayer的多个实例,但是在进行设备测试时,我注意到曲目在首次播放时的播放非常不同步。此外,当我暂停播放曲目时,它们会继续变得不同步。经过一番研究,我发现AVAudioplayer的延迟太长,无法在我的应用程序中使用。

I had been using multiple instances of AVAudioPlayer but when performing device testing, I noticed that the tracks are playing very slightly out of sync when they are first played. Furthermore, when I pause and play the tracks they continue to get more out of sync. After a bit of research I've realized that AVAudioplayer just has too much latency and won't work for my application.

在我的应用程序中,我基本上有一个NSArray of AVAudioPlayers我会循环播放并播放每个或暂停/停止每个播放,我确定这是导致它在设备上不同步的原因。

In my application I basically had an NSArray of AVAudioPlayers that I would loop through and play each one or pause/stop each one, I'm sure this is what caused it to get out of sync on the device.

它似乎苹果的音频混音器对我来说效果很好,但是当我尝试实现它时,我遇到一个EXC_BAD_ACCESS错误,我无法弄清。

It seemed like apple's audio mixer would work well for me, but when I try implementing it I get a EXC_BAD_ACCESS error that I can't figure out.

我知道答案是使用OpenAL或音频单元,但是当我需要同时播放5个.mp3音轨时,似乎不需要花几个星期来学习这些内容。有人对如何做到这一点有什么建议吗?谢谢

I know the answer is to use OpenAL or audio units but It just seems unnecessary to spend weeks learning about these when all I need to do is play around 5 .mp3 tracks at the same time. Does anyone have any suggestions on how to accomplish this? Thanks

推荐答案

感谢admsyn的建议,我能够提出解决方案。

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

AVAudioPlayer具有currentTime属性,该属性返回轨道的当前时间,也可以对其进行设置。

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

因此,我实现了admsyn所述的startSynchronizedPlayback,然后当我停止跟踪时添加了以下内容:

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

这段代码基本上遍历了我的音轨数组,每个音轨都拥有自己的AVAudioPlayer,从第一个音轨中获取当前时间,然后设置所有当时的以下曲目。现在,当我使用startSynchronizedPlayback方法时,它们都将同步播放,暂停暂停将使它们也保持同步。希望这对其他尝试保持同步的人有所帮助。

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.

这篇关于适用于iOS应用的多轨mp3播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆