2012-12-03 3 views
1

각각 50 개의 AVAudioPlayers를 사용하여 각기 별도의 오디오 파일을로드하고 있습니다. 파일은 고정되어 있으며 앱 번들 내에 있습니다. 그것들은 나중에 앱에서 발생하는 이벤트에 의해 트리거됩니다. 현재 내가 지금처럼, 각 플레이어 인스턴스의 생성을 하드 코딩 해요 :로딩 AVAudioPlayer 파일 Plist에서 가져온 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... 

을 분명히 이것은 코딩 연습 labourious 나쁜입니다. 대신 Plist에있는 파일 목록을 가져 와서 위 코드의 한 예를 채우는 변수에로드하고 싶습니다. 이 방법으로 DRY하는 방법을 배우고 싶지만 Plists, 배열, 사전 등에서 데이터를로드하는 데 제한된 경험이 있습니다.

관련성이있는 방향으로 나를 가리킬지라도 지도 시간.

감사합니다.

답변

1

: audioPlayerArray 내가 나중에 얻을 수 있다는 것을 의미에서 생성 된 선수 퍼팅

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

} 

.

4

'새 줄'구분 기호를 사용하여 텍스트 파일에 오디오 파일 이름을 넣기 만하면됩니다. 그리고 파일을 읽고 이름을 배열에 저장하십시오.

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

아래에 주어진 파일 이름을 얻을 수있는 배열을 사용하여 URL 배열에서 동적으로 파일 이름을 만들려면 :

파일 이름은 AUDIO1처럼, AUDIO2
NSURL *no01URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[audioFiles objectAtIndex:audioCount]] ofType:@"aiff"]]; 

경우 .. 전혀 필요 없음 이 모든 것을 사용합니다. 그냥 같은 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로 취할 수 있습니다. –