【发布时间】: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