Ios 防止卸下耳机时出现视频暂停
使用的语言:Swift 2.3 我使用的是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
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")
}
也看看