如何使用AVAudioPlayer关闭视频的音频?

时间:2018-11-23 21:55:53

标签: ios iphone swift avaudioplayer

我有一个简单的相机,可以录制视频,然后过渡到videoPreview。里面有一个按钮,可以将镜头重新按回相机。但是,我这样做视频音频不会停止。如何删除它(而不是静音),以便可以拍摄新的vid?

以下是我的视频预览代码和我尝试过的内容

    class VideoPlayback: UIViewController {

    //receives url video value from VideoViewController
    var videoURL: URL!

    //place where video gets displayed
    @IBOutlet weak var videoView: UIView!

    var avPlayer = AVPlayer()

    private var playerQueue: AVQueuePlayer!
    private var playerItem1: AVPlayerItem!
    private var playerLooper: AVPlayerLooper!
    private var playerLayer: AVPlayerLayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        print(avPlayer, "avPlayer")
        print("videoURL", videoURL)

        playerItem1 = AVPlayerItem(url: videoURL as URL)
        print(playerItem1, "playerItem1")

        playerQueue = AVQueuePlayer(playerItem: playerItem1)
        print(playerQueue, "player queue")

        playerLayer = AVPlayerLayer(player: playerQueue)
        print(playerLayer, "playerLayer")

        playerLooper = AVPlayerLooper(player: playerQueue, templateItem: playerItem1)
        print(playerLooper, "player looper")

        playerLayer.frame = view.bounds
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        print(videoView, "yruewioq")
        videoView.layer.insertSublayer(playerLayer, at: 0)

        view.layoutIfNeeded()
        self.playerQueue?.play()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //attempt at turing audio off
        playerQueue = nil
    }
    @IBAction func cancelVidPreviewButton(_ sender: Any) {
        performSegue(withIdentifier: "cancelVideo", sender: self)
    }
}

更新:

我也尝试过playerLayer.isMuted = true ,但是我发现它没有成员isMute

3 个答案:

答案 0 :(得分:1)

您可以使用AVPlayer's volume属性来控制AVPlayer的音量。

volume设置为0可以使音频静音。

根据Documentation

  

值0.0表示静音;值1.0(默认值)   指示播放器实例的完整音频音量。

希望这会有所帮助

答案 1 :(得分:1)

如果我正确理解,则需要确保音频会话在segue之前已经结束。不过,将音量设为零可能会影响音频会话。

答案 2 :(得分:0)

所以我看到这里实际上需要做的是删除所有具有音频的视频数据,如下所示

        self.playerQueue?.removeAllItems()

但是,如果您想摆脱音频,只需要执行.pause()然后删除数据即可。这是docs