Results 1 to 13 of 13

Thread: QMediaPlaylist loop doesn't work in console

  1. #1
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QMediaPlaylist loop doesn't work in console

    Sorry if my qustion is lame, but i am new in Qt. So, I have a console application in QT, and i use c++. I would like to create a text-based mini game and i need some background music. I used QMediaPlayer QT class for this, and it worked... but i realise i can not loop the music. I found another QT Class for this the QMediaPlaylist. But the loop function did not work (it played just once). I tried it in a maindwindow application and if i used in the mainwindow.cpp it worked fine. And i dont know why, and where is my fault? I tried this way:

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. Menu();
    5.  
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    menu.cpp
    Qt Code:
    1. Menu::Menu(QObject *parent) : QObject(parent)
    2. {
    3. ///...........
    4. this->mp = new MediaPlay();
    5. mp->PlayMusic("backgroundmusic.mp3");
    6. ///...........
    7. }
    To copy to clipboard, switch view to plain text mode 
    Mediaplay.cpp
    Qt Code:
    1. MediaPlay::MediaPlay(QObject *parent) : QObject(parent)
    2. {
    3.  
    4. }
    5.  
    6. void MediaPlay::PlayMusic(QString name)
    7. {
    8. this->playlist = new QMediaPlaylist(this);
    9. this->settings = new QSettings(QDir::currentPath() + "/settings.ini", QSettings::IniFormat);
    10. playlist->addMedia(QUrl::fromLocalFile(".//res//"+name));
    11. playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
    12.  
    13. this->player = new QMediaPlayer(this);
    14. player->setVolume(settings->value("iVolume").toInt());
    15. player->setPlaylist(playlist);
    16. player->play();
    17. }
    18.  
    19. void MediaPlay::SetVolume(int valume)
    20. {
    21. settings->setValue("iVolume",valume);
    22. player->setVolume(valume);
    23. }
    To copy to clipboard, switch view to plain text mode 
    /------
    I have an exit function in Menu class. In the exit function i call "qApp->quit();"
    and then not close the console, it sarts playing the background music in loop... but why?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMediaPlaylist loop doesn't work in console

    When you had it in a GUI app at what point in time was the Menu object created?

    It is quite posible that the the event loop needs to be running before the player starts playing (or maybe before creation).

  3. #3
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    Quote Originally Posted by ChrisW67 View Post
    When you had it in a GUI app at what point in time was the Menu object created?

    It is quite posible that the the event loop needs to be running before the player starts playing (or maybe before creation).
    when i tried in the gui application i didn't create menu object so i just use the player in this way:
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtMultimedia/QtMultimedia>
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. this->playlist = new QMediaPlaylist(this);
    8. playlist->addMedia(QUrl::fromLocalFile("kula.mp3"));
    9. playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
    10.  
    11. this->player = new QMediaPlayer(this);
    12. player->setPlaylist(playlist);
    13. player->play();
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. //delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtMultimedia/QMediaPlaylist>
    6. #include <QtMultimedia/QMediaPlayer>
    7.  
    8. QT_BEGIN_NAMESPACE
    9. namespace Ui { class MainWindow; }
    10. QT_END_NAMESPACE
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow(QWidget *parent = nullptr);
    18. ~MainWindow();
    19.  
    20. QMediaPlaylist *playlist;
    21. QMediaPlayer *player ;
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25. };
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    and i didn't touch the main.cpp...
    hmm and how can i create the event loop for this problem? (I didn't create event loop before)
    btw really thanks to your help!!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    Are you sure the path you are building in PlayMusic() actually points to the correct location of your np3 file? Put in a qDebug() statement or two to print the absolute path to the file (QDir::absolutePath()) and see if that's where your file is actually located. (Note that absolutePath() does not verify that the file exists, it just turns "." and ".." relative paths into the fully-expanded path).

    When you run your app in Qt Creator and when you run it standalone, quite often your working directory is not the same and not where you think it is.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    Quote Originally Posted by d_stranz View Post
    Are you sure the path you are building in PlayMusic() actually points to the correct location of your np3 file? Put in a qDebug() statement or two to print the absolute path to the file (QDir::absolutePath()) and see if that's where your file is actually located. (Note that absolutePath() does not verify that the file exists, it just turns "." and ".." relative paths into the fully-expanded path).

    When you run your app in Qt Creator and when you run it standalone, quite often your working directory is not the same and not where you think it is.
    yeah i understand what you say... but the problem is not the reach the file, because in the console application (which I want to achieve) play the music, but just once, not in the loop. And when i quit the application (just in the menu class) and the return a.exec(); execute and not close the app.. start the music in loop. And when i tried it in the gui application (as I showed above) its work perfectly. So the path is fine.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    I am not sure I understand what you are trying to do. In your Menu constructor, you are creating a MediaPlay instance with no parent. This means that the instance is not owned by the Menu class, but is a top-level QObject owned by the app itself. So when your Menu class goes out of scope, the MediaPlay instance is still valid.

    In line 11 of Mediaplay.cpp, you tell the media player to loop. (setPlaybackMode())

    So what I think is happening is that the QMediaPlayer instance is still "alive" even though your app has exited, so the music continues to play in a loop. It is possible that the QMediaPlayer sets up its own event loop to play music, so even though the main event loop has exited (a.exec() ends), the media player's loop is still running.

    So try this: In the Menu constructor, pass "this" to the constructor for MediaPlay and see if that makes a difference.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    i dont really get it, i try to do this? :
    Qt Code:
    1. Menu::Menu(QObject *parent) : QObject(parent)
    2. {
    3. this->mp = new MediaPlay(this);
    4. mp->PlayMusic("backgroundmusic.mp3");
    5. }
    To copy to clipboard, switch view to plain text mode 
    and the problem could be that my mediaplay class constructor dont have any parents?
    btw, my problem is not that the music plays in a loop when i try to quit from the program ( i can play around that a simple return 0) I just dont get it why this happen.
    MY Problem is when i start the program and call my menu constructor...
    Qt Code:
    1. Menu::Menu(QObject *parent) : QObject(parent)
    2. {
    3. ///...........
    4. this->mp = new MediaPlay();
    5. mp->PlayMusic("backgroundmusic.mp3");
    6. ///...........
    7. }
    To copy to clipboard, switch view to plain text mode 
    ... than plays the music but just once not in the loop .. dont repeat the music when finished
    so i want loop in my background music but this way it's only play once

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    I think you need to create a simple program that shows the problem and post the -complete- code here (all classes, all methods, not just pieces). If your console program is simple, then just post that.

    It is really hard to understand from your description what is happening. You said your Menu class calls qApp->quit(). Where, when? If you are doing that, then why are you surprised if the music stops?

    And you said this in your first post:

    I have an exit function in Menu class. In the exit function i call "qApp->quit();"
    and then not close the console, it starts playing the background music in loop... but why?
    and this in your last post:

    ... then plays the music but just once not in the loop .. don't repeat the music when finished
    So which is it? Does it loop or not?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    Quote Originally Posted by d_stranz View Post
    I think you need to create a simple program that shows the problem and post the -complete- code here (all classes, all methods, not just pieces). If your console program is simple, then just post that.

    It is really hard to understand from your description what is happening. You said your Menu class calls qApp->quit(). Where, when? If you are doing that, then why are you surprised if the music stops?

    And you said this in your first post:



    and this in your last post:



    So which is it? Does it loop or not?

    Sorry for my bad english but i said the same first and last time too.
    The most importent is the main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. Menu();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    SO, I mentioned that, when i call the qApp->quit() in the Menu class. Menu object is over jump to the "return a.exec();" and in this line starts the Music loop in the correct way BUT ITS NOT THE PROBLEM because
    i can handel it (but i mentioned it cuze its interesting.. its working the great why but its wrong place where start the loop in the correct way).
    BUT, What i want to achive is when i call the menu constructor and i start the music in the loop. But what happens the music is just ONCE playing. This point where i want the loop.

    If u still dont get it, i try to introduce it with code in a simple way. btw i really thank to you to try to help me I am really sad it still dosn't work

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMediaPlaylist loop doesn't work in console

    Here is a single-file, stripped down version of your program:
    Qt Code:
    1. QT -= gui
    2. QT += multimedia
    3. CONFIG += c++11 console
    4. CONFIG -= app_bundle
    5. DEFINES += QT_DEPRECATED_WARNINGS
    6.  
    7. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QMediaPlayer>
    3. #include <QMediaPlaylist>
    4.  
    5. class MediaPlay: public QObject {
    6. Q_OBJECT
    7. public:
    8. MediaPlay(QObject *parent = nullptr) : QObject(parent) { }
    9.  
    10. void PlayMusic(QString name) {
    11. playlist = new QMediaPlaylist(this);
    12. playlist->addMedia(QUrl::fromLocalFile("/tmp/untitled/res/"+name));
    13. playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
    14.  
    15. player = new QMediaPlayer(this);
    16. player->setVolume(50);
    17. player->setPlaylist(playlist);
    18. player->play();
    19. }
    20.  
    21. private:
    22. QMediaPlayer *player;
    23. QMediaPlaylist *playlist;
    24. };
    25.  
    26. class Menu: public QObject {
    27. Q_OBJECT
    28. public:
    29. Menu(QObject *parent = nullptr) : QObject(parent)
    30. {
    31. mp = new MediaPlay(this);
    32. mp->PlayMusic("backgroundmusic.mp3");
    33. }
    34. private:
    35. MediaPlay *mp;
    36. };
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QCoreApplication a(argc, argv);
    41. Menu menu;
    42. return a.exec();
    43. }
    44.  
    45. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    This compiles and runs as expected on Qt 5.15 under Linux.

    I removed the settings stuff because QDir::currentPath() will almost certainly not be where you expect at all times.

    Change the absolute path to your MP3 and try this.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  11. #11
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    Quote Originally Posted by ChrisW67 View Post
    Here is a single-file, stripped down version of your program:
    Qt Code:
    1. QT -= gui
    2. QT += multimedia
    3. CONFIG += c++11 console
    4. CONFIG -= app_bundle
    5. DEFINES += QT_DEPRECATED_WARNINGS
    6.  
    7. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QMediaPlayer>
    3. #include <QMediaPlaylist>
    4.  
    5. class MediaPlay: public QObject {
    6. Q_OBJECT
    7. public:
    8. MediaPlay(QObject *parent = nullptr) : QObject(parent) { }
    9.  
    10. void PlayMusic(QString name) {
    11. playlist = new QMediaPlaylist(this);
    12. playlist->addMedia(QUrl::fromLocalFile("/tmp/untitled/res/"+name));
    13. playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
    14.  
    15. player = new QMediaPlayer(this);
    16. player->setVolume(50);
    17. player->setPlaylist(playlist);
    18. player->play();
    19. }
    20.  
    21. private:
    22. QMediaPlayer *player;
    23. QMediaPlaylist *playlist;
    24. };
    25.  
    26. class Menu: public QObject {
    27. Q_OBJECT
    28. public:
    29. Menu(QObject *parent = nullptr) : QObject(parent)
    30. {
    31. mp = new MediaPlay(this);
    32. mp->PlayMusic("backgroundmusic.mp3");
    33. }
    34. private:
    35. MediaPlay *mp;
    36. };
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QCoreApplication a(argc, argv);
    41. Menu menu;
    42. return a.exec();
    43. }
    44.  
    45. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    This compiles and runs as expected on Qt 5.15 under Linux.

    I removed the settings stuff because QDir::currentPath() will almost certainly not be where you expect at all times.

    Change the absolute path to your MP3 and try this.


    okaaay its working!!!!! )) YOU ARE MY HERO thank you a lot!! Any idea my version why was wrong? Thnak you again!

  12. #12
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    Quote Originally Posted by ChrisW67 View Post
    Here is a single-file, stripped down version of your program:
    Qt Code:
    1. QT -= gui
    2. QT += multimedia
    3. CONFIG += c++11 console
    4. CONFIG -= app_bundle
    5. DEFINES += QT_DEPRECATED_WARNINGS
    6.  
    7. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QMediaPlayer>
    3. #include <QMediaPlaylist>
    4.  
    5. class MediaPlay: public QObject {
    6. Q_OBJECT
    7. public:
    8. MediaPlay(QObject *parent = nullptr) : QObject(parent) { }
    9.  
    10. void PlayMusic(QString name) {
    11. playlist = new QMediaPlaylist(this);
    12. playlist->addMedia(QUrl::fromLocalFile("/tmp/untitled/res/"+name));
    13. playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
    14.  
    15. player = new QMediaPlayer(this);
    16. player->setVolume(50);
    17. player->setPlaylist(playlist);
    18. player->play();
    19. }
    20.  
    21. private:
    22. QMediaPlayer *player;
    23. QMediaPlaylist *playlist;
    24. };
    25.  
    26. class Menu: public QObject {
    27. Q_OBJECT
    28. public:
    29. Menu(QObject *parent = nullptr) : QObject(parent)
    30. {
    31. mp = new MediaPlay(this);
    32. mp->PlayMusic("backgroundmusic.mp3");
    33. }
    34. private:
    35. MediaPlay *mp;
    36. };
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QCoreApplication a(argc, argv);
    41. Menu menu;
    42. return a.exec();
    43. }
    44.  
    45. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    This compiles and runs as expected on Qt 5.15 under Linux.

    I removed the settings stuff because QDir::currentPath() will almost certainly not be where you expect at all times.

    Change the absolute path to your MP3 and try this.

    It's woking, but i have one more question... i realize when i am waiting for an input so "int input; cin >> input;" and the music is stop any advice?

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMediaPlaylist loop doesn't work in console

    It's woking, but i have one more question... i realize when i am waiting for an input so "int input; cin >> input;" and the music is stop any advice?
    "cin >> input;" is basically a blocking operation. It stops your program and waits for the user to type something. When the user hits Return, it puts the text into the variable and resumes execution. A console program has only one thread, so if that thread is blocked, everything is blocked.

    There is a way around this, by creating a socket connection to stdin and using Qt's event loop to listen for input on that socket. See this post. Since the media player and socket both use the event loop, this lets both run at the same time and the event loop will handle both of them.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. The following user says thank you to d_stranz for this useful post:

    Kaguro (4th July 2020)

Similar Threads

  1. Replies: 0
    Last Post: 21st June 2020, 18:47
  2. Console Doesn't Stay Open
    By Atomic_Sheep in forum General Programming
    Replies: 0
    Last Post: 9th October 2015, 12:09
  3. Replies: 2
    Last Post: 12th May 2014, 15:06
  4. Replies: 4
    Last Post: 6th August 2011, 02:40
  5. JavaScript for loop won't work
    By Asperamanca in forum Qt Quick
    Replies: 2
    Last Post: 15th October 2010, 22:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.