使用AVAudioPlayer播放多种声音(一对一) [英] Play multiple sounds (one by one) with AVAudioPlayer

查看:64
本文介绍了使用AVAudioPlayer播放多种声音(一对一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVAudioPlayer制作MP3播放器.我有多种MP3声音,想一一播放.以下是我的应用程序的逻辑:

I am using AVAudioPlayer to make an MP3 player. I have multiple MP3 sounds and would like to play these sounds one by one. Following is the logic of my app:

///// For playing 1st sound

mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
[mp3Player  prepareToPlay];
[mp3Player play];


///// For playing 2nd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil]; 
[mp3Player  prepareToPlay];
[mp3Player play];


///// For playing 3rd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url3 error:nil];
[mp3Player  prepareToPlay];  
[mp3Player play];

etc...

只想知道:在播放下一个声音之前,我淘汰播放器并分配AVAudioPlayer.此逻辑是否涉及性能(速度)或内存(泄漏)? (我正在使用ARC).如果是这样,有什么办法可以只分配AVAudioPlayer一次,并接受指向这些声音的不同URL?提前非常感谢.

Just would like to know: I nil out the player and alloc AVAudioPlayer before playing the next sound. Is there any performance (speed) or memory (leak) concern in this logic? (I am using ARC). If so, is there any way to alloc AVAudioPlayer just ONE time, and to accept diffrent URLs pointing to those sounds? Thanks a lot in advance.

推荐答案

我认为下面的链接可能会帮助您:

I think below link may help you:

如何在AVAudioPlayer中循环播放多个音频文件?

其中audioPlayerDidFinishPlaying用于播放完一首歌曲.

In which audioPlayerDidFinishPlaying is used to play next song after one finishes.

这篇关于使用AVAudioPlayer播放多种声音(一对一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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