Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Scenes and Video keyboard controls

Tagged: ,

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #20799

    Phil

    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()
            }
        }
    }

     

    #20803

    Günther
    Felgo Team

    Hi!

    There is probably an issue with the focus setting when using the scene. By default, the configured activeScene of GameWindow automatically gets focus to handle key events. This is required to e.g. conveniently support the backButtonPressed signal per scene: https://felgo.com/doc/vplay-scene/#backButtonPressed-signal

    You can also have a look at the keyPressedGlobally signal of GameWindow if you don’t mind working with a global key handler:
    https://felgo.com/doc/vplay-gamewindowapplicationwindow/#keyPressedGlobally-signal

    Best,
    Günther

    #20816

    Phil

    Thanks Günter. I think it’s best to avoid the global key handlers, so I added the key bindings to PlayerScene and this is now working ok for me.

    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
        }
    
        Keys.onSpacePressed: {
            console.log("space pressed in playerScene")
            loader.item.playPause()
        }
    
        // load levels at runtime
        Loader {
            id: loader
            source: "Player.qml"
            onLoaded: {
                item.width = parent.width
                item.height = parent.height
                item.video.focus = true
            }
        }
    }

     

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded