使用 AVAudioPlayer 播放多个音频文件 [英] Play multiple audio files using AVAudioPlayer

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

问题描述

我计划免费发布 10 首歌曲录音,但捆绑在一个 iphone 应用程序中.到目前为止,它们无法在网络、iTunes 或任何地方使用.

I am planning on releasing 10 of my song recordings for free but bundled in an iphone app. They are not available on web or itunes or anywhere as of now.

正如你想象的那样,我是 iphone sdk(最新)的新手,所以我一直在浏览开发者文档、各种论坛和 stackoverflow 来学习.

I am new to iphone sdk (latest) as you can imagine, so I have been going through the developer documentation, various forums and stackoverflow to learn.

Apple 的 avTouch 示例应用程序是一个很好的开始.但我希望我的应用程序能够一一播放所有 10 首曲目.所有歌曲都添加到资源文件夹中,并命名为track1、track2...track10.

Apple's avTouch sample application was a great start. But I want my app to play all the 10 tracks one by one. All the songs are added to resources folder and are named as track1, track2...track10.

在 avTouch 应用程序代码中,我可以看到以下 2 个部分,我认为我需要对这些部分进行更改以实现我想要的.但我迷路了.

In the avTouch app code I can see the following 2 parts which is where I think I need to make changes to achieve what I am looking for. But I am lost.

// Load the array with the sample file
NSURL *fileURL = [[NSURL alloc] 
                 initFileURLWithPath: 
                 [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"m4a"]];


- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag == NO)
    NSLog(@"Playback finished unsuccessfully");

[player setCurrentTime:0.];
[self updateViewForPlayerState];
}

任何人都可以帮助我吗
1.如何加载包含添加到资源文件夹的所有10条轨道的阵列
2.当我点击播放时,播放器应该开始第一首曲目.当第一首曲目结束时,第二首曲目应该开始,其余曲目依此类推.

can anyone please help me on
1. how to load the array with all the 10 tracks which are added to resources folder
2. and when I hit play, player should start the first track. when the 1st track ends 2nd track should start and so on for the remaining tracks.

谢谢

推荐答案

inScript,您找到解决方案了吗?您可能想坚持使用诸如 if {//do something } else {//do something else } 之类的简单语句来连续播放它们.

inScript, did you find a solution to this yet? you might want to stick with something simple like an if { // do something } else { // do something else } statement to play them back-to-back.

要加载到数组中,请创建一个新的 plist 文件(右键单击目录树 -> 添加 -> 新文件"并在其中找到一个属性列表;将文件命名为 soundlist.接下来打开该新文件,右键单击它在默认情况下显示字典"的地方,转到值类型"并选择数组"...如果您查看该行的最右侧,您会看到一个看起来像三栏的小按钮,单击该按钮添加您的第一个项目.现在您一次添加一个项目,track01"、track02"等......每行一个.

For loading into an array, create a new plist file (right click on directory tree -> Add -> New File" and find a property list in there; name the file soundslist. Next open that new file, right click on it where it says "Dictionary" by default, go down to "Value Type" and select "Array"... If you look to the far right of that line, you'll see a little 3-bar looking button, click that to add your first item. Now you add one item at a time, "track01", "track02" etc... one per line.

此代码位于您的 .h 文件中:

This code goes in your .h file:

NSArray* soundsList;

此代码位于您的 .m 文件中:

This code goes in your .m file:

NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist" ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];

数组总是从索引 # 0 开始...所以如果你有 5 个轨道,track01 将是索引 0,track02 将是索引 1,依此类推.如果您想快速轮询数组以查看其中的内容,可以添加以下代码:

Arrays always start at index # 0... so if you have 5 tracks, track01 would be index 0, track02 would be index 1, and so on. If you want to quickly poll your array to see what's in it, you can add this bit of code:

int i = 0;

for (i; i <= ([soundsList count] - 1); i++) {

    NSLog(@"soundsList contains %@", [soundsList objectAtIndex:i]);

}

所做的只是计算您的数组中有多少项目(即 5 或 10 首歌曲或多少首歌曲),objectAtIndex: 将在您发送到其中的任何索引号处返回该对象.

All that does is count how many items are in your array (i.e. 5 or 10 or however many songs) and objectAtIndex: will return the object at whatever index number you send into it.

要连续播放,您只需将 if-then 语句放在 audioPlayerDidFinishPlaying 方法中

For playing back-to-back you'd just put the if-then statement in the audioPlayerDidFinishPlaying method

如果你想播放文件,你可以这样做:

If you want to play the file, you can do:

NSString* filename = [soundsList objectAtIndex:YOURINDEXNUMBER];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"mp3"];  

AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded

[newAudio release]; // release the audio safely

theAudio.delegate = self; 
[theAudio prepareToPlay];
[theAudio setNumberOfLoops:0];
[theAudio play];

YOURINDEXNUMBER 是您想播放的任何曲目#(记住,0 = track01,1 = track02 等)

where YOURINDEXNUMBER is whatever track # you wanna play (remember, 0 = track01, 1 = track02, etc)

如果您在设置 .h 文件中的变量时需要帮助,请告诉我,我可以引导您完成.另外,记得在你的 dealloc 方法中释放 theAudio 以便在程序退出时释放它.

If you need help setting up the variables in your .h file, let me know and I can walk you through it. Also, remember to release theAudio in your dealloc method so it will release it when the program exits.

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

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