Reading utf8 QMediaPlaylist



  • 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?



  • 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.



  • 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();
            }
    

  • Moderators

    @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.



  • 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



  • @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();
    }
    


  • 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



  • [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();
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.