qml播放器

import QtQuick 2.5
import QtQuick.Window 2.0
import QtGraphicalEffects 1.0
import QtMultimedia 5.6
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.1
import FileIO 1.0


ApplicationWindow {
    id:root
    width: 700
    height: 500
    objectName: "Vedio"
    x:Screen.desktopAvailableWidth/5
    y:Screen.desktopAvailableHeight/5
    visible: true
    color:"black"
    flags:Qt.Window |Qt.FramelessWindowHint   //添加了这一句,不显示标题栏与边框


    property real lastX
    property real lastY

    property variant fileurls:[]  //保存选择多个时的路径数组
    property variant filenames:[] //多个文件数组

    property bool spaceStatus:false;


    //获取影音名称
    function getVedioName()
    {
        var strList=filedlg.fileUrl.toString().split("/");

        var name=strList[strList.length-1];
        return name;
    }


    //获取多个影音名称
    function getVedioNames()
    {
        for(var k=0;k<fileurls.length;k++)
        {
            var str=fileurls[k].split("/");
            var name=str[str.length-1];
            filenames.push(name)
        }
    }


    function dateFormat(second){
        var timeTxt="";
        var hh,mm,ss;
        hh = second / 3600 | 0;
        second = Math.round(second) - hh * 3600;
        mm = second / 60 | 0;
        ss = Math.round(second) - mm * 60;
        if(hh!==0)
        {
            if(hh<10)
            {
                timeTxt =   "0"+hh+":";
            }
            else
            {
                timeTxt =   hh+":";
            }
        }
        if(mm<10)
        {
            mm  =   "0"+mm;
        }
        timeTxt =   timeTxt+mm;
        if(ss<10)
        {
            ss  =   "0"+ss;
        }
        timeTxt =   timeTxt+":"+ss;
        return  timeTxt;
    }

    property real curtime:0;
    property real totaltime;
    property real oldval :0.0;  //拖动滚动条之前的值
    property real oldpos :0.0;  //the play position

    Timer{
        id:timer
        repeat: true
        interval: 1000
        onTriggered:{
            curtime+=1
            var text= dateFormat(curtime)
            nowtime.text=text+" / "+dateFormat(totaltime)
        }
    }

    Timer{
        id:timerplay
        interval: 2000
        onTriggered: {
            console.log("timerplay is called")
            playMp4.stop()

            playMp4.play()
            playMp4.seek(0);

            root.totaltime=playMp4.duration/1000;
            root.curtime=0;
            vedioSlider.value=0.0
            timer.restart()
            anim.restart()
        }
    }


    Filedlg{
        id:filedlg
        width: 500
        height: 400
    }

    FileIO{
        id:fileio
    }


    //加载右栏目列表
    Listmore{
        id:listmore
        visible: true
        x:root.x+root.width
        y:root.y
    }


    Timer{
        id:history
        interval: 100
        running: true
        onTriggered:{
            listmore.nameArry=fileio.getListFiles()
            listmore.pathArry=fileio.getListPaths()
            listmore.filecount=listmore.nameArry.length
        }
    }


    //整体纵向排列

    Rectangle{

        Keys.enabled: true
        focus:true


        Keys.onEscapePressed:{
            topArea.visible=true
            bottomArea.visible=true
            root.visibility=Window.Windowed  //回复默认大小
        }
        Keys.onSpacePressed: {

            if(!spaceStatus)
            {
                pause.visible=false
                play.visible=true
                playMp4.pause()
                timer.stop()
                anim.stop()
                spaceStatus=true
            }
            else
            {
                playMp4.play()
                play.visible=false
                pause.visible=true
                timer.start()
                anim.start()
                spaceStatus=false
            }
        }

        color:"black"

        //自定义菜单栏
        anchors.fill: parent

        Rectangle{
            id:topArea
            width: parent.width
            height: 30
            color:"black"

            Rectangle{
                x:titleIcom.width
                anchors.verticalCenter: parent.verticalCenter
                width: 200
                height: parent.height
                color:"black"

                Text{
                    id:filename
                    text:"熊熊,你好"
                    color: "white"
                    font.pixelSize:15
                    anchors.verticalCenter: parent.verticalCenter

                    SequentialAnimation on x {
                        loops: Animation.Infinite
                        PauseAnimation{ duration: 10000 }
                        NumberAnimation{
                            to: -filename.text.length*15-60
                            duration: 4000
                        }

                        NumberAnimation{
                            to: filename.text.length*15
                            duration:0
                        }
                        NumberAnimation{
                            to: 0
                            duration: 3000
                        }
                    }

                }

            }

            Rectangle{
                id:titleIcom
                width: 40
                height: 20
                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                color:"black"
                Image{
                    width: 20
                    height: width
                    anchors.centerIn: parent
                    source: "qrc:/pic/panda.png"

                }
            }



            Image{
                id:hide
                width: 15
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right:max.left
                anchors.rightMargin: 10
                source: "qrc:/pic/hide.png"
                opacity: mouse1.containsMouse?0.4:1
                MouseArea{
                    id:mouse1
                    anchors.fill: parent
                    onPressed: {
                        root.visibility=Window.Minimized
                    }
                    hoverEnabled: true
                }
            }

            Image{
                id:max
                width: 15
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right:close.left
                anchors.rightMargin: 10
                source: "qrc:/pic/max.png"
                opacity: mouse2.containsMouse?0.4:1
                MouseArea{
                    id:mouse2
                    anchors.fill: parent
                    onPressed: {
                        root.visibility=Window.Maximized
                    }
                    hoverEnabled: true
                }
            }

            Image{
                id:close
                width: 15
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.rightMargin: 10
                source: "qrc:/pic/close.png"
                opacity: mouse3.containsMouse?0.4:1
                MouseArea{
                    id:mouse3
                    anchors.fill: parent
                    onPressed: {
                        root.close()
                        fileio.slotWriteTofile(listmore.nameArry,listmore.pathArry);
                        listmore.close()
                    }
                    hoverEnabled: true
                }
            }

            MouseArea{
                width: parent.width-75
                height: 30
                acceptedButtons: Qt.LeftButton //只处理鼠标左键
                onPressed: { //接收鼠标按下事件
                    lastX = mouseX
                    lastY = mouseY
                }
                onPositionChanged: { //鼠标按下后改变位置
                    root.x += (mouseX - lastX)
                    root.y += (mouseY - lastY)
                }
            }
        }

        //分割线
        Rectangle{
            id:splitTop
            width: parent.width
            height: 1
            anchors.bottom: topArea.bottom
            color:"lightgray"
            opacity: 0.2

        }


        //选择播放文件
        Rectangle{
            id:selectFile
            width: parent.width
            height: root.height-topArea.height-bottomArea.height
            visible: true
            anchors.top:splitTop.bottom
            //border.color: "black"

            Button{
                anchors.centerIn: parent
                width: 90
                height: 30
                text: "打开文件"
                iconSource: "qrc:/pic/fileIcom.png"
                opacity: 0.7
                onClicked: {
                    filedlg.selectMultiple=false
                    filedlg.selectFolder=false
                    filedlg.open();
                }

            }

            Connections{
                target:filedlg
                onAccepted:{

                    //add folder
                    if(filedlg.selectFolder)
                    {
                        root.fileurls.splice(0,root.fileurls.length)
                        root.filenames.splice(0,root.filenames.length)
                        fileio.slotGetAllfiles(filedlg.folder)
                        root.filenames=fileio.getListFiles()
                        root.fileurls=fileio.getListPaths()

                        console.log(root.filenames);
                        for(var a=0;a<root.filenames.length;a++)
                        {
                            if(listmore.nameArry.length<1)
                            {
                                listmore.nameArry.push(root.filenames[a])
                                listmore.pathArry.push(root.fileurls[a])
                                listmore.filecount+=1
                            }

                            for(var b=0;b<listmore.nameArry.length;b++)
                            {
                                if(listmore.nameArry[b]===root.filenames[a]) break;
                                if(b===listmore.nameArry.length-1)
                                {
                                    listmore.nameArry.push(root.filenames[a])
                                    listmore.pathArry.push(root.fileurls[a])
                                    listmore.filecount+=1
                                }

                            }

                        }
                        return;
                    }


                    //add more file
                    if(filedlg.selectMultiple)
                    {
                        root.fileurls.splice(0,root.fileurls.length)
                        root.filenames.splice(0,root.filenames.length)
                        root.fileurls=filedlg.fileUrls
                        getVedioNames();
                        console.log(root.filenames)
                        for(var m=0;m<root.filenames.length;m++)
                        {
                            if(listmore.nameArry.length<1)
                            {
                                listmore.nameArry.push(root.filenames[m])
                                listmore.pathArry.push(root.fileurls[m])
                                listmore.filecount+=1
                            }

                            for(var n=0;n<listmore.nameArry.length;n++)
                            {
                                if(listmore.nameArry[n]===root.filenames[m]) break;
                                if(n===listmore.nameArry.length-1)
                                {
                                    listmore.nameArry.push(root.filenames[m])
                                    listmore.pathArry.push(root.fileurls[m])
                                    listmore.filecount+=1

                                }
                            }

                        }
                        //console.log(listmore.nameArry)
                        return;
                    }


                    //add singal file
                    playMp4.source=filedlg.fileUrl
                    filename.text=getVedioName()
                    viewarea.visible=true
                    selectFile.visible=false

                    for(var i=0;i<listmore.nameArry.length;i++)
                    {
                        if(listmore.nameArry[i]===filename.text) return
                    }
                    listmore.nameArry.push(filename.text)
                    listmore.pathArry.push(playMp4.source.toString())
                    listmore.filecount+=1

                    play.visible=false
                    pause.visible=true
                    timerplay.restart()

                }
            }

        }


        //doubleclock the file start Play event
        Connections{
            target: listmore
            onCurselnameChanged:{

                playMp4.source=listmore.curselpath
                filename.text=listmore.curselname

                selectFile.visible=false
                viewarea.visible=true


                play.visible=false
                pause.visible=true
                timerplay.restart()

            }
        }



        //视频显示区域
        Rectangle{
            id:viewarea
            width: parent.width
            height: root.height-topArea.height-bottomArea.height
            color:"black"
            visible: false
            anchors.top:splitTop.bottom
            MediaPlayer {

                id: playMp4
                autoLoad: true
                autoPlay: false
                volume: 0.8

            }


            VideoOutput {
                id: video
                anchors.fill: parent
                source: playMp4
                MouseArea{
                    anchors.fill: parent
                    acceptedButtons: Qt.LeftButton
                    onDoubleClicked:
                    {
                        topArea.visible=false
                        bottomArea.visible=false
                        root.visibility=Window.FullScreen //全屏xia显示
                    }

                }

                Image {
                    id: pauseIcon
                    visible: play.visible||root.spaceStatus
                    anchors.centerIn: parent
                    source: "qrc:/pic/pauseIcon.png"
                }
            }


            Slider{
                id:vedioSlider
                anchors.bottom: viewarea.bottom
                width: parent.width
                height: 10
                visible: true
                opacity: sliderarea.containsMouse?0.2:0
                MouseArea{
                    id:sliderarea
                    anchors.fill: parent
                    onPressed: {
                        anim.stop()
                        vedioSlider.value=mouseX/vedioSlider.width  //滚动条宽度为700像素
                        playMp4.seek(playMp4.duration*vedioSlider.value)
                        root.curtime=playMp4.duration*vedioSlider.value/1000
                        anim.start()
                    }
                    hoverEnabled:true
                }

                style: SliderStyle {
                    groove: Rectangle {
                        color: "red"
                        radius: 1
                        height: 10
                    }
                    handle:Rectangle {
                        anchors.centerIn: parent
                        color: control.pressed ? "white" : "darkgray"
                        border.color: "gray"
                        border.width: 2
                        implicitWidth: 15
                        implicitHeight: 15
                        radius:8
                        Rectangle{
                            width: parent.width-8
                            height: width
                            radius: width/2
                            color: "black"
                            anchors.centerIn: parent
                        }
                    }
                }
            }
        }



        NumberAnimation{
            target: vedioSlider
            properties: "value"
            id:anim
            to:1
            duration: playMp4.duration-playMp4.position<0?root.totaltime:playMp4.duration-playMp4.position

        }



        //分割线
        Rectangle{
            id:splitBottom
            width: parent.width
            height: 1
            anchors.bottom: bottomArea.top
            color:"lightgray"
            opacity: 0.2
        }


        Rectangle{
            id:bottomArea
            height: 50
            width: parent.width
            color:"black"
            anchors.bottom: parent.bottom

            Text{
                id:nowtime
                text:"00:00 / 00:00"
                color: "white"
                font.pixelSize:15
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: stop.left
                anchors.rightMargin: 10

            }

            Image{
                id:stop
                source: "qrc:/pic/stop.png";
                width:30
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: slow.left
                anchors.rightMargin: 10
                opacity: mouse4.containsMouse?0.4:1

                MouseArea{
                    id:mouse4
                    anchors.fill:parent
                    onClicked: {
                        playMp4.seek(0.0)
                        vedioSlider.value=0.0
                        playMp4.stop()
                        timer.stop()
                        anim.stop()
                        nowtime.text="00:00 / 00:00"
                        viewarea.visible=false
                        selectFile.visible=true
                        filename.text="熊熊,你好"
                    }
                     hoverEnabled:true
                }
            }

            Image{
                id:slow
                source: "qrc:/pic/slow.png";
                width:30
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: control.left
                anchors.rightMargin: 10
                opacity: mouse5.containsMouse?0.4:1
                MouseArea{
                    id:mouse5
                    anchors.fill:parent
                    onClicked: {
                        if(vedioSlider.value>=0.0)
                        {
                            anim.stop()
                            playMp4.seek(playMp4.position-45000)
                            root.curtime=playMp4.position/1000
                            vedioSlider.value=playMp4.position/playMp4.duration
                            anim.start()
                        }
                    }
                    hoverEnabled: true
                }

            }


            Rectangle{
                id:control
                width:38
                height: width
                anchors.centerIn: parent
                color:"black"
                opacity: playArea.containsMouse||pauseArea.containsMouse?0.4:1


                Image {
                    id: play
                    source: "qrc:/pic/play.png";
                    anchors.fill: parent
                    MouseArea{
                        id:playArea
                        anchors.fill: parent
                        onClicked:{
                            playMp4.play()
                            play.visible=false
                            pause.visible=true
                            timer.start()
                            anim.start()
                        }
                        hoverEnabled: true
                    }
                }

                Image {
                    id: pause
                    source: "qrc:/pic/pause.png";
                    visible: false
                    anchors.fill: parent
                    MouseArea{
                        id:pauseArea
                        anchors.fill: parent
                        onClicked:{
                            pause.visible=false
                            play.visible=true
                            playMp4.pause()
                            timer.stop()
                            anim.stop()
                        }
                        hoverEnabled: true
                    }

                }

            }



            Image{
                id:fast
                source: "qrc:/pic/fast.png";
                width:30
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: control.right
                anchors.leftMargin: 10
                opacity: mouse6.containsMouse?0.4:1
                MouseArea{
                    id:mouse6
                    anchors.fill:parent
                    onClicked: {
                        if(vedioSlider.value<=1.0)
                        {
                            anim.stop()
                            playMp4.seek(playMp4.position+45000)
                            root.curtime=playMp4.position/1000
                            vedioSlider.value=playMp4.position/playMp4.duration
                            anim.start()
                        }
                    }
                    hoverEnabled: true

                }
            }



            Image {
                id: voice
                source: "qrc:/pic/voice.png"
                width: 15
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: fast.right
                anchors.leftMargin: 10

            }

            //音量控制
            Slider{
                id:voiceSlider
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: voice.right
                anchors.leftMargin: 3
                width: 180
                height: 10
                opacity: 0.5
                value: 0.8
                onValueChanged: {
                    playMp4.volume=voiceSlider.value
                }

            }



            //右列表框触发
            Image {
                source: "qrc:/pic/more.png"
                width: 30
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.rightMargin: 5

                opacity: mouse7.containsMouse?0.4:1

                MouseArea{
                    id:mouse7
                    anchors.fill:parent
                    onClicked: {
                        listmore.visible=listmore.visible?false:true
                    }
                    hoverEnabled: true
                }

            }

        }
    }

}

 

转载于:https://my.oschina.net/urlove/blog/1785555

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值