Trying to use QMediaPlayer but I get errors

  • Using Qt Creator 5.8

    Hi! So I have been trying to figure out how to use Qt's Multimedia, and I was getting errors, so I used "Run qmake", but now the program doesn't work and I don't get the errors anymore so I can't post what they said, but I can show you the code.


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QMediaPlayer>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
    private slots:
        void on_pushButton_clicked();
        Ui::MainWindow *ui;
        QMediaPlayer *player;
    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        delete ui;
    void MainWindow::on_pushButton_clicked()
        player->setMedia(QUrl::fromLocalFile("/Users/Donna/Documents/AbletonProjects/Creepy Project/Unravel.wav"));


    #include "mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow w;;
        return a.exec();

    project file

    # Project created by QtCreator 2017-02-28T08:50:49
    QT       += core gui
    QT       += multimedia
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = MusicMachine
    TEMPLATE = app
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    SOURCES += main.cpp\
    HEADERS  += mainwindow.h
    FORMS    += mainwindow.ui

    So I don't see what's wrong here, and what happens now is when I run the program and click the button that is supposed to play the sound, the program just stops running. It gives the "program name has stopped working" error.

    So any help is appreciated and thanks in advance! Let me know if there is more info I can give. :)

  • Hi @JTrocks55
    You are using mediaPlayer pointer without allocating memory so it gets crash.
    include this line constructor.
    player = new QMedeaPlayer;
    hope it help you.

  • It worked! I had done it before but it was adding to the errors, and when I solved those I forgot to re add that. Thanks!