Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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_Xcode_Avaudiosession_Headset - Fatal编程技术网

Ios 防止卸下耳机时出现视频暂停

Ios 防止卸下耳机时出现视频暂停,ios,swift,xcode,avaudiosession,headset,Ios,Swift,Xcode,Avaudiosession,Headset,使用的语言:Swift 2.3 我使用的是avplayervewcontroller,这是我的代码 let player = AVPlayer(URL: videoUrl!) playerViewController.player = player playerViewController.showsPlaybackControls = false 我在这里做的是播放一段视频,这段视频应该是不可信的,不可屏蔽的 在这里,我使用NSNotificationCenter N

使用的语言:Swift 2.3

我使用的是
avplayervewcontroller
,这是我的代码

let player = AVPlayer(URL: videoUrl!)            
playerViewController.player = player
playerViewController.showsPlaybackControls = false
我在这里做的是播放一段视频,这段视频应该是不可信的,不可屏蔽的

在这里,我使用
NSNotificationCenter

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.actionAfterVideo), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
这将关闭我的
avplayervewcontroller

func actionAfterVideo() {
    playerViewController.dismissViewControllerAnimated(true, completion: nil)
}
除了用户插拔耳机外,所有这些功能都可以正常工作。这会自动暂停视频。他们当然可以通过使用
控制中心
来恢复,但这不是直观的


当耳机从手机上拔下时,我是否可以阻止视频暂停?

您可以添加一个通知来检查会话是否被中断,如下所示:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "playInterrupt:", name: AVAudioSessionInterruptionNotification, object: yourSession)
func playInterrupt(notification: NSNotification)
{
    if notification.name == AVAudioSessionInterruptionNotification && notification.userInfo != nil
    {
        var info = notification.userInfo!
        var intValue: UInt = 0

        (info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)

        if let type = AVAudioSessionInterruptionType(rawValue: intValue)
        {
            switch type
            {
            case .Began:
                player.pause()
            case .Ended:
                let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
            }
        }
    }
}

func resumeNow(timer : NSTimer)
{
    player.play()
    print("restart")
}
并添加其
播放中断
实现,如下所示:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "playInterrupt:", name: AVAudioSessionInterruptionNotification, object: yourSession)
func playInterrupt(notification: NSNotification)
{
    if notification.name == AVAudioSessionInterruptionNotification && notification.userInfo != nil
    {
        var info = notification.userInfo!
        var intValue: UInt = 0

        (info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)

        if let type = AVAudioSessionInterruptionType(rawValue: intValue)
        {
            switch type
            {
            case .Began:
                player.pause()
            case .Ended:
                let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
            }
        }
    }
}

func resumeNow(timer : NSTimer)
{
    player.play()
    print("restart")
}

还可以查看

您可以添加一个通知,以检查会话是否被中断,如下所示:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "playInterrupt:", name: AVAudioSessionInterruptionNotification, object: yourSession)
func playInterrupt(notification: NSNotification)
{
    if notification.name == AVAudioSessionInterruptionNotification && notification.userInfo != nil
    {
        var info = notification.userInfo!
        var intValue: UInt = 0

        (info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)

        if let type = AVAudioSessionInterruptionType(rawValue: intValue)
        {
            switch type
            {
            case .Began:
                player.pause()
            case .Ended:
                let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
            }
        }
    }
}

func resumeNow(timer : NSTimer)
{
    player.play()
    print("restart")
}
并添加其
播放中断
实现,如下所示:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "playInterrupt:", name: AVAudioSessionInterruptionNotification, object: yourSession)
func playInterrupt(notification: NSNotification)
{
    if notification.name == AVAudioSessionInterruptionNotification && notification.userInfo != nil
    {
        var info = notification.userInfo!
        var intValue: UInt = 0

        (info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)

        if let type = AVAudioSessionInterruptionType(rawValue: intValue)
        {
            switch type
            {
            case .Began:
                player.pause()
            case .Ended:
                let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
            }
        }
    }
}

func resumeNow(timer : NSTimer)
{
    player.play()
    print("restart")
}
也看看