其他播放时停止播放直播mp3?

时间:2014-04-19 07:22:12

标签: ios avaudioplayer avaudiosession

我目前正在制作直播音乐应用,并且我已经完成了所有按钮的工作和播放声音。

然而,如果正在播放一个声音,当我按下另一个按钮,而不是停止原始声音时,它只是播放它,请帮我解决这个问题?请帮忙!

非常感谢

- (IBAction)Play1:(id)sender {

    NSString *stream = @".mp3"
    ;

    NSURL *url = [NSURL URLWithString:stream];

    NSURLRequest *urlrequest = [NSURLRequest requestWithURL: url];

    [Webview1 loadRequest:urlrequest];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    [audioPlayer play];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


}

1 个答案:

答案 0 :(得分:1)

创建布尔值并在流播放时设置为true。之后当你想要播放另一个流控制你的bool值时,如果是真的话,首先停止audioPlayer并用你发挥的新网址改变audioplayer contentUrl。这就是全部< / p>

@property (nonatomic, assign, getter=isPlaying) BOOL playing; 

//first set playing NO , after that  

- (void)playAnotherStream:(NSURL *)streamUrl
{
    if (isPlaying) {
        [self.audioPlayer stop];
        self.playing = NO;
    }
    [self.player setContentURL:streamUrl];
    [self. audioPlayer play];
    self.playing = YES;
}