Hello,
I have scenes in my app, and one of the scenes is a video player. I want to be able to control the video with the keyboard keys, however this is not working when I put the video inside a scene. I’ve added some sample code below. Do you know why this is the case?
Thanks,
Phil
Main.qml
import Felgo 3.0
import QtQuick 2.0
import QtMultimedia 5.0
GameWindow {
id: app
MenuScene {
id: menuScene
anchors.fill: parent
Page {
AppButton {
anchors.centerIn: parent
text: "play"
onClicked: {
app.state = "player"
playerScene.focusVideo()
}
}
}
}
PlayerScene {
id: playerScene
width: parent.width
height: parent.height
}
state: "menu"
activeScene: menuScene
// state machine, takes care reversing the PropertyChanges when changing the state, like changing the opacity back to 0
states: [
State {
name: "menu"
PropertyChanges {target: menuScene; opacity: 1}
PropertyChanges {target: app; activeScene: menuScene}
},
State {
name: "player"
PropertyChanges {target: playerScene; opacity: 1}
PropertyChanges {target: app; activeScene: playerScene}
}
]
}
PlayerScene.qml
import Felgo 3.0
import QtQuick 2.0
Scene {
id: playerScene
opacity: 0
Behavior on opacity {NumberAnimation {duration: 250}}
function focusVideo() {
loader.item.video.focus = true
}
// load levels at runtime
Loader {
id: loader
source: "Player.qml"
onLoaded: {
item.width = parent.width
item.height = parent.height
item.video.focus = true
}
}
}
Player.qml
import Felgo 3.0
import QtQuick 2.5
import QtMultimedia 5.0
Item {
property var backSignal
property alias video: video
Page {
Video {
id: video
anchors.fill: parent
source: "../assets/VPlayAnimation.mp4"
autoPlay: false
focus: true
Keys.onSpacePressed: {
console.log("space pressed")
playPause()
}
}
AppButton {
text: video.playbackState === MediaPlayer.PlayingState ? "pause" : "play"
anchors.horizontalCenter: parent.horizontalCenter
onClicked: video.playbackState === MediaPlayer.PlayingState ? video.pause() : video.play()
}
}
function playPause() {
if (video.playbackState === MediaPlayer.PlayingState) {
video.pause()
} else {
video.play()
}
}
}