Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用Swift禁用MPMoviePlayerController的音频(和中断)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
目前,这就是我在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)
}

我已经通过以下方式阅读了关于禁用音频的方法(根本没有工作).请记住,我正在尝试禁用它,以便不通过音乐应用程序,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.

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 = AVMutableAu@L_652_2@mix()
    audioZeroMix.inputParameters = allAudioParams

    // Create a player item
    let playerItem = AVPlayerItem(asset: asset)
    playerItem.au@L_652_2@mix = 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)
}

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。