Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 在我的应用程序中与用户';什么是iTunes音乐?_Ios_Swift_Swift3_Avplayer - Fatal编程技术网

Ios 在我的应用程序中与用户';什么是iTunes音乐?

Ios 在我的应用程序中与用户';什么是iTunes音乐?,ios,swift,swift3,avplayer,Ios,Swift,Swift3,Avplayer,我有一个没有音乐的视频,我想向用户显示,即使音乐在另一个应用程序中播放。如果播放音乐,我下面的代码会中断视频播放 视图控制器: if let filePath = Bundle.main.path(forResource: "streamVideo", ofType: "mp4") { print("handled video: \(filePath)") let videoURL = NSURL(fileURLWithPath: filePath)

我有一个没有音乐的视频,我想向用户显示,即使音乐在另一个应用程序中播放。如果播放音乐,我下面的代码会中断视频播放

视图控制器:

    if let filePath = Bundle.main.path(forResource: "streamVideo", ofType: "mp4") {

        print("handled video: \(filePath)")
        let videoURL = NSURL(fileURLWithPath: filePath)
        avPlayer = AVPlayer(url: videoURL as URL)

        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        avPlayer.volume = 0
        avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none

        avPlayerLayer.frame = view.layer.bounds
        view.backgroundColor = UIColor.clear;
        view.layer.insertSublayer(avPlayerLayer, at: 0)

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(FirstVC.playerItemDidReachEnd),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                               object: avPlayer.currentItem)

        avPlayer.play()

    }
应用程序内代理:

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    } catch let error as NSError {
        print(error)
    }

    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch let error as NSError {
        print(error)
    }

这个问题可能是一个重复的问题,它的解决方案是@RhythmicFistman不,这是另一个问题。我的音乐继续播放,但视频不播放。