2014-04-19 60 views
0

我目前正在制作一个直播音乐应用程序,并且我已经有了所有的按钮可以工作并播放来自它们的声音。停止播放直播mp3而其他播放?

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

非常感谢

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

创建布尔值,并设置为true时流playing.after,当你要玩另一个流控制你的bool值,如果这是真的,第一站audioPlayer和改变audioplayer的contentURL与新的网址,你play.that所有

@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; 
} 
+0

非常感谢,对不起,我是新的Objective-C,我应该添加到每个流播放?谢谢 – mimi