使用Swift禁用MPMoviePlayerController的音频(和中断)

使用Swift禁用MPMoviePlayerController的音频(和中断),第1张

概述目前,这就是我在UIViewController的子视图中播放视频的方式: override func viewDidAppear(animated: Bool) { let filePath = NSBundle.mainBundle().pathForResource("musicvideo", ofType: "mp4") self.moviePlayerControlle 目前,这就是我在UIVIEwController的子视图中播放视频的方式:

overrIDe func vIEwDIDAppear(animated: Bool)  {    let filePath = NSBundle.mainBundle().pathForResource("musicvIDeo",ofType: "mp4")    self.movIEPlayerController.contentURL = NSURL.fileURLWithPath(filePath)    self.movIEPlayerController.play()    self.movIEPlayerController.repeatMode = .One    self.movIEPlayerController.vIEw.frame = self.vIEw.bounds    self.movIEPlayerController.scalingMode = .AspectFill    self.movIEPlayerController.controlStyle = .None    self.movIEPlayerController.allowsAirPlay = false    self.vIEw.addSubvIEw(self.movIEPlayerController.vIEw)}@H_419_12@  

我已经通过以下方式阅读了关于禁用音频的方法(根本没有工作).请记住,我正在尝试禁用它,以便不通过音乐应用程序,Spotify等中断当前播放的音乐.

// Playing media items with the applicationMusicPlayer will restore the user's Music state after the application quits.// The current volume of playing music,in the range of 0.0 to 1.0.// This property is deprecated -- use MPVolumeVIEw for volume control instead.@H_419_12@  

1)MPMusicPlayerController.applicationMusicPlayer().volume = 0

2)MPVolumeVIEw甚至没有设置实际音量的设置?这是一个控制.

3)self.movi​​ePlayerController.useApplicationAudioSession = false

解决方法 所以我找到了 this answer.

这是我最终使用的Swift代码.然后我使用AVPlayerLayer作为子图层添加到视图中,它完美地工作.

感谢OP成功获得Apple技术人员并提供原始Objective-C code.

我现在面临的唯一问题是它:

1)中断当前音乐播放,无论是来自Music,Spotify等.

2)如果我关闭应用程序并再次打开它,视频将停止播放.

overrIDe func vIEwDIDAppear(animated: Bool)  {    let filePath = NSBundle.mainBundle().pathForResource("musicvIDeo",ofType: "mp4")    var asset: AVURLAsset?    asset = AVURLAsset.URLAssetWithURL(NSURL.fileURLWithPath(filePath),options: nil)    var audioTracks = NSArray()    audioTracks = asset!.tracksWithMediaType(AVMediaTypeAudio)    // Mute all the audio tracks    let allAudioParams = NSMutableArray()    for track: AnyObject in audioTracks {        // AVAssetTrack        let audioinputParams = AVMutableAudioMixinputParameters()        audioinputParams.setVolume(0.0,atTime: kCMTimeZero)        audioinputParams.trackID = track.trackID        allAudioParams.addobject(audioinputParams)    }    let audioZeroMix = AVMutableAudioMix()    audioZeroMix.inputParameters = allAudioParams    // Create a player item    let playerItem = AVPlayerItem(asset: asset)    playerItem.audioMix = audioZeroMix    // Create a new Player,and set the player to use the player item    // with the muted audio mix    let player = AVPlayer.playerWithPlayerItem(playerItem) as AVPlayer    player.play()    let layer = AVPlayerLayer(player: player)    player.actionAtItemEnd = .None    layer.frame = self.vIEw.bounds    layer.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill    self.vIEw.layer.addSublayer(layer)}@H_419_12@                            	          总结       

以上是内存溢出为你收集整理的使用Swift禁用MPMoviePlayerController的音频(和中断)全部内容,希望文章能够帮你解决使用Swift禁用MPMoviePlayerController的音频(和中断)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1006379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存