【问题标题】:loading AVAudioPlayer file URLs from a Plist从 Plist 加载 AVAudioPlayer 文件 URL
【发布时间】:2012-12-31 08:21:18
【问题描述】:

我正在使用大约 50 个 AVAudioPlayer,每个都加载一个单独的音频文件。这些文件是固定的并且在应用程序包中。它们由稍后在应用程序中的事件触发。目前我正在硬编码每个播放器实例的创建,如下所示:

//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 中拥有文件列表,并将这些文件加载​​到仅填充上述代码示例的变量中。我想学习如何对此进行 DRY,但从 Plist、数组、字典等加载数据的经验有限。

感谢您提供任何帮助,即使是为了让我了解相关教程的方向。

谢谢。

【问题讨论】:

    标签: objective-c ios plist avaudioplayer


    【解决方案1】:

    您可以使用“换行符”分隔符简单地将音频文件名放在文本文件中。并读取文件并将名称存储在数组中。要创建 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"]];  
    

    如果您的文件名像 audio1,audio2.. 则无需使用所有这些。只需更改 URL,例如:

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

    【讨论】:

    • 您好 Anusha,感谢您的建议。不过,我更喜欢 Plist 而不是纯文本文件,因为我可能需要将其他属性与每个音频剪辑相关联。另外,根据您上面的示例,audioCount 变量将在何处/如何定义?
    • @MightyMeta 在上面的代码中直接使用audio1、audio2等。如果你把它放在一个循环中,你可以把循环计数作为audioCount。
    【解决方案2】:

    其他需要这样做的人都可以,这就是我最终得到的结果:

    //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 中意味着我可以稍后再使用它们。

    【讨论】:

      猜你喜欢
      • 2013-03-17
      • 2012-06-06
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多