ios - 音频流播放: unable to change play/stop button image

标签 ios objective-c audio ios6 uibutton

我正在为 iOS 6 开发一个带有 XCode 4.5 的广播流应用程序,主要使用 Storyboard 。
我成功使它能够在后台播放。因此,无论我从我的应用程序走到哪里,无论是到其他选项卡,还是在单击模拟器上的主页按钮后,它都会继续播放。
我正在使用 Matt Gallagher 的音频流媒体,我将其包含在下面的应用程序委托(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];
    }
}

我遇到的问题是,当我单击播放按钮时,它会启动音频流媒体,但不会变成加载按钮或停止按钮。这使我无法停止音频,因为按钮图像没有更改。自从我放了 NSLog 里面播放状态更改:方法,我知道播放状态确实发生了变化。好像是 setButtonImagedNamed: 方法不射击。有什么想法吗?

最佳答案

请看我的回答: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 中设置了图像,因此图像会根据图像的状态自动更改

关于ios - 音频流播放: unable to change play/stop button image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628116/

相关文章:

iphone - 如何在谓词是方法的情况下执行 NSArray filteredArrayUsingPredicate?

objective-c - 许多文件的自动音频水印

ipad - iPad HTML5加载多个音频文件

ios - PresentViewController 故障排除

iphone - 条件不满足时退出函数

objective-c - 从类的方法显示 UILabel

objective-c - iOS 圆弧 : OR operator on ManagedObject pointers

javascript - Android WebView 使用 javascript 播放音频

ios - 无法将 NSDictionary 值转换为 json

ios - 当在 View 中注入(inject)实体时,SwiftUI 预览版无法使用核心数据