34

我已经完成了我的作业......一直在这里阅读,文档,谷歌搜索,stackoverflowing......但是当用户让应用程序进入后台时,我仍然没有让我的声音坚持下去。

到目前为止我所做的:将 UIBackgroundModes、音频添加到 plist 文件中。

首先这段代码:

radioAudio = [[AVAudioSession alloc] init];
[radioAudio setCategory:AVAudioSessionCategoryPlayback error:nil];
[radioAudio setActive:YES error:nil];

然后这个:

NSString *radioURL = @"http://xxx.xxx.xxx/radio.m3u";
radioPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:radioURL]] retain];

但是一旦用户点击主页按钮,我的声音就消失了。

我也找到了这个,但还没有添加,因为我读过的一些东西说它是不需要的;

newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
     if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
        [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
bgTaskId = newTaskId;

现在我不知道我应该去哪里让我的 AVPlayer 让收音机唱歌,而用户在手机上做其他事情。我正在使用 4.2 的 iPhone 4 上对此进行测试。为 4.0 构建它。

有人对我应该做什么有任何建议吗?

4

7 回答 7

52

使用 Swift 4 更新 IOS 11.2:

现在,如果您使用AVPlayer播放音乐文件,您还应该配置MPNowPlayingInfoCenter.default()在锁定屏幕上显示正在播放的信息。

下面的代码将在屏幕上显示正在播放的控件,但它无法响应任何命令。

如果您还想控制工作,您应该在这里查看苹果的示例项目:https ://developer.apple.com/library/content/samplecode/MPRemoteCommandSample/Introduction/Intro.html#//apple_ref/doc/uid/TP40017322

Apple 示例代码涵盖了所有内容,但我觉得它令人困惑。

如果您想在锁定屏幕上播放声音并显示控件,这些步骤就可以了。

重要提示:如果您使用AVPlayer播放声音。如果您使用某些第三方库来生成声音或播放声音文件,您应该阅读代码中的注释。此外,如果您使用的是 ios 模拟器 11.2,您将无法在锁定屏幕上看到任何控件。您应该使用设备来查看它的工作情况。


1- 选择项目-> 功能-> 设置背景模式-> 勾选音频、AirPlay 和画中画

ios 11 后台音频设置

2- AppDelegate.swift文件应如下所示:

import UIKit

import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        // Override point for customization after application launch.

        do
        {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

         //!! IMPORTANT !!
         /*
         If you're using 3rd party libraries to play sound or generate sound you should
         set sample rate manually here.
         Otherwise you wont be able to hear any sound when you lock screen
         */
            //try AVAudioSession.sharedInstance().setPreferredSampleRate(4096)
        }
        catch
        {
            print(error)
        }
        // This will enable to show nowplaying controls on lock screen
        application.beginReceivingRemoteControlEvents()

        return true
    }
}

3- ViewController.swift应该是这样的:

import UIKit

import AVFoundation
import MediaPlayer

class ViewController: UIViewController
{

    var player : AVPlayer = AVPlayer()

    override func viewDidLoad()
    {
        super.viewDidLoad()


        let path = Bundle.main.path(forResource: "music", ofType: "mp3")
        let url = URL(fileURLWithPath: path!)

        // !! IMPORTANT !!
        /*
            If you are using 3rd party libraries to play sound 
            or generate sound you should always setNowPlayingInfo 
            before you create your player object.

            right:
            self.setNowPlayingInfo()
            let notAVPlayer = SomePlayer()

            wrong(You won't be able to see any controls on lock screen.):
            let notAVPlayer = SomePlayer()
            self.setNowPlayingInfo()
         */

        self.setNowPlayingInfo()
        self.player = AVPlayer(url: url)

    }


    func setNowPlayingInfo()
    {
        let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
        var nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo ?? [String: Any]()

        let title = "title"
        let album = "album"
        let artworkData = Data()
        let image = UIImage(data: artworkData) ?? UIImage()
        let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: {  (_) -> UIImage in
            return image
        })

        nowPlayingInfo[MPMediaItemPropertyTitle] = title
        nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album
        nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork

        nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo
    }

    @IBAction func startPlayingButtonPressed(_ sender: Any)
    {
        self.player.play()
    }

旧答案 IOS 8.2:

帕特里克的回答是完全正确的。

但我要写下我为ios 8.2所做的事情:

我添加了我的应用程序的 info.plist 所需的背景模式,如下所示:

在此处输入图像描述

在我的AppDelegate.h中,我添加了这些导入:

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

然后在我的AppDelegate.m中,我编写了应用程序 didFinishLaunchingWithOptionsthis,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

    return YES;
}

现在即使屏幕被锁定,应用程序也会继续播放音乐 :)

于 2015-03-19T09:04:16.957 回答
42

有同样的问题,但找到了解决方案..

看这里:https ://devforums.apple.com/message/395049#395049

以上链接内容:


替换APPNAME为您自己的应用名称!

我在 iOS 4.2.1

编辑:到目前为止使用 iOS5 + 6 + 7 beta

添加UIBackgroundModes,APPNAME-Info.plist选择 App 播放音频

然后将AudioToolBox框架添加到文件夹框架中。

APPNAMEAppDelegate.h添加:

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

所以它看起来像这样:

...
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
...

APPNAMEAppDelegate.m添加以下内容:

// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

/* Pick any one of them */
// 1. Overriding the output audio route
//UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
//AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

// 2. Changing the default output audio route
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

进入

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

但在这两行之前:

[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

构建您的项目并查看是否有任何错误,如果没有,请尝试在模拟器的设备上调试,它可以在模拟器上出错。

希望这可以帮助其他有同样问题的人..

于 2011-03-09T22:48:34.497 回答
10

通过将以下内容添加到我的applicationDidFinishLaunching

// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];    
// Allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
于 2011-01-22T23:24:19.317 回答
6

iOS 9 斯威夫特

您只需要将以下内容添加到您的 didFinishLaunchingWithOptions

do  {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
    //TODO
}
于 2015-12-16T18:46:36.423 回答
1

您有一个很好的 GPS 后台应用程序示例,即使在后台也可以播放声音:

http://www.informit.com/articles/article.aspx?p=1646438&seqNum=5

在此示例中,使用了 AudioToolbox。

我自己做了一个测试,它可以工作:创建一个简单的项目来监控 GPS 柱 ( UIBackgroundModes location) 和每个 x 接收到的位置,使用播放声音

AudioServicesPlaySystemSound(soundId);

然后,如果您将audio其作为您的一部分UIBackgroundModes,即使应用程序不再处于前台,声音也会播放。

我做了这样的测试,它工作正常!

(我没有设法让它与 AVPlayer 一起工作,所以我回退到 AudioToolbox)

于 2011-03-18T15:37:35.087 回答
1

我有同样的问题,我在 Apple Docs 中找到了解决方案:https ://developer.apple.com/library/ios/qa/qa1668/_index.html

问:当我的应用程序在后台使用 AV Foundation 时,如何确保我的媒体继续播放?

答:您必须声明您的应用在后台播放可听内容,并为您的音频会话分配适当的类别。另请参阅视频媒体的特殊注意事项

于 2016-03-01T15:41:09.573 回答
0

Swift 5工作

以下是我为在 avPlayer 正在播放的视频中播放背景音乐所做的 4 件事。我遵循了@AndriySavran 的答案中的 Apple方向链接以及此Apple 链接以及其他一些内容。

1-在 AppDelegate 中didFinishLaunching我放了:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        do {
            try AVAudioSession.sharedInstance().setCategory( .playback, mode: .moviePlayback, options: [.mixWithOthers, .allowAirPlay])
            print("Playback OK")
            try AVAudioSession.sharedInstance().setActive(true)
            print("Session is Active")
        } catch {
            print(error)
        }

    // whatever other code that you use ...

    return true
}

2- 遵循@LeoDabus 的这个答案。在您的Signing & Capabilties>Background Modes中(如果不存在背景模式,则从 Capabilites 中选择它)> 勾选Audio, Airplay, and Picture in Picture

3-在具有您的视图控制器中AVPlayer,添加.didEnterBackgroundNotification.willEnterForegroundNotification通知

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(appDidEnterBackground),
                                           name: UIApplication.didEnterBackgroundNotification, object: nil)
    
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(appWillEnterForeground),
                                           name: UIApplication.willEnterForegroundNotification, object: nil)

}

4-对于选择器方法,添加 Apple 链接中的代码

@objc func appDidEnterBackground() {

    playerLayer?.player = nil
}

@objc func appWillEnterForeground() {
    
    playerLayer?.player = self.player
}
于 2021-11-17T23:10:40.753 回答