Active Topics

 


Reply
Thread Tools
Posts: 8 | Thanked: 0 times | Joined on Jun 2012 @ Germany
#1
Hi everybody,
I got some problem to retrieve audio length from qml Audio component, I use the example code below:

Code:
import QtQuick 1.1
import com.nokia.meego 1.1
import QtMultimediaKit 1.1

Item {
    id: player

    property string color: "transparent"
    property string filePath: ""

    property bool playing: false
    //property int duration: audio.source != "" ? audio.duration : 0
    property int duration: audio.duration
    //property string durationTime: audio.source != "" ? getTimeFromMSec(audio.duration) : ""
    property string durationTime: getTimeFromMSec(duration)
    property alias position: audio.position
    property alias volume: audio.volume
    property string error: audio.errorString

    function play() {
        playing = true
        audio.play()
    }

    function pause() {
        audio.pause()
        playing = false
    }

    function stop() {
        audio.stop()
        playing = false
    }

    function loadAudio(fileName) {
        audio.stop()
        audio.source = fileName
    }


    function getTimeFromMSec(msec) {
        if (msec <= 0 || msec == undefined) {
            return ""

        } else {
            var sec = "" + Math.floor(msec / 1000) % 60
            if (sec.length == 1)
                sec = "0" + sec
            var hour = Math.floor(msec / 3600000)
            if (hour < 1) {
                return Math.floor(msec / 60000) + ":" + sec
            } else {
                var min = "" + Math.floor(msec / 60000) % 60
                if (min.length == 1)
                    min = "0" + min

                return hour + ":" + min + ":" + sec
            }
        }
    }

    onFilePathChanged: loadAudio(filePath)

    Audio {
        id: audio
        volume: 0
        Component.onCompleted: timer.running = true
    }
    Timer {
        id: timer
        interval: 100; running: false;  repeat: true; triggeredOnStart: true
        onTriggered: {
            print("****")
            if(audio.playing)  {
                if(player.durationTime != ''){
                    repeat = false
                    running = false
                    player.stop();
                    player.volume = 0.5
                }
            }
            else player.play()
        }
    }


    Rectangle {
        width: parent.width
        height: parent.height
        color: player.color

        ToolIcon {
            id: playIcon
            iconId: "toolbar-mediacontrol-play";
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            state: "pause"
            states: State {
                name: "play"
                PropertyChanges { target: playIcon; iconId: "toolbar-mediacontrol-pause" }
            }
            onClicked: {
                if(state == "play") {
                    state = "pause"
                    player.pause()
                }
                else {
                    state =  "play"
                    player.play()
                }
            }
        }
        ToolIcon {
            id: stopIcon
            iconId: "toolbar-mediacontrol-stop";
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: playIcon.right
            onClicked: {
                player.stop()
                playIcon.state = "pause"
            }

        }

        ProgressBar {
            id: progressBar
            anchors.top: parent.top
            width: parent.width
            maximumValue: player.duration
            minimumValue: 0

            Binding {
                target: progressBar
                property: "value"
                value: player.position
                when: player.playing
            }
        }
        Text {
            id: timeLabel
            anchors.left: stopIcon.right
            anchors.verticalCenter: parent.verticalCenter
            text: player.durationTime
        }
    }
}
I can not get audio duration event after audio is loaded, only after i call the play() function and a little bit later, can i get the duration, before that, the audio.duration property is 0 or -1.
is this a bug or I missed something?
above I used a very ugly way to get the duration, set a timer, and play with no sound until I get the duration string.

Last edited by nilsLee; 2012-09-11 at 17:07.
 
Reply


 
Forum Jump


All times are GMT. The time now is 01:49.