How to force QT5 MediaPlayer to show Subtitles?

后端 未结 2 2113
日久生厌
日久生厌 2021-01-26 12:45

I am evaluating a migration from Qt 4.8 towards Qt 5.2 and the most important point is the multimedia backend. In Qt 5.2 there are some important features the Phonon backend in

2条回答
  •  轮回少年
    2021-01-26 13:26

    I searched a lot, apparently Qt mediaplayer does not support subtitle, I use srt parser to show subtitle.

    My BackEnd class code:

     #include "backend.h"
    BackEnd::BackEnd(QObject *parent) : QObject(parent)
    {
         readSubtitleFile("./en.srt");
    }
    
    void BackEnd::readSubtitleFile(QString directory)
    {
          cout<< "readSubtitleFile:"<< directory.toStdString()<getParser();
    
          sub = parser->getSubtitles();
    }
    
    QString BackEnd::getSubtitleText(double playTime)
    {
        for(SubtitleItem * element : sub) {
            double startTime = element->getStartTime();
            double endTime = element->getEndTime();
            if( (startTime <= playTime) && (playTime <= endTime)) {
                cout<< "getSubtitleText: founded"<< element->getText()<getText());
            }
        }
        cout<< "getSubtitleText: not founded"<< endl;
        return "";
    }
    
    bool BackEnd::isFileExist(const string &temp)
    {
        if (FILE *file = fopen(temp.c_str(), "r")) {
                fclose(file);
                return true;
            } else {
                return false;
            }
    }
    

    My qml file:

        Window {
        visible: true
        width: 900
        height: 700
        title: qsTr("My Player")
          Rectangle {
            id: root
            color: "black"
            width: parent.width
            height: parent.height
            function msToTime(duration) {
              var milliseconds=parseInt((duration%1000)/100),
              seconds = Math.floor((duration / 1000) % 60),
            minutes = Math.floor((duration / (1000 * 60)) % 60),
            hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
    
          hours = (hours < 10) ? "0" + hours : hours;
          minutes = (minutes < 10) ? "0" + minutes : minutes;
          seconds = (seconds < 10) ? "0" + seconds : seconds;
    
          return hours + ":" + minutes + ":" + seconds;
        }
        Column {
            width: parent.width
            height: parent.height
            Item {
                width: parent.width
                height: parent.height-100
                MediaPlayer {
                    id: mediaplayer
                    source: "file:///E:/1.mp4"
                }
    
                VideoOutput {
                    anchors.fill: parent
                    source: mediaplayer
                }
            }
            Text {
                id: subtitleText
                text: qsTr("")
                y: -150
                font.pixelSize: 18
                color: "white"
                anchors.horizontalCenter: 
             parent.horizontalCenter
            }
        }
        Timer {
          id: refreshTimer
          interval: 1000//30 // 60 Hz
          running: true
          repeat: true
          onTriggered: {
             durationPass.text =  
                root.msToTime(mediaplayer.position);
             subtitleText.text = 
    
            BackEnd.getSubtitleText(mediaplayer.position);
          }
      }
    }
    

提交回复
热议问题