Qt World Summit: Submit your Presentation

[Android] Audio duration = -1 when play a mp3 audio on website

  • Hi,

    I am facing an issue with mp3 duration, it doesn't work well on android. This is my sample code:

    import QtQuick 2.14
    import QtQuick.Window 2.14
    import QtMultimedia 5.14
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        Rectangle {
            anchors.fill: parent
            color: "lightblue"
            Text {
                text: "Click Me!";
                font.pointSize: 24;
                width: 150; height: 50;
                Audio {
                    id: playMusic
                    source: "https://phubq.com/upload/EA/toeic_practice/part1/toeic_practice_p1_s10_0.mp3"
                    onPlaybackStateChanged: {
                        console.log("Current status: " + playMusic.playbackState)
                        console.log("Duration: " + playMusic.duration)
                MouseArea {
                    id: playArea
                    anchors.fill: parent
                    onPressed:  { playMusic.play() }

    This is output by os:

    qml: Current status: 1
    qml: Duration: 0
    qml: Current status: 0
    qml: Duration: 17604


    D libMP3Example_x86.so: qml: Current status: 1
    D libMP3Example_x86.so: qml: Duration: -1
    D libMP3Example_x86.so: qml: Current status: 0
    D libMP3Example_x86.so: qml: Duration: -1

    I don't know why on Android, that duration is -1. In my project, I need correct value to use for Slider.
    Btw, I tested on iOS, and it worked well, then it seems this happens only in android system.
    It also works well if I use mp3 file as resource instead play online mp3.
    This is my system info:

    Android studio: 3.5.2
    NDK: r20b

    Any one have solution for this or met this before?

Log in to reply