2012-12-03 56 views
1

我正在使用大约50个AVAudioPlayers,每个加载一个单独的音频文件。这些文件已修复并在应用程序包中。他们后来在应用程序中的事件触发。目前,我正在对每个播放器实例的创建进行硬编码,如下所示:从Plist中加载AVAudioPlayer文件的URL

//No01 
NSURL *no01URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"audio1" ofType:@"aiff"]]; 
self.no01Player = [[AVAudioPlayer alloc] initWithContentsOfURL:no01URL error:nil]; 
no01Player.numberOfLoops = 0; 
no01Player.volume = 0; 
no01Player.delegate = self; 

//No02 
NSURL *no02URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"audio2" ofType:@"aiff"]]; 
self.no02Player = [[AVAudioPlayer alloc] initWithContentsOfURL:no02URL error:nil]; 
no02Player.numberOfLoops = 0; 
no02Player.volume = 0; 
no02Player.delegate = self; 

//No03 and so on... 

显然,这是一种不好的编码习惯。我想在Plist中拥有文件列表,并将这些文件加载​​到仅填充上述代码示例的变量中。我想学习如何做到这一点,但加载Plist,阵列,字典等数据的经验有限。

任何帮助表示赞赏,即使它是指向我的方向的相关教程。

谢谢。

回答

1

确定为别人需要做到这一点,这是我结束了:

//Load location data from Plist file into an Array 

NSString *path = [[NSBundle mainBundle] pathForResource:@"Audionodes" ofType:@"plist"]; 
locationArray = [[NSArray alloc] initWithContentsOfFile:path]; 

// Create AVAudioPlayers for this view 

self.audioPlayerArray = [[NSMutableArray alloc] init]; 

for (int i = 0; i < [locationArray count]; i++) { 

    NSString *filename = [[locationArray objectAtIndex:i] valueForKey:@"filename"]; 
    NSString *filetype = [[locationArray objectAtIndex:i] valueForKey:@"filetype"]; 
    int loops = [[[locationArray objectAtIndex:i] valueForKey:@"loops"] intValue]; 
    NSURL *url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:filename ofType:filetype]]; 

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    player.delegate = self; 
    player.numberOfLoops = loops; 
    player.volume = 0; 

    [self.audioPlayerArray addObject:player]; 

    [url release]; 
    [player release]; 

} 

把生成的球员在audioPlayerArray意味着我可以在他们以后得到。

4

您可以使用'换行'分隔符将音频文件名放在文本文件中。读取文件并将其名称存储在数组中。要创建URL从数组动态取文件名下方

// Read description from text file 
NSBundle *audioBundle = [NSBundle mainBundle]; 
NSString *audioPath = [audioBundle pathForResource:audioFileNames ofType:@"txt"]; 
NSString *audioContent = [NSString stringWithContentsOfFile:audioPath encoding:NSUTF8StringEncoding error:nil]; 
// ===========Make an array using audioContent============= 
NSArray *audioFiles = [audioContent componentsSeparatedByString:@"\n"]; 

给出使用数组来获取文件名:

NSURL *no01URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[audioFiles objectAtIndex:audioCount]] ofType:@"aiff"]]; 

如果你的文件名是像音频1,音频2 ..没有必要的使用所有这些。就在类似URL改变:

NSURL *no01URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"audio%d",audioCount] ofType:@"aiff"]]; 
+0

嗨Anusha,谢谢你的建议。我更喜欢Plist而不是纯文本文件,因为我可能需要将其他属性与每个音频剪辑相关联。另外,在上面的示例中,audioCount变量的位置/方式将如何定义? – MightyMeta

+0

@MightyMeta在你的上面的代码中直接使用audio1,audio2等。如果你把它放在一个循环中,你可以把这个循环计数作为audioCount。 –