0
세그먼트 화 된 컨트롤을 사용하여 AVFoundation 오디오 기능을 사용하여 오디오 스트림을 제어하는 프로그램이 있습니다. 그러나 너무 지저분 해 보입니다. 음악 트랙을 바꿀 때마다 다시 인스턴스화해야합니다.AVAudio 스트림의 트랙 변경
어떻게하면 더 우아하고 깨끗하게 만들 수 있습니까? 새 트랙을 재생할 때마다 새로운 플레이어를 만들어야합니까?
- (IBAction)musicChoice:(UISegmentedControl *)segmentedControl {
switch(segmentedControl.selectedSegmentIndex)
{
{ case 0:
[self.player pause];
currentMusic = arc4random_uniform(upperBound);
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: _musicToPlay[currentMusic]
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
self.player = newPlayer;
[_player prepareToPlay];
[_player setDelegate: self];
[self.player play];
break; }
{ case 1:
[self.player pause];
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Track1"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
self.player = newPlayer;
[_player prepareToPlay];
[_player setDelegate: self];
[self.player play];
break; }
{ case 2:
[self.player pause];
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Track2"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
self.player = newPlayer;
[_player prepareToPlay];
[_player setDelegate: self];
[self.player play];
break; }
}
}
다음은 음악 트랙을 순환하는 데 사용하는 NSArray입니다.
_musicToPlay = @[@"Track1",@"Track2",@"Track3",@"Track4",@"Track5"];
이미이 IBAction를 도달 시간에 의해 음악이 실행되고, 이는 내가 미리
[self.player pause];
감사를 사용하는 이유입니다!
나는 또한 모든 현재 컴파일하고 실행하는 것이, 난 그냥 궁금 해서요 말을해야하는 경우 일을하는 더 좋은 방법이 있습니다. –