Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Trying to use QMediaPlayer but I get errors

    General and Desktop
    creator multimedia media player sound
    2
    3
    1245
    Loading More Posts
    • 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
      JTrocks55 last edited by

      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 Reply Quote 0
      • Venkatesh V
        Venkatesh V last edited by

        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 Reply Quote 2
        • J
          JTrocks55 last edited by

          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 Reply Quote 0
          • First post
            Last post