如何在背景Swift中播放音频?

如您所见,我正在流式传输音频广播。但是当我按下主页按钮并退出应用程序时,流媒体播放停止,或者我听不到声音。如何继续在后台流式传输并从锁定屏幕收听?


ViewController.Swift


import UIKit

import AVFoundation

import MediaPlayer

import GoogleMobileAds



    class ViewController: UIViewController, GADInterstitialDelegate {


        @IBOutlet weak var exitMapButton: UIButton!

        @IBOutlet weak var radarMap: UIWebView!

        var interstitial: GADInterstitial!

        func createAndLoadInterstitial() -> GADInterstitial {

            var interstitial = GADInterstitial(adUnitID: "ca-app-pub-5378899862041789/2782958552")

            interstitial.delegate = self

            interstitial.loadRequest(GADRequest())

            return interstitial

        }


        func getAd(){

            if (self.interstitial.isReady)

            {

                self.interstitial.presentFromRootViewController(self)

                self.interstitial = self.createAndLoadInterstitial()

            }

        }

        @IBOutlet weak var ataturkButton: UIButton!

        @IBOutlet weak var sabihaButton: UIButton!

        @IBOutlet weak var esenbogaButton: UIButton!

        @IBOutlet weak var weatherButton: UIButton!

        @IBOutlet weak var statusLabel: UILabel!

        @IBOutlet weak var playButton: UIButton!

        @IBOutlet weak var webViewButton: UIButton!

        var googleBannerView: GADBannerView!

override func viewDidLoad() {

            super.viewDidLoad()

        }

class PlayerAv {

            var audioLink: String?

            var player: AVPlayer

            init(link: String) {

                self.audioLink = link

                self.player = AVPlayer(URL: NSURL(string: link))

            }

        }

        var myPlayer = PlayerAv(link: "http://www.liveatc.net/play/ltba.pls")

        var setTowerState = ""


        @IBAction func sliderValueChanged(sender: UISlider) {

            var currentValue = Float(sender.value)

            println(currentValue)

            myPlayer.player.volume = currentValue

        }

 

莫回无
浏览 783回答 3
3回答

holdtom

您需要设置应用程序功能后台模式(音频和AirPlay),并将AVAudioSession类别设置为AVAudioSessionCategoryPlayback并将其设置为活动状态从Xcode 8.2.1•Swift 3.0.2开始do {    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)    print("Playback OK")    try AVAudioSession.sharedInstance().setActive(true)    print("Session is Active")} catch {    print(error)}Xcode 10•Swift 4.2do {    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])    print("Playback OK")    try AVAudioSession.sharedInstance().setActive(true)    print("Session is Active")} catch {    print(error)}

温温酱

Xcode 10.2.1 Swift 4请在您的AppDelegate中添加以下代码func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {        do {            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [.mixWithOthers, .allowAirPlay])            print("Playback OK")            try AVAudioSession.sharedInstance().setActive(true)            print("Session is Active")        } catch {            print(error)        }        return true    }注意:-请根据需要配置选项。例如在播放视频文件时停止背景音频 options: [.allowAirPlay, .defaultToSpeaker]并且不要忘记在后台模式下启用音频和播放 
打开App,查看更多内容
随时随地看视频慕课网APP