Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading utf8 QMediaPlaylist
QtWS25 Last Chance

Reading utf8 QMediaPlaylist

Scheduled Pinned Locked Moved Solved General and Desktop
qmediaplaylistutf-8utf8
8 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • UnitScanU Offline
    UnitScanU Offline
    UnitScan
    wrote on last edited by UnitScan
    #1

    I have a QMediaPlaylist with this filename:

    "♥♫ Feels So Good (sonique Best Remix Extended Version) Music Song Video ❦ love romantic"

    When I load the playlist, the file is read in this way:

    "♥♫ Feels So Good (sonique Best Remix Extended Version) Music Song Video ❦ love romantic"

    Playlist in loaded in this way:

    void MainWindow::loadPlaylist() {
        playlist->load(QUrl::fromLocalFile("C:/Users/Marco/Music/default.m3u"), "m3u");
        for (int i=0; i < playlist->mediaCount(); i++) {
            QString mediafile = playlist->media(i).canonicalUrl().toString();
            // rest of the stuff
    }
    

    I tried

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    

    and

    QString string = playlist->media(i).canonicalUrl().toString();
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QByteArray encodedString = codec->fromUnicode(string);
    QString mediafile = encodedString;
    

    But the result does not change. This happens both with .m3u and .m3u8 playlist.

    Can you help me?

    raven-worxR 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Let's see where the problem is...
      I'd ask you test one thing:

      QFile playListFile("C:/Users/Marco/Music/default.m3u");
      if(!file.open(QIODevice::ReadOnly))
      Q_ASSERT(false);
      QTextStream playRead(&file);
      // playRead.setAutoDetectUnicode(true); //Uncomment and test separately
      while(!playRead.atEnd()){
      qDebug() << playRead.readLine().trimmed();
      }
      

      and see if it is parsed correctly.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • UnitScanU Offline
        UnitScanU Offline
        UnitScan
        wrote on last edited by UnitScan
        #3

        Tried this with the same result:

                QFile file("C:/Users/Marco/Music/default.m3u");
                if(!file.open(QIODevice::ReadOnly))
                Q_ASSERT(false);
                QTextStream playRead(&file);
                playRead.setAutoDetectUnicode(true); 
                while(!playRead.atEnd()){
                qDebug() << playRead.readLine().trimmed();
                }
        
               QFile file("C:/Users/Marco/Music/default.m3u");
                if(!file.open(QIODevice::ReadOnly))
                Q_ASSERT(false);
                QTextStream playRead(&file);
                playRead.setCodec("UTF-8"); 
                while(!playRead.atEnd()){
                qDebug() << playRead.readLine().trimmed();
                }
        
        1 Reply Last reply
        0
        • UnitScanU UnitScan

          I have a QMediaPlaylist with this filename:

          "♥♫ Feels So Good (sonique Best Remix Extended Version) Music Song Video ❦ love romantic"

          When I load the playlist, the file is read in this way:

          "♥♫ Feels So Good (sonique Best Remix Extended Version) Music Song Video ❦ love romantic"

          Playlist in loaded in this way:

          void MainWindow::loadPlaylist() {
              playlist->load(QUrl::fromLocalFile("C:/Users/Marco/Music/default.m3u"), "m3u");
              for (int i=0; i < playlist->mediaCount(); i++) {
                  QString mediafile = playlist->media(i).canonicalUrl().toString();
                  // rest of the stuff
          }
          

          I tried

          QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
          

          and

          QString string = playlist->media(i).canonicalUrl().toString();
          QTextCodec *codec = QTextCodec::codecForName("UTF-8");
          QByteArray encodedString = codec->fromUnicode(string);
          QString mediafile = encodedString;
          

          But the result does not change. This happens both with .m3u and .m3u8 playlist.

          Can you help me?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @UnitScan said in Reading utf8 QMediaPlaylist:

          "♥♫ Feels So Good (sonique Best Remix Extended Version) Music Song Video ❦ love romantic"

          how did you get this string? via qDebug?
          Note that qDebug doesn't output any NON-ASCII characters (of QStrings) anymore on the console. See QTBUG-47316

          Try to output the QString as char* (e.g. str.toUtf8().constData()) to qDebug instead.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • UnitScanU Offline
            UnitScanU Offline
            UnitScan
            wrote on last edited by
            #5

            Not only in qDebug, filename is loaded formatted as this: http://imgur.com/a/2wVoJ

            When I load the media directly from file, I use this function:

            void MainWindow::openmediaOnClick()
            {
                *filenames = QFileDialog::getOpenFileNames(this, tr("Open Media"), 
                settings->value("app/lastUrl").toString(),
                tr("Audio Files (*.mp3 *.wav *.ogg)"));
                if (filenames->size() > 0) {
                    playlist->clear();
                    for (int i=0; i < filenames->size(); i++) {
                        QString name(filenames->at(i));
                        QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
                        playlist->addMedia(QUrl(QFileInfo(name).filePath()));
            
                    }
            //rest of the stuff
                }
            }
            

            And works well

            VRoninV 1 Reply Last reply
            0
            • UnitScanU UnitScan

              Not only in qDebug, filename is loaded formatted as this: http://imgur.com/a/2wVoJ

              When I load the media directly from file, I use this function:

              void MainWindow::openmediaOnClick()
              {
                  *filenames = QFileDialog::getOpenFileNames(this, tr("Open Media"), 
                  settings->value("app/lastUrl").toString(),
                  tr("Audio Files (*.mp3 *.wav *.ogg)"));
                  if (filenames->size() > 0) {
                      playlist->clear();
                      for (int i=0; i < filenames->size(); i++) {
                          QString name(filenames->at(i));
                          QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
                          playlist->addMedia(QUrl(QFileInfo(name).filePath()));
              
                      }
              //rest of the stuff
                  }
              }
              

              And works well

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @UnitScan That indicates that the problem is in how QMediaPlaylist reads the m3u file and it does it via QTextStream so if QTextStream won't work, the QMediaPlaylist will fail too.

              could you right click on the m3u file, open with->notepad then go to file->save as and explicitly set the encoding to unicode in the combobox next to the save button before saving it?

              after you did it do my test again but, as @raven-worx correcly pointed out, replace

              while(!playRead.atEnd()){
              qDebug() << playRead.readLine().trimmed();
              }
              

              with

              while(!playRead.atEnd()){
              const QString m3uLine = playRead.readLine().trimmed(); //add a watch here please, to be sure
              qDebug() << m3uLine .toUtf8().constData();
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • UnitScanU Offline
                UnitScanU Offline
                UnitScan
                wrote on last edited by UnitScan
                #7

                This works well:

                QFile file("C:/Users/Marco/Music/default.m3u");
                if(!file.open(QIODevice::ReadOnly))
                Q_ASSERT(false);
                QTextStream playRead(&file);
                playRead.setCodec("UTF-8"); 
                while(!playRead.atEnd()){
                        const QString m3uLine = playRead.readLine().trimmed(); //add a watch here please, to be sure
                        qDebug() << m3uLine.toUtf8().constData();
                        }
                

                The last problem is when I try to save this result for later use, for example to display the file name in QTableWidget with this code

                 QString mediafile = playRead.readLine().trimmed()..toUtf8().constData()
                 QFileInfo fi(mediafile);
                 QString text = fi.completeBaseName();
                 QTableWidgetItem* filename = new QTableWidgetItem(text);
                 filename->setText(text);
                 filename->setToolTip(fi.completeBaseName());
                 tracklist->setItem(i, 1, filename);
                

                An empty string is returned: http://imgur.com/a/2bwZk

                1 Reply Last reply
                0
                • UnitScanU Offline
                  UnitScanU Offline
                  UnitScan
                  wrote on last edited by UnitScan
                  #8

                  [SOLVED]

                  Replace

                  playlist->load(QUrl::fromLocalFile("C:/Users/Marco/Music/default.m3u"), "m3u");
                  

                  with

                  QFile inputFile("C:/Users/Marco/Music/default.m3u");
                      if (inputFile.open(QIODevice::ReadOnly))
                      {
                         QTextStream in(&inputFile);
                         in.setCodec("UTF-8");
                         while (!in.atEnd())
                         {
                            QString line = in.readLine().trimmed().toUtf8().constData();
                            playlist->addMedia(QUrl(QFileInfo(line).filePath()));
                         }
                         inputFile.close();
                      }
                  

                  and

                  playlist->save(QUrl::fromLocalFile("C:/Users/Marco/Music/default.m3u"), "m3u");
                  

                  with

                  QFile outputFile("C:/Users/Marco/Music/default.m3u");
                  outputFile.open(QIODevice::WriteOnly|QIODevice::Text);
                  for (int i=0; i < playlist->mediaCount(); i++) {
                      //…
                      QTextStream out(&outputFile);
                      out << mediafile[0].toUpper()+mediafile.mid(1) << endl;
                      //…
                  }
                  outputFile.close();
                  
                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved