为了账号安全,请及时绑定邮箱和手机立即绑定

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

/ 猿问

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

SSM
BIG阳 2019-12-12 14:10:57

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


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

        }

 


查看完整描述

3 回答

?
慕神8447489

您需要设置应用程序功能后台模式(音频和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.2


do {

    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)

}



查看完整回答
反对 回复 2019-12-13
?
MMTTMM

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]

并且不要忘记在后台模式下启用音频和播放 



查看完整回答
反对 回复 2019-12-13
?
慕仙森

只粘贴在viewDidload上

    let path = Bundle.main.path(forResource:"Bismallah", ofType: "mp3")


    do{

        try playerr = AVAudioPlayer(contentsOf: URL(fileURLWithPath: path!))

    } catch {

        print("File is not Loaded")

    }

    let session = AVAudioSession.sharedInstance()

    do{

        try session.setCategory(AVAudioSessionCategoryPlayback)

    }

    catch{

    }


    player.play()



查看完整回答
反对 回复 2019-12-13

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信