QMediaPlayer and QMediaPlaylist - problem with next



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



  • @refresh


  • Lifetime Qt Champion

    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 ?



  • Crashes, I just click on the "Next"


  • Lifetime Qt Champion

    What does a run with the debugger tell you ?



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


  • Lifetime Qt Champion

    How does your main.cpp look like ?

    Also, why make player and playlist static ?



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


  • Lifetime Qt Champion

    Looks right.

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



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


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

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




Log in to reply
 

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