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

查看:148
本文介绍了使用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,您找到了解决方案吗?这个呢?你可能想要坚持像那样简单的东西,如果{//做某事}其他{//做别的事情} 语句背靠背地播放它们。

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文件(右键单击目录树 - > Add - > New File并在其中找到属性列表;将文件命名为soundslist接下来打开那个新文件,右键单击它默认显示Dictionary,然后转到Value Type并选择Array......如果你看到该行的最右边,你会看到一个小小的3栏查看按钮,点击该按钮添加你的第一个项目。现在你一次添加一个项目,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天全站免登陆