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. Trying to use QMediaPlayer but I get errors
QtWS25 Last Chance

Trying to use QMediaPlayer but I get errors

Scheduled Pinned Locked Moved Solved General and Desktop
creatormultimediamediaplayersound
3 Posts 2 Posters 1.8k 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.
  • J Offline
    J Offline
    JTrocks55
    wrote on 4 Mar 2017, 02:15 last edited by
    #1

    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.

    mainwindow.h

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

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        player->setMedia(QUrl::fromLocalFile("/Users/Donna/Documents/AbletonProjects/Creepy Project/Unravel.wav"));
        player->setVolume(50);
        player->play();
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        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.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # 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\
            mainwindow.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. :)

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Venkatesh V
      wrote on 4 Mar 2017, 09:17 last edited by
      #2

      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.

      1 Reply Last reply
      2
      • J Offline
        J Offline
        JTrocks55
        wrote on 4 Mar 2017, 13:36 last edited by
        #3

        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!

        1 Reply Last reply
        0

        2/3

        4 Mar 2017, 09:17

        • Login

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