gpt4 book ai didi

ios - 在 swift spritekit 场景中淡化视频

转载 作者:行者123 更新时间:2023-11-28 06:17:37 24 4
gpt4 key购买 nike

我正在将陈列室应用程序从 as3/starling 移植到本地 swift iPad 应用程序。

我有两个问题:

  1. 如何在我的 spritekit 内容上淡化视频(从 alpha 0 到 1)。

  2. 如何在不显示 iOS 屏幕音量通知图形的情况下使用单个 UI 元素控制 iPad 音量。

最佳答案

我的第一个答案回答了您最初的问题,这个答案是针对评论中发布的后续问题量身定制的。我觉得这两个答案对社区都有用,而不是合并成一个我认为会造成混淆的答案。

在这里,我实现了一个音量 slider 和一个控制 VideoNode 的 AVPlayer 对象的滑动 slider 。您可以获得 AVPlayer 的全部功能,但具有 SKVideoNode 的便利性。另外,播放视频的音量是可以控制的,无需调整系统音量和随后的灰色弹窗:

import SpriteKit
import MediaPlayer


class GameScene: SKScene {

let seekSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))
let volSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))

// Because it takes time for us to load the video, we don't know it's duration,
// hence we don't know what the `.maximumValue` should be for the seekSlider
let defaultMax = Float(0.123456789)

var player: AVPlayer!
var videoNode: SKVideoNode!

func seekSliderChangedValue(sender: UISlider) {
// Wait for file to finish loading so we can get accurate end duration:
if sender.maximumValue == defaultMax {
if CMTimeGetSeconds(player.currentItem!.duration).isNaN { return }
else { sender.maximumValue = Float(CMTimeGetSeconds(player.currentItem!.duration)) }
}

let value = CMTimeMake(Int64(seekSlider.value), 1)
player.seek(to: value)
player.play()
}

func volSliderChangedValue(sender: UISlider) {
player.volume = sender.value
}

override func didMove(to view: SKView) {
removeAllChildren() // Delete this in your actual project.

// Add some labels:
let seekLabel = SKLabelNode(text: "Seek")
seekLabel.setScale(3)
seekLabel.verticalAlignmentMode = .center
seekLabel.position = CGPoint(x: frame.minX + seekLabel.frame.width/2,
y: frame.maxY - seekLabel.frame.height)
let volLabel = SKLabelNode(text: "Volume")
volLabel.setScale(3)
volLabel.verticalAlignmentMode = .center
volLabel.position = CGPoint(x: frame.minX + volLabel.frame.width/2,
y: frame.minY + volLabel.frame.height + volSlider.frame.height)

// Make player and node:
let url = Bundle.main.url(forResource: "sample", withExtension: "mp4")!
player = AVPlayer(url: url)
videoNode = SKVideoNode(avPlayer: player!)

//Configure seek slider:
seekSlider.addTarget(self, action: #selector(seekSliderChangedValue), for: UIControlEvents.valueChanged)
seekSlider.maximumValue = defaultMax
let seekOrigin = convertPoint(toView: CGPoint(x: seekLabel.frame.minX, y: seekLabel.frame.minY))
seekSlider.frame = CGRect(origin: seekOrigin, size: CGSize(width: 200, height: 15))

//Configure vol slider:
volSlider.addTarget(self, action: #selector(volSliderChangedValue), for: UIControlEvents.valueChanged)
volSlider.value = 1
let volOrigin = convertPoint(toView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY))
volSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15))

//Scene stuff:
view.addSubview(seekSlider)
view.addSubview(volSlider)
addChild(seekLabel)
addChild(volLabel)
addChild(videoNode)

// Start video and animation:
videoNode.alpha = 0
videoNode.play()
videoNode.run(.fadeIn(withDuration: 5))
}
}

关于ios - 在 swift spritekit 场景中淡化视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719189/

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