转到另一个视图控制器后恢复音乐

时间:2017-09-11 14:00:05

标签: ios swift avfoundation

我正在尝试构建音乐应用。当我留在我的音乐视图控制器中时,音乐正在播放。我还设置了音乐背景模式。音乐正在后台模式中播放,但是当我转到另一个视图控制器时。音乐重新开始。问题是什么?

这是我尝试的内容

  //this is global variable 

    var player : AVPlayer?
    var playerItem:AVPlayerItem?

override func viewDidLoad() {
            super.viewDidLoad()
           musicplayer()
           play()
    }



 func musicplayer(){

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
            print("AVAudioSession Category Playback OK")
            do {
                try AVAudioSession.sharedInstance().setActive(true)

                print("AVAudioSession is Active")

            } catch let error as NSError {
                print(error.localizedDescription)
            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }


    }

这是我的玩家游戏

    func play(){
    let musicUrl = totalAlbumList[myIndex].audiofile
            if let mm = musicUrl {
                var url: URL!
                url = URL(string: mm)
                playerItem = AVPlayerItem(url: url)
                player = AVPlayer(playerItem: playerItem)
                player?.volume = 1.0
                player?.rate = 1.0
                player?.play()

}

1 个答案:

答案 0 :(得分:0)

如果您想在所有课程中播放音乐,只需将您的AVPlayer和与播放器相关的所有代码放在单件类中。

后台模式仅在应用程序处于后台模式且您的UIViewController正在运行时才有帮助。 使用下面的课程来播放音乐。

//
//  MusicPlayer.swift
//  MusicPlayer
//
//  Created by Lakhwinder Singh on 11/09/17.
//  Copyright © 2017 lakh. All rights reserved.
//

import UIKit
import AVFoundation

class MusicPlayer: NSObject {

    //Constant varialbe of class
    static let player = MusicPlayer()

    //this is global variable
    var player : AVPlayer?
    var playerItem:AVPlayerItem?

    func initPlayer(){
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
            print("AVAudioSession Category Playback OK")
            do {
                try AVAudioSession.sharedInstance().setActive(true)

                print("AVAudioSession is Active")

            } catch let error as NSError {
                print(error.localizedDescription)
            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }

    func playMusic(_ musicUrl: String?) {
        if let mm = musicUrl {
            var url: URL!
            url = URL(string: mm)
            playerItem = AVPlayerItem(url: url)
            player = AVPlayer(playerItem: playerItem)
            player?.volume = 1.0
            player?.rate = 1.0
            player?.play()
        }
    }

}

只需在AppDelegate中调用以下代码即可初始化播放器。

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

并在课堂上使用此代码。

MusicPlayer.player.playMusic(totalAlbumList[myIndex].audiofile)