最近学习了qml的QtMultimedia,看了一个音乐播放器的例子,非常简单。
主要就是播放器的模块比较重要,因为它可以获得播放的时长那个,进度,等等,然后用按钮添加播放器模块的事件,用text文本框来显示播放的进度以及状态。
import QtQuick 2.5
import QtMultimedia 5.0
import QtQuick.Window 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 2.0
Window{
visible: true;
width: 1000;
height: 600;
minimumHeight: 320;
minimumWidth: 480;
title: "music";
property var utilDate: new Date();
Image {
anchors.horizontalCenter: parent.horizontalCenter;
fillMode: Image.PreserveAspectFit;
source: "qrc:/new/prefix1/Image/bk1.jpg";
}
function msecString(msecs){
utilDate.setTime(msecs);
return Qt.formatTime(utilDate,"mm::ss");
}
MediaPlayer{
id:player;
source: "C:/Users/zfc/Desktop/image/1.mp3";
onPositionChanged: {
//进度
progress.text=msecString(position)+progress.sDuration;
}
onDurationChanged: {
//时长
progress.sDuration=" / "+msecString(duration);
}
onPlaybackStateChanged: {
switch(playbackState){
case MediaPlayer.PlayingState:
state.text="播放中";
break;
case MediaPlayer.PausedState:
state.text="已暂停";
break;
case MediaPlayer.StoppedState:
state.text="停止";
break;
}
}
}
Row{
id:controller;
anchors.top: parent.verticalCenter;
anchors.horizontalCenter: parent.horizontalCenter;
anchors.topMargin: 24;
spacing: 100;
Button{
width: 50;
height: 50;
background: Image {
source: "qrc:/new/prefix1/Image/pre2.png";
}
onClicked: {
if(player.seekable){
player.seek(player.position-5000);
}
}
}
Button{
width: 50;
height: 50;
background: Image {
source: "qrc:/new/prefix1/Image/pause2.png";
}
onClicked: {
player.pause();
}
}
Button{
width: 50;
height: 50;
background: Image {
source: "qrc:/new/prefix1/Image/play2.png";
}
onClicked: {
player.play();
}
}
Button{
width: 50;
height: 50;
background: Image {
source: "qrc:/new/prefix1/Image/like.png";
}
onClicked: {
player.stop();
}
}
Button{
width: 50;
height: 50;
background: Image {
source: "qrc:/new/prefix1/Image/next2.png";
}
onClicked: {
if(player.seekable){
player.seek(player.position+5000);
}
}
}
}
Text {
id: progress
anchors.top: parent.verticalCenter;
anchors.horizontalCenter: parent.horizontalCenter;
anchors.topMargin: -120;
property string sDuration;
text: qsTr("text");
}
Text{
id:state;
anchors.left: progress.left;
anchors.bottom: progress.top;
anchors.bottomMargin: 4;
color: "red";
font.pointSize: 32;
}
}