Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在AVAudio流中更改轨迹_Objective C_Ios7_Avfoundation - Fatal编程技术网

Objective c 在AVAudio流中更改轨迹

Objective c 在AVAudio流中更改轨迹,objective-c,ios7,avfoundation,Objective C,Ios7,Avfoundation,我有一个程序,使用分段控制来控制音频流使用AVFoundation音频功能。然而,它看起来非常混乱,每次我想更改音乐曲目时都必须重新实例化它 我怎样才能使它更优雅、更干净?我真的需要为每一个播放新曲目的实例创建一个新播放器吗 - (IBAction)musicChoice:(UISegmentedControl *)segmentedControl { switch(segmentedControl.selectedSegmentIndex) { { case 0

我有一个程序,使用分段控制来控制音频流使用AVFoundation音频功能。然而,它看起来非常混乱,每次我想更改音乐曲目时都必须重新实例化它

我怎样才能使它更优雅、更干净?我真的需要为每一个播放新曲目的实例创建一个新播放器吗

- (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的

我还应该说,目前所有的东西都在编译和运行,我只是想知道是否有更好的方法来做事情。