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. QMediaPlayer and QMediaPlaylist - problem with next
QtWS25 Last Chance

QMediaPlayer and QMediaPlaylist - problem with next

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 5.9k 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.
  • M Offline
    M Offline
    maniek1310
    wrote on last edited by
    #1

    Hi, I have a problem with the function next (). When you click on the "Next" button, the program automatically closes. Maybe you have any ideas how to fix it?

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtMultimedia/QMediaPlayer>
    #include <QTime>
    #include <QFileDialog>
    #include <QFile>
    #include <QFileInfo>
    #include <QMediaPlaylist>

    using namespace std;

    QMediaPlayer *player = new QMediaPlayer();
    QMediaPlaylist *playlist = new QMediaPlaylist;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QObject::connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(aktualna_pozycja(qint64)));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::run_file(QString file)
    {
    QFile file_1(file);

    QFileInfo fileInfo(file_1.fileName());
    QString name(fileInfo.fileName());
    
    ui->name_music->setText(name);
    
    playlist->addMedia(QUrl::fromLocalFile&#40;file&#41;);
    playlist->addMedia(QUrl::fromLocalFile&#40;"K:/Anakonda.mp3"&#41;);
    playlist->setCurrentIndex(0);
    
    player->setPlaylist(playlist);
    
    player->setVolume(80);
    //player->setMedia(QUrl::fromLocalFile&#40;file&#41;);
    player->play();
    
    //ui->label_2->setText(file);
    

    }

    void MainWindow::on_pushButton_clicked()
    {
    player->play();
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    player->stop();
    }
    void MainWindow::on_horizontalSlider_sliderMoved(int position)
    {
    player->setVolume(position);
    ui->glosnosc->setText(QString::number(position) + QString("%"));
    }

    void MainWindow::aktualna_pozycja(qint64 position)
    {
    qint64 duration = player->duration();

    int ile_procent = 0;
    int seconds1 = (position / 1000) % 60;
    int minutes1 = (position / 60000) % 60;
    int hours1 = (position / 3600000) % 24;
    int seconds2 = (duration / 1000) % 60;
    int minutes2 = (duration / 60000) % 60;
    int hours2 = (duration / 3600000) % 60;
    
    QTime time_akt(hours1, minutes1, seconds1);
    QTime time_end(hours2, minutes2, seconds2);
    
    ui->akt_poz->setText(time_akt.toString());
    ui->end_poz->setText(time_end.toString());
    
    ile_procent = (position * 100) / duration;
    
    ui->ile_trwa->setValue(ile_procent);
    

    }

    void MainWindow::on_pushButton_3_clicked()
    {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Wybierz piosenkę"), "C:\", tr("Pliki MP3 (*.mp3)"));

    if(fileName != "")
        run_file&#40;fileName&#41;;
    

    }

    void MainWindow::on_pause_clicked()
    {
    player->pause();
    }

    void MainWindow::on_next_file_clicked()
    {
    playlist->next();
    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maniek1310
      wrote on last edited by
      #2

      @refresh

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Please practice some patience. Allow at least 24 hours before bumping your own thread. People on this forum don't necessarily lives in the same time zone as you.

        That said, what do you mean by close ? The application just stops ? Crashes ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maniek1310
          wrote on last edited by
          #4

          Crashes, I just click on the "Next"

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What does a run with the debugger tell you ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maniek1310
              wrote on last edited by
              #6

              http://imageshack.com/a/img199/3483/1ljx.png

              Console : Rozpoczęto debugowanie
              QEventLoop: Cannot be used without QApplication
              QObject::connect: Cannot connect (null)::aboutToQuit() to QNativeWifiEngine::closeHandle()

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                How does your main.cpp look like ?

                Also, why make player and playlist static ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maniek1310
                  wrote on last edited by
                  #8

                  main.cpp
                  @#include "mainwindow.h"
                  #include <QApplication>

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();

                  return a.exec&#40;&#41;;
                  

                  }
                  @

                  mainwindow.h
                  @#ifndef MAINWINDOW_H
                  #define MAINWINDOW_H

                  #include <QMainWindow>

                  namespace Ui {
                  class MainWindow;
                  }

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT

                  public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();

                  private slots:
                  void on_pushButton_clicked();

                  void on_pushButton_2_clicked();
                  
                  void on_horizontalSlider_sliderMoved(int position);
                  
                  void aktualna_pozycja(qint64 position);
                  
                  void on_pushButton_3_clicked();
                  
                  void run_file&#40;QString file&#41;;
                  
                  void on_pause_clicked();
                  
                  void on_next_file_clicked();
                  
                  void on_pushButton_4_clicked();
                  

                  private:
                  Ui::MainWindow *ui;
                  };

                  #endif // MAINWINDOW_H
                  @

                  test_sound.pro
                  @#-------------------------------------------------

                  Project created by QtCreator 2014-02-04T19:08:13

                  #-------------------------------------------------

                  QT += core gui
                  QT += widgets multimedia
                  QT += multimedia
                  QT += core gui multimedia

                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

                  TARGET = test_sound
                  TEMPLATE = app

                  SOURCES += main.cpp
                  mainwindow.cpp

                  HEADERS += mainwindow.h

                  FORMS += mainwindow.ui
                  @

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Looks right.

                    Please move both player and playlist has non static member variable of MainWindow and try again

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      maniek1310
                      wrote on last edited by
                      #10

                      I do not understand. I could explain it somehow differently?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @QMediaPlayer *player = new QMediaPlayer();
                        QMediaPlaylist *playlist = new QMediaPlaylist;@

                        These are static, remove them. And add two member variable in MainWindow to replace them.

                        @mainwindow.h
                        class MainWindow : public QMainWindow {
                        …

                        private:
                        QMediaPlayer *_player;
                        QMediaPlaylist *_playlist;

                        }

                        mainwindow.cpp

                        MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow),
                        _player(new QMediaPlayer),
                        _playlist(new QMediaPlayList)
                        {
                        ui->setupUi(this);

                        QObject::connect(_player, SIGNAL(positionChanged(qint64)), this, SLOT(aktualna_pozycja(qint64)));
                        

                        }
                        @

                        Something like that

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          maniek1310
                          wrote on last edited by
                          #12

                          @mainwindow.h
                          ...
                          private:
                          Ui::MainWindow *ui;
                          QMediaPlayer *player;
                          QMediaPlayer *playlist;
                          };@

                          @mainwindow.cpp
                          ...
                          MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow),
                          player(new QMediaPlayer),
                          playlist(new QMediaPlaylist)
                          {
                          ui->setupUi(this);

                          QObject::connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(aktualna_pozycja(qint64)));
                          

                          }
                          ...
                          @

                          błąd: cannot convert 'QMediaPlaylist*' to 'QMediaPlayer*' in initialization
                          playlist(new QMediaPlaylist)

                          błąd: 'class QMediaPlayer' has no member named 'addMedia' playlist->addMedia(QUrl::fromLocalFile(file));

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            The error is pretty clear, you declared playlist list as a QMediaPlayer and not QMediaPlayList.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              maniek1310
                              wrote on last edited by
                              #14

                              Okay, thank you. Now the program compiles correctly, but it is still the same problem.

                              After pressing the "Next" button shows up is:

                              http://imageshack.com/a/img577/9630/0p87.png

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Run your program using the debugger, that should catch the problem

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  maniek1310
                                  wrote on last edited by
                                  #16

                                  http://imageshack.com/a/img199/3483/1ljx.png

                                  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