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. Media player playlist using PyQT
Forum Updated to NodeBB v4.3 + New Features

Media player playlist using PyQT

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 4.6k Views 1 Watching
  • 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.
  • Y Offline
    Y Offline
    yanisk
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        yanisk
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          1
          • Y Offline
            Y Offline
            yanisk
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1
              • Y Offline
                Y Offline
                yanisk
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  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.

                  1 Reply Last reply
                  1
                  • Y Offline
                    Y Offline
                    yanisk
                    wrote on last edited by
                    #9

                    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)
                    
                    mrjjM 1 Reply Last reply
                    0
                    • Y yanisk

                      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)
                      
                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      1

                      • Login

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