Objective c 在AVAudio流中更改轨迹
我有一个程序,使用分段控制来控制音频流使用AVFoundation音频功能。然而,它看起来非常混乱,每次我想更改音乐曲目时都必须重新实例化它 我怎样才能使它更优雅、更干净?我真的需要为每一个播放新曲目的实例创建一个新播放器吗Objective c 在AVAudio流中更改轨迹,objective-c,ios7,avfoundation,Objective C,Ios7,Avfoundation,我有一个程序,使用分段控制来控制音频流使用AVFoundation音频功能。然而,它看起来非常混乱,每次我想更改音乐曲目时都必须重新实例化它 我怎样才能使它更优雅、更干净?我真的需要为每一个播放新曲目的实例创建一个新播放器吗 - (IBAction)musicChoice:(UISegmentedControl *)segmentedControl { switch(segmentedControl.selectedSegmentIndex) { { case 0
- (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"];
我已经有音乐运行的时候,这个iAction是达到,这就是为什么我使用
[self.player pause];
提前谢谢 你可以试着用一个来搞乱,如果你事先知道一些流,你可以搞乱播放列表的顺序来改变下一个要播放的文件
否则,如果您需要在流之间有更短的停机时间,您可以尝试一个较低级别的框架,例如或CoreAudio的我还应该说,目前所有的东西都在编译和运行,我只是想知道是否有更好的方法来做事情。