View控制器转换中的问题?

时间:2017-07-07 08:27:23

标签: ios swift3 uiviewcontroller avplayer

我的项目存在问题。我在ViewWillAppear中使用AVPlayer播放音频并且工作正常。我在播放器中有一个文本阅读按钮,当我点击此按钮时,一个新的视图控制器即将到来并显示一些文本,但是当我按下后退按钮时,音频正在从开始播放。我想继续玩。我不知道,怎么办?我正在使用Swift3。

// PlayerViewController

import UIKit
import AVFoundation
import AVKit

class PlayerViewController: UIViewController, URLSessionDownloadDelegate {

 var audioFileURL : NSURL!


func play() {

    RemoteModel.sharedInstanceRemoteModel.audioPlayer.play()
}

func pause() {
   RemoteModel.sharedInstanceRemoteModel.audioPlayer.pause()
}

override func viewWillAppear(_ animated: Bool) {

     super.viewWillAppear(animated)

        if (audioFileURL != nil){
                   playSound(url:  audioFileURL as NSURL)
                     }
      }

// MARK:-  Play Sound

func playSound(url:NSURL){

    showHUD("Loading...")
    startTime = Date()

    print("geturl \(url)\n")
    DispatchQueue.main.async {
        let asset = AVURLAsset(url: url as URL, options: nil)
        //  print("Testing line 725== \(asset)")

        let playerItem = AVPlayerItem(asset: asset)


   RemoteModel.sharedInstanceRemoteModel.audioPlayer.replaceCurrentItem(with: playerItem)

        RemoteModel.sharedInstanceRemoteModel.audioPlayer.play()

    }
}

@IBAction func VolumeControl(_ sender: UISlider) {

    RemoteModel.sharedInstanceRemoteModel.audioPlayer.volume = sender.value
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



 // MARK: - ibutton Action


@IBAction func ibuttonAction(_ sender: UIButton) {

     self.performSegue(withIdentifier: "ibutton", sender: self)
  }

}

// iButtonViewController

   import UIKit
   import AVFoundation

   class MusicTextViewController: UIViewController,  {

    @IBOutlet var textView: UITextView!


override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()

}

@IBAction func BackAction(_ sender: UIButton) {

    // self.dismiss(animated : true, completion : nil)


    self.performSegue(withIdentifier: "goToPlayerViewFromTextView", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToPlayerViewFromTextView" {

        let playerVC = segue.destination as! PlayerViewController

        playerVC.comingFromPlayerTextView = true

        }
     }
  }

1 个答案:

答案 0 :(得分:0)

通过创建PlayerViewController的子视图完成。现在我可以轻松播放音频以及阅读文本。