1

Hi I am still learning C++ and QT for my major project for school this year and I would like some help with some syntax of C++ and using certain QT functions. As I am making a media manager, I have managed to get a song to play from pressing a button from a form. Now I want to pause the same song by pressing another button, but I am not completely sure what to do, could you help?

I already have this to play a song:

void MainWindow::playAudioFile(){
    QMediaPlayer *player = new QMediaPlayer(this);
    player->setMedia(QUrl::fromLocalFile("LOCATION OF SONG FILE"));
    player->setVolume(50);
    player->play();
}   

But I want to know how to pause the same audioi file from the QMediaPlayer called 'player', and at the moment all I have thought of is this and I am not sure if I am doing it correctly:

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

Both of these functions (if that is what they are called) begin with a button press, which I know works for the first one.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
Kinexd
  • 33
  • 3
  • 8

1 Answers1

1

You are trying to access a non-accessible object here:

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

I am surprised if it was even compiling for you. The solution would be to change this:

QMediaPlayer *player = new QMediaPlayer(this);

to

player = new QMediaPlayer(this);

where the "player" object is the member of your MainWindow class, so basically you would put this into your MainWindow class:

#include <QMainWindow>
#include <QMediaPlayer>

class MainWindow : public QMainWindow
{
    Q_OBJECT
    public:
        explicit MainWindow(QObject *parent = 0)
            : QObject(parent)
            , player(new MediaPlayer(this))
    ...
    public slots:
        void playAudioFile() {
            player->setMedia(QUrl::fromLocalFile("LOCATION OF SONG FILE"));
            player->setVolume(50);
            player->play();
        }
        void pauseAudioFile(){
            player->pause();
        }
    private:
        QMediaPlayer *player;
}

That being said, you may not need a heap object in this case at all, and you can start using a stack object without dynamic memory allocation.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • @Kinexd: sure, you have to do either `#include QMediaPlayer` in the header or `class QMediaPlayer;` before the class declaration, and include it in the source. – László Papp May 05 '14 at 10:42
  • Sorry, but I am getting many errors trying your solution and it is confusing me. So inside the `void MainWindow::playAudioFile()` i have changed `QMediaPlayer *player = new QMediaPlayer(this);` to `player = new QMediaPlayer(this);`. Then in the MainWindow class I changed it to how you put it. – Kinexd May 05 '14 at 10:48
  • 1
    @Kinexd: fyi, it is not a Qt issue, but generic C++. I would suggest to read upon classes in C++. It is important to know how members work. – László Papp May 05 '14 at 10:49
  • I really want to learn more C++ as you can tell I don't know a lot. Would you recommend anything that will help me learn C++ for my purposes? – Kinexd May 05 '14 at 10:51
  • 1
    @Kinexd: I updated the answer to be copy/pasteable, but please take a look [at the recommended books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – László Papp May 05 '14 at 11:04