音频流播放:无法更改播放/停止按钮图像

时间:2014-05-13 09:57:17

标签: ios objective-c audio ios6 uibutton

我正在开发一款适用于iOS 6的XCode 4.5的无线电流媒体应用程序,主要使用故事板。 我成功地让它能够在后台播放。因此,无论我从我的应用程序,无论是其他标签,还是单击模拟器上的主页按钮,它都会继续播放。 我使用的是Matt Gallagher的音频流,我将其包含在我的app delegate .m文件中

#pragma mark - audio streaming

    - (void)playAudio:(indoRadio *)np withButton:(NSString *)currentButton
{
    if ([currentButton isEqual:@"playbutton.png"])
    {
        [self.nowplayingVC.downloadSourceField resignFirstResponder];

        [self createStreamer:np.URL];
        [self setButtonImageNamed:@"loadingbutton.png"];
        [audioStreamer start];
    }
    else
    {
            [audioStreamer stop];
    }
}

- (void)createStreamer:(NSString *)url
{
    if (audioStreamer)
    {
        return;
    }

    [self destroyStreamer];

    NSString *escapedValue = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(nil,(CFStringRef)url,NULL,NULL,kCFStringEncodingUTF8);

    NSURL *streamurl = [NSURL URLWithString:escapedValue];
    audioStreamer = [[AudioStreamer alloc] initWithURL:streamurl];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playbackStateChanged:)
     name:ASStatusChangedNotification
     object:audioStreamer];
}

- (void)playbackStateChanged:(NSNotification *)aNotification
{
    NSLog(@"playback state changed? %d",[audioStreamer isWaiting]);
    if ([audioStreamer isWaiting])
    {
        [self setButtonImageNamed:@"loadingbutton.png"];
    }
    else if ([audioStreamer isPlaying])
    {
        [self setButtonImageNamed:@"stopbutton.png"];
    }
    else if ([audioStreamer isIdle])
    {
        [self destroyStreamer];
        [self setButtonImageNamed:@"playbutton.png"];
    }
}

- (void)destroyStreamer
{
    if (audioStreamer)
    {
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:ASStatusChangedNotification
         object:audioStreamer];

        [audioStreamer stop];
        audioStreamer = nil;
    }
}

- (void)setButtonImageNamed:(NSString *)imageName
{
    if (!imageName)
    {
        imageName = @"playButton";
    }
    self.nowplayingVC.currentImageName = imageName;

    UIImage *image = [UIImage imageNamed:imageName];
    [self.nowplayingVC.playBtn.layer removeAllAnimations];
    [self.nowplayingVC.playBtn setImage:image forState:0];

    if ([imageName isEqual:@"loadingbutton.png"])
    {
        [self.nowplayingVC spinButton];
    }
}

我遇到的问题是,当我点击播放按钮时,它会启动音频流,但不会变成加载按钮或停止按钮。这使我无法停止音频,因为按钮图像没有改变。由于我在 playbackStateChanged:方法中放置了 NSLog ,我知道播放状态确实发生了变化。似乎 setButtonImagedNamed:方法没有触发。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

请查看我的回答:Disabled buttons not changing image in

对于按钮,有正常,选定,突出显示和禁用状态。

因此,在您的情况下,您必须处理正常和选定状态。

在xib中,将图像设置为正常和选定状态的按钮。

现在在playAudio的功能中,而不是检查图像名称,检查按钮状态如下:

- (void)playAudio:(indoRadio *)np withButton:(NSString *)currentButton
{
    if (playButton.selected)
    {
        //Stop the streaming

        //set selection NO
        [playButton setSelected:NO];
    }
    else
    {
        //Start the streaming

        //set selection YES
        [playButton setSelected:YES];
    }
}

playButton是播放按钮的IBOutlet

由于您已在xib中设置图像,因此图像会根据图像状态自动更改

答案 1 :(得分:0)

如果您以编程方式添加了playBtn,请检查按钮是否被声明为弱或其在某处被释放。如果它很弱,那就强搞它。

如果按钮位于nib文件中,则问题出在IBOutlet中。正确地重做连接。

答案 2 :(得分:0)

事实证明,该代表并不知道现在正在播放的VC正在调用哪个。 @ sarp-kaya关于Calling a UIViewController method from app delegate的问题激发了我的灵感。 我实际上已将此代码放在我的视图控制器viewDidLoad中:

myAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;

但我忘记添加这一行:

appDelegate.nowplayingVC = self;

所以,我需要做的就是添加一行,现在就可以了。好吧,因为没有注意到这样一个基本的东西,我真傻。但感谢您的帮助:D