1

私は、それぞれが個別のオーディオ ファイルをロードする約 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 に格納し、これらを変数にロードして、上記のコードの 1 つの例を設定したいと思います。これで DRY になる方法を学びたいのですが、Plist、配列、辞書などからデータをロードする経験が限られています。

関連するチュートリアルの方向性を示すことであっても、どんな助けでも大歓迎です。

ありがとうございました。

4

2 に答える 2

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"]];  

ファイル名が audio1,audio2.. のような場合、これらすべてを使用する必要はありません。次のように URL を変更するだけです。

NSURL *no01URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"audio%d",audioCount] ofType:@"aiff"]];  
于 2012-12-04T05:15:54.967 に答える
1

これを行う必要がある他の人にとってはOKです。これが私が最終的に得たものです:

//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 に入れるということは、後でそれらを取得できることを意味します。

于 2012-12-31T00:21:18.910 に答える