SIcoder120
SIcoder120

Reputation: 3

Trying to create a simple QML program where a user can select a video file and it starts playing it

The program runs but when i select the video file it shows the error "Cannot assign [undefined] to QUrl" on the File Dialog onAccepted function. It seems like fileDialog.fileUrl is of type QUrl but mediaPlayer.source is an undefined type.

This is my code

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Dialogs
import QtMultimedia

ApplicationWindow {
    id: window
    width: 640
    height: 480
    visible: true
    title: "Video Player"

    MediaPlayer {
        id: mediaPlayer
    }

    VideoOutput {
        id: videoOutput
        anchors.fill: parent
    }

    FileDialog {
        id: fileDialog
        title: "Select Video File"
        nameFilters: ["Video Files (*.avi *.mp4 *.mkv)"]

        onAccepted: {
            mediaPlayer.source = fileDialog.fileUrl
            console.log(fileDialog.fileUrl)
            mediaPlayer.play()
        }
    }

    Button {
        text: "Select Video File"
        anchors.centerIn: parent
        onClicked: fileDialog.open()
    }

    Button {
        text: mediaPlayer.source
    }
}

Upvotes: 0

Views: 296

Answers (1)

iam_peter
iam_peter

Reputation: 3924

There is no property fileUrl in FileDialog. You should use selectedFile. Also you need to assign the VideoOutput to the videoOutput property of the MediaPlayer.

import QtQuick
import QtQuick.Controls
import QtQuick.Dialogs
import QtMultimedia

ApplicationWindow {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Video Player")

    MediaPlayer {
        id: mediaPlayer
        videoOutput: videoOutput
    }

    VideoOutput {
        id: videoOutput
        anchors.fill: parent
    }

    FileDialog {
        id: fileDialog
        title: qsTr("Select Video File")
        nameFilters: ["Video Files (*.avi *.mp4 *.mkv)"]

        onAccepted: {
            mediaPlayer.source = fileDialog.selectedFile
            mediaPlayer.play()
        }
    }

    Button {
        text: qsTr("Select Video File")
        anchors.centerIn: parent
        onClicked: fileDialog.open()
    }
}

Upvotes: 0

Related Questions