gpt4 book ai didi

iOS Swift Mute-unmute 音频像 facebook 自动播放视频

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:17 25 4
gpt4 key购买 nike

我目前正在使用 MPMoviePlayerController?在我的应用程序中播放视频。但我想要 facebook 风格的视频,最初视频播放静音,但当用户点击它时取消静音。

我是编程新手,但根据我的研究,我无法在 MPMoviePlayerController 中找到任何内容?我们可以在其中做到这一点。

目前我正在使用以下代码播放视频。

class InformationViewController: UIViewController {

@IBOutlet var thumbnailImage: UIImageView!
@IBOutlet var videoPlayerView: UIView!

@IBOutlet var controlBtn: UIButton!

var moviePlayer:MPMoviePlayerController!
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
super.viewDidLoad()


}


@IBAction func controlBtn(sender: AnyObject) {
controlBtn.alpha = 0
moviePlayer = MPMoviePlayerController(contentURL: url)
let frameWidth = self.videoPlayerView.frame.size.width
let frameHeight = self.videoPlayerView.frame.size.height

//moviePlayer.view.frame = CGRect(x: 24, y: 18, width: frameWidth, height: frameHeight)

self.videoPlayerView.addSubview(moviePlayer.view)


// Send button backword
self.videoPlayerView.sendSubviewToBack(moviePlayer.view)
moviePlayer.fullscreen = false
moviePlayer.controlStyle = MPMovieControlStyle.Embedded

// Pure Visual Format Language style (http://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically)
(moviePlayer.view).setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["view": videoPlayerView, "videoView": moviePlayer.view]

var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-(<=0)-[videoView(\(frameWidth))]", options: .AlignAllCenterY, metrics: nil, views: views)
view.addConstraints(constH)
var constW = NSLayoutConstraint.constraintsWithVisualFormat("V:[view]-(<=0)-[videoView(\(frameHeight))]", options: .AlignAllCenterX, metrics: nil, views: views)
view.addConstraints(constW)

}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated) ///< Don't for get this BTW!

if let player = self.moviePlayer {
player.stop()
}
}}

最佳答案

在与 Apple 技术人员交谈后发现无法使用 MPMoviePlayerController 控制或静音音频。

因此,您可以使用 AVFoundations AVPlayer 类在像 FB 一样播放时将视频静音。

源代码:

AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:strURL]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];

//Using this you can mute the audio and play only video as you wish
avPlayer.muted = YES;

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = IS_IPAD ? CGRectMake(0, 0, 748, 423): CGRectMake(0, 0, 311, 200);
avPlayerLayer.backgroundColor =[UIColor clearColor].CGColor;
avPlayerLayer.videoGravity =AVLayerVideoGravityResize;

将音频静音后,您只需更改 bool 值即可取消静音,如下所示。 avPlayer.muted=否;

关于iOS Swift Mute-unmute 音频像 facebook 自动播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com