ios - 单击后退按钮iOS 6时如何继续播放音频

标签 ios objective-c uitableview audio

我正在使用适用于iOS 6的XCode 4.5开发 radio 广播应用程序,主要使用 Storyboard 。登录 View 后,它将显示一个标签栏 Controller ,其中包含3个标签: channel 列表,关于,新闻。登录后,它将默认打开“ channel 列表”选项卡,其中显示了广播 channel 列表(我正在使用uitableview填充该列表)。然后单击一个 channel 将带您到另一个 View Controller (在同一选项卡菜单中),显示“正在播放” View ,您需要在其中单击播放按钮以启动流媒体。

在这里,我实现了Matt Gallagher的音频流媒体。我已经在Info.plist上添加了“必需的背景模式”键和“应用程序播放音频”值。我还在我的appDelegate的didFinishLaunchinWithOptions:方法中添加了以下代码

UIDevice *thisDevice = [UIDevice currentDevice];
if([thisDevice respondsToSelector:@selector(isMultitaskingSupported)]
   && thisDevice.multitaskingSupported)
{
    __block UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
        /* just fail if this happens. */
        NSLog(@"BackgroundTask Expiration Handler is called");
        [application endBackgroundTask:backgroundTask];
    }];
}

到目前为止,在选择了一个 channel 之后,我已经在模拟器中运行它,然后在“正在播放” View 中点击“播放”按钮,即使我移至“关于”或“新闻”选项卡,它仍然会播放音频。单击“主页”时也没有问题。
我的问题是,当我单击“正在播放” View 上方的导航栏的“后退按钮”(意味着返回到“ channel ”列表)时,或者当我单击最上方的后退按钮(意味着返回到登录 View )时,音频停止播放。当我尝试打开相同的 channel 时,它不会恢复流式传输,因此我需要单击“播放”按钮以重新开始播放。

我如何才能做到,只要用户登录,在导航回 channel 列表或登录 View 时,音频就会继续播放(除非用户单击其他 channel )?
我猜想这与UITableView有关,例如保持选定单元格的状态。但是我不知道该怎么做。所以请,我感谢您的帮助:)

最佳答案

因此,我遵循@matt的关于将音频流媒体放入appDelegate的建议。我将下面的代码从“正在播放” View Controller 移至我的appDelegate.m文件

#pragma mark - audio streamer

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

        [self createStreamer:np.URL];
        [self.nowplayingVC 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
{
    if ([audioStreamer isWaiting])
    {
        [self.radioDetailVC setButtonImageNamed:@"loadingbutton.png"];
    }
    else if ([audioStreamer isPlaying])
    {
        [self.nowplayingVC setButtonImageNamed:@"stopbutton.png"];
    }
    else if ([audioStreamer isIdle])
    {
        [self destroyStreamer];
        [self.nowplayingVC setButtonImageNamed:@"playbutton.png"];
    }
}

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

        [audioStreamer stop];
        audioStreamer = nil;
    }
}

当我单击播放按钮时,音频流媒体在后台运行良好。无论我在应用程序内外的任何地方,它仍在播放。

关于ios - 单击后退按钮iOS 6时如何继续播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23623212/

相关文章:

ios - 显示表格 View 所有单元格的详细信息

iphone - UITableView设计

ios - 无法在 "My Mac 64-bit"上运行 sim

iphone - 在 willAnimateRotationToInterfaceOrientation 中禁用动画?

ios - CoreGraphics 矩形渐变

iphone - 如果更改编辑中的文本,UITextField 会崩溃

objective-c - 如何模仿 App Store.app UI

objective-c - 用不同线宽的部分绘制连续的CGPath

ios - 单元格数量有限的 UITableView

ios - 使用 MKAnnotation 继续下一个 View Controller