Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我无法从锁定屏幕控制我的音频应用程序_Ios_Swift_Avfoundation_Avkit - Fatal编程技术网

Ios 我无法从锁定屏幕控制我的音频应用程序

Ios 我无法从锁定屏幕控制我的音频应用程序,ios,swift,avfoundation,avkit,Ios,Swift,Avfoundation,Avkit,我已经创建了一个从URL播放广播流的应用程序,它在应用程序位于后台时播放音频,但在锁定的屏幕上没有控制器-我只需要一个应用程序名和播放/暂停按钮。我在应用程序设置中检查了背景模式下的音频和播放。有什么想法吗?提前感谢您的帮助。这是我的密码: import UIKit import AVKit class ViewController: UIViewController { var player : AVPlayer! var dict = NSDictionary(

我已经创建了一个从URL播放广播流的应用程序,它在应用程序位于后台时播放音频,但在锁定的屏幕上没有控制器-我只需要一个应用程序名和播放/暂停按钮。我在应用程序设置中检查了背景模式下的音频和播放。有什么想法吗?提前感谢您的帮助。这是我的密码:

import UIKit
import AVKit


class ViewController: UIViewController {
    
    var player : AVPlayer!
    var dict = NSDictionary()
    
    
    //@IBOutlet weak var ArtistLabel: UILabel!
    
    
    @IBAction func playButtonPressed(_ sender: UIButton){
        let url = "https://radiopromil.online/radio/8000/radio.mp3"
        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)
        }
            player = AVPlayer(url: URL(string: url)!)
            player.volume = 1.0
            player.rate = 1.0
            player.play()
    }
    
    
    
    @IBAction func stopButtonStopped(sender: UIButton) {
        player.pause()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        overrideUserInterfaceStyle = .light
       
    }
   

}




您需要导入媒体播放器,然后进行设置

import UIKit
import AVKit
import MediaPlayer

class ViewController: UIViewController {

var player : AVPlayer!
var dict = NSDictionary()


//@IBOutlet weak var ArtistLabel: UILabel!


@IBAction func playButtonPressed(_ sender: UIButton){
    let url = "https://radiopromil.online/radio/8000/radio.mp3"
    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)
    }
        player = AVPlayer(url: URL(string: url)!)
        player.volume = 1.0
        player.rate = 1.0
        player.play()
}



@IBAction func stopButtonStopped(sender: UIButton) {
    player.pause()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    overrideUserInterfaceStyle = .light
    setupRemoteCommandCenter()
   
}
func setupRemoteCommandCenter() {
    // Get the shared MPRemoteCommandCenter
    let commandCenter = MPRemoteCommandCenter.shared()
    
    // Add handler for Play Command
    commandCenter.playCommand.addTarget { event in
        return .success
    }
    
    // Add handler for Pause Command
    commandCenter.pauseCommand.addTarget { event in
        return .success
    }
    
    // Add handler for Next Command
    commandCenter.nextTrackCommand.addTarget { event in
        return .success
    }
    
    // Add handler for Previous Command
    commandCenter.previousTrackCommand.addTarget { event in
        return .success
    }
}



}

这应该可以奏效

很多人都感谢你的提示,但它仍然不起作用。你知道为什么吗?