Results 1 to 8 of 8

Thread: Unable to play audio from memory.

  1. #1

    Default Unable to play audio from memory.

    Hi!

    I want to load a sound from memory and play it but I don't heard anything.

    Here is the code :

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QMediaPlayer>
    3. #include <QBuffer>
    4. #include <QIODevice>
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8. unsigned short samples[1000000];
    9. for (unsigned int i = 0; i < sizeof(samples) / sizeof(float); i++) {
    10. samples[i] = 1000;
    11. }
    12. QByteArray byteArray((const char*) samples,sizeof(samples));
    13. QBuffer mediaStream(&byteArray);
    14. QMediaPlayer mp;
    15. mp.setMedia(QMediaContent(),&mediaStream);
    16. mp.play();
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Here is the error message :
    Qt Code:
    1. GStreamer; Unable to pause - ""
    2. Error : "No URI set"
    To copy to clipboard, switch view to plain text mode 
    Last edited by LaurentDuroisin; 28th January 2016 at 10:15.

  2. #2
    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: Unable to play audio from memory.

    Nothing happens because you are calling play() before you have an event loop running (a.exec()). Move your media player code out of main() and into some GUI class (like a QDialog). After that GUI element is visible, then you should be able to start the media player and play a sound.

  3. #3

    Default Re: Unable to play audio from memory.

    Ok, I'll try that thing, thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unable to play audio from memory.

    If it is an event loop issue, just call the play() slot via a single shot timer.

    Cheers,
    _

  5. #5

    Default Re: Unable to play audio from memory.

    Hi, what is a "shot timer" ?

    I've tried to call the play function into a QDialog.

    Qt Code:
    1. #include "mydialog.h"
    2. #include <QMediaPlayer>
    3. #include <QBuffer>
    4. MyDialog::MyDialog()
    5. {
    6. unsigned short samples[1000000];
    7. for (unsigned int i = 0; i < sizeof(samples) / sizeof(float); i++) {
    8. samples[i] = 1000;
    9. }
    10. QByteArray byteArray((const char*) samples,sizeof(samples));
    11. QBuffer mediaStream(&byteArray);
    12. QMediaPlayer mp;
    13. mp.setMedia(QMediaContent(),&mediaStream);
    14. mp.play();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QApplication>
    2. #include "mydialog.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MyDialog dialog;
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    But I've always this message, even if I set play into a loop.

    Qt Code:
    1. GStreamer; Unable to pause - ""
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unable to play audio from memory.

    Quote Originally Posted by LaurentDuroisin View Post
    Hi, what is a "shot timer" ?
    A single shot timer is a timer that only fires once.
    See QTimer::singleShot().

    Quote Originally Posted by LaurentDuroisin View Post
    I've tried to call the play function into a QDialog.

    Qt Code:
    1. #include "mydialog.h"
    2. #include <QMediaPlayer>
    3. #include <QBuffer>
    4. MyDialog::MyDialog()
    5. {
    6. unsigned short samples[1000000];
    7. for (unsigned int i = 0; i < sizeof(samples) / sizeof(float); i++) {
    8. samples[i] = 1000;
    9. }
    10. QByteArray byteArray((const char*) samples,sizeof(samples));
    11. QBuffer mediaStream(&byteArray);
    12. QMediaPlayer mp;
    13. mp.setMedia(QMediaContent(),&mediaStream);
    14. mp.play();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Your "mp" is a variable on the stack of the MyDialog constructor. It will be destroyed once the constructor ends.

    Cheers,
    _

  7. #7
    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: Unable to play audio from memory.

    Your "mp" is a variable on the stack of the MyDialog constructor. It will be destroyed once the constructor ends.
    Not only that, but if you are constructing the MyDialog instance on the stack in main() *before* calling app.exec(), then you *still* don't have an event loop running at the point the constructor code is executing.

    Add a button to your dialog and a slot in which you execute the play code. Connect the button's clicked() signal to this slot in the MyDialog constructor.

  8. #8

    Default Re: Unable to play audio from memory.

    Ok I'll try that.

Similar Threads

  1. How to record and play audio on N8
    By doubleti in forum Newbie
    Replies: 0
    Last Post: 1st December 2011, 11:46
  2. play audio file
    By hema in forum Qt Quick
    Replies: 1
    Last Post: 11th August 2011, 05:27
  3. how to play audio file in qml
    By hema in forum Newbie
    Replies: 0
    Last Post: 10th August 2011, 10:57
  4. Unable to play audio outside Qt creator
    By somnathbanik in forum Newbie
    Replies: 0
    Last Post: 9th November 2010, 06:47
  5. play audio url
    By tungvc in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2010, 11:04

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.