【问题标题】:Audio streaming playback: unable to change play/stop button image音频流播放:无法更改播放/停止按钮图像
【发布时间】:2014-05-13 09:57:17
【问题描述】:

我正在使用 XCode 4.5 for iOS 6 开发一个广播流应用程序,主要使用情节提要。 我成功使它能够在后台播放。因此,无论我从我的应用程序走到哪里,无论是到其他选项卡,还是在单击模拟器上的主页按钮后,它都会继续播放。 我正在使用 Matt Gallagher 的音频流媒体,我将其包含在下面的应用程序委托 .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: 方法似乎没有触发。有什么想法吗?

【问题讨论】:

  • 你是否尝试在 setButtonImageNamed 方法中设置断点??检查 playBtn 是否不为零。
  • @Adithya 是的,我尝试检查 self.radioDetailVC.playBtn 是否 == nil,它返回 TRUE。这是否意味着我以错误的方式处理按钮?

标签: ios objective-c audio ios6 uibutton


【解决方案1】:

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

【讨论】:

  • 事实上@svrushal,以前我把上面问题中的代码放在视图控制器中,它工作正常(按钮图像更改和流媒体),但因为我希望它在后台播放,即使我导航回频道列表或登录视图,我将代码移动到我的应用程序委托中。现在唯一不起作用的是 setButtonImageNamed 方法。我刚刚发现这里的 playBtn 是 nil
【解决方案2】:

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

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

【讨论】:

  • 在我将视图控制器中的 playBtn 属性设置为(非原子,保留)之前,我将其更改为(非原子,强),但仍然无法正常工作。至于 IBOutlet,我猜不是,因为它可以调用我从按钮的 IBAction 中引用的 playAudio 方法。就像我在我的问题中提到的那样,流媒体运行良好,只有每次我点击按钮时,它总是启动流媒体(而不是停止它),因为“playbutton.png”图像没有改变
【解决方案3】:

原来代理不知道我调用的是哪个 nowplayingVC。 @sarp-kaya 关于Calling a UIViewController method from app delegate 的问题启发了我。 我实际上已将此代码放在我的视图控制器 viewDidLoad 中:

myAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;

但我忘了添加这一行:

appDelegate.nowplayingVC = self;

所以,我需要做的就是添加这一行,它现在可以工作了。好吧,我太傻了,没有注意到这样一个基本的事情。但是感谢您的帮助:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多