Media player playlist using PyQT



  • I've some problems making this work. I wanna use playlist to play audio files. here is my code:

    playlist = QMediaPlaylist()
    url = QUrl.fromLocalFile("/home/user/Downloads/ss.mp3")
    playlist.addMedia(QMediaContent(url))
    
    
    player.setPlaylist(playlist)
    player.playlist().setCurrentIndex(0)
    player.play()
    

    The problem is that when I feed the file as unique media source (player.serMedia()) it works, but when I run the code above, the music doesn't play!

    What's the problem here?


  • Qt Champions 2016

    Hi
    Disclaimer. Not a python user.

    Did you try to see if
    QMediaPlaylist::error()
    says something ?

    also
    bool QMediaPlaylist::isEmpty() const
    or
    int QMediaPlaylist::mediaCount() const

    to see if list is actually loaded ?



  • @mrjj QMediaPlaylist::error() =0 and QMediaPlaylist::mediaCount() =1 So, I think the playlist is ok. But the player doesn't play its content.


  • Qt Champions 2016

    Hmm, yes it does sound like something loaded.

    You seems to do exactly as
    https://meetingcpp.com/index.php/br/items/building-an-mp3-player-with-qt5.html

    Is this the same place/ code as when you test (player.setMedia())

    I wonder if it runs out of scope or anything like that.



  • When I load a single file, it's fine. like this:

                local = QUrl(audio.url)
                media = QMediaContent(local)
                player.setMedia(media)
                player.play()
    

    But the code for playlist doesn't start the playback.


  • Qt Champions 2016

    @yanisk
    Hmm. Sorry can't spot anything that looks suspicious.

    I would have a look at
    currentMedia
    and see if all looks good inside.

    I wonder ifs the QUrl that is the issue but you are using full path so seems fine.



  • When I check

    player.play()
    print(player.currentMedia().canonicalUrl())
    

    Gives :

    PyQt5.QtCore.QUrl('file:///home/user/Downloads/ss.mp3')
    
    

    Which means the current media is indeed correct, yet the player doesn't start playing it.


  • Qt Champions 2016

    Hi
    This works in c++

    void MainWindow::on_pushButton_released() {
      auto player = new QMediaPlayer;
      auto playlist = new QMediaPlaylist(player);
      playlist->addMedia(QUrl::fromLocalFile("e:/1.mp3"));
      playlist->addMedia(QUrl::fromLocalFile("e:/2.mp3"));
      playlist->addMedia(QUrl::fromLocalFile("e:/3.mp3"));
      playlist->setCurrentIndex(1);
      player->setPlaylist(playlist);
      player->play();
    }
    

    Its starts playing at once.



  • Thanks man!

    Now I see my mistake. It's when I created the playlist object, I didn't provide QMediaplayer player as a parameter. Now, It works!

            playlist = QMediaPlaylist(player)

  • Qt Champions 2016

    @yanisk
    ahh, i totally missed that. good found :)
    You had player.setPlaylist(playlist) so i thought it was enough "binding"


Log in to reply
 

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