【问题标题】:iOS background Audio Stream (m3u) doesn't work on device (but does in simulator)iOS后台音频流(m3u)在设备上不起作用(但在模拟器中)
【发布时间】:2014-06-18 01:25:04
【问题描述】:

我有一个从 url 加载 m3u 的 UIWebView:

NSString *url = @"http://141.217.20.38:8000/live.m3u";
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];

接着是:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

这适用于模拟器,但是当我通过 Xcode(使用开发配置文件)将它安装在设备 (iPhone 5) 上时,音频不会在后台播放。

我不确定我错过了什么,但我们将不胜感激。

【问题讨论】:

    标签: ios audio background m3u


    【解决方案1】:

    所以,看起来还需要创建一个 AVSession。我添加了以下代码,现在它在设备上的工作方式与在模拟器中相同

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                             error:&setCategoryError];
    if (!ok) {
        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }
    

    【讨论】:

      【解决方案2】:

      beginBackgroundTaskWithExpirationHandler: 用于执行冗长的任务,而不是让您的应用程序在后台运行。

      您需要在info.plist 中注册您的应用后台模式,将后台模式(UIBackgroundModes) 设置为audio。如App States and Multitasking中所述

      【讨论】:

      • 我有应用程序使用所需背景模式中设置的 AirPlay 播放音频或流式传输音频/视频。
      • 没有UIBackgroundModes AirPlay,你可以选择audio, location, voip, fetch, remote-notification, newsstand-content, external-accessory, @987654和bluetooth-peripheral
      • 我假设这是 Xcode 在后台模式下选中“音频和 Airplay”复选框后设置的:Screenshot
      • 我将音频添加到列表中,但我仍然得到相同的行为:背景音频在模拟器中有效,但在设备上无效。
      • info.plist的实际内容是<key>UIBackgroundModes</key> <array> <string>audio</string> <string>remote-notification</string> </array>
      猜你喜欢
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多