Results 1 to 17 of 17

Thread: QNetworkAccessManager and phonon - can it be done?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QNetworkAccessManager and phonon - can it be done?

    I have a problem with phonon: I need to stream audio from a password protected site, and there doesn't seem to be any way to set authentication information in Phonon::MediaSource, so it displays the default Windows authentication dialog for every file I play. I figured that maybe I could use QNetworkAccessManager to stream and phonon to play, which would solve the authentication problem, but so far I wasn't able to get the two to work together.

    I tried a lot of different variations, all with the basic idea that QNetworkReply inherits from QIODevice, so I should be able to set it as setCurrentSource of MediaObject. But it just refuses to play while streaming. It does work if I call play() after the entire download finishes. But it won't play while it downloads. Here's a simplified example to give you the idea:

    Qt Code:
    1. QNetworkRequest request;
    2. request.setUrl(QUrl("http://localhost/mm132.mp3"));
    3. networkReply = networkAccessManager.get(request);
    4.  
    5. mediaObject = Phonon::createPlayer(Phonon::MusicCategory);
    6. mediaObject->setCurrentSource(networkReply);
    7. mediaObject->play()
    To copy to clipboard, switch view to plain text mode 

    I also tried creating a separate QByteArray and write to it in readReady() and then use it with QBuffer in read-only mode, assigning it to MediaSource, but it won't work either. Maybe my thinking is too simplistic and it needs some more complex buffering system...

    Anyone has any suggestions on how this can be done? (Can it be done?) Or is there a way to overwrite that default authentication dialog in phonon and supply the user name and password some other way?

    I would appreciate any ideas or snippets of code you might have. Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and phonon - can it be done?

    How did you try to set the auth data on the request? Did you use the appropriate auth mechanism?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and phonon - can it be done?

    Thank you for answering. As far as the proper authentication mechanism on QNetworkAccessManager request, then yes, I did. I get the data downloaded properly, it's the playing of that data with phonon that doesn't work.

    As far as Phonon::MediaSource, there doesn't seem to be a mechanism for setting username and password. That's the problem.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and phonon - can it be done?

    Have you ensured there is enough data downloaded for the media to be recognized by the player? You can start playing immediately after sending the request, remember there is no buffering involved.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and phonon - can it be done?

    I thought about this, so I tried playing only after the buffer is a certain size, but it didn't work either. I'm not sure how much data is enough... But tell me, is this approach even doable (simply)? Because if you can see that this won't work properly (in a user-friendly fashion) without writing some complex buffering system, than I won't be spending time on it. I can use a flash player in a QWebView to accomplish pretty much the same thing, and that will work for sure.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and phonon - can it be done?

    Quote Originally Posted by serenti View Post
    I thought about this, so I tried playing only after the buffer is a certain size, but it didn't work either. I'm not sure how much data is enough... But tell me, is this approach even doable (simply)?
    If it works when the download is complete when reading from the network reply then it should also work during download. It's hard for me to say how much of the data should be available, it probably depends on the codec used. I would allow at least a megabyte to be downloaded before I started playing... Also make sure the network reply object is open in a proper mode.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and phonon - can it be done?

    Thank you for your comments. I think I'll try using a Flash player in a QWebView instead. It seems simpler and works out of the box. I'll just need to check if flash objects work on all three platforms.

    Pozdrawiam.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and phonon - can it be done?

    I just made a small example application and it works fine for me. I can download a file using QNetworkAccessManager and set the stream being downloaded as a phonon media source. The setting routine looks like so:
    Qt Code:
    1. void downloadProgress(qint64 cur,qint64 tot) {
    2. if(!loaded && cur>1024){
    3. qDebug() << "Loading...";
    4. pl->load(Phonon::MediaSource((QIODevice*)sender()));
    5. connect(pl->mediaObject(), SIGNAL(stateChanged(Phonon::State,Phonon::State)),
    6. this, SLOT(onStateChanged(Phonon::State,Phonon::State)));
    7. loaded = true;
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    where "pl" is a Phonon::VideoPlayer and "loaded" is a boolean where I check if I have already set the source on the player.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and phonon - can it be done?

    There must be something wrong on my end, because I can't make it play with your sample code either. Could you take a look? The file does download, the video player does work when used directly with the url, all the slots are connected and execute... Any ideas?

    Qt Code:
    1. test::test(QWidget *parent, Qt::WFlags flags)
    2. : QDialog(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6. pl = new Phonon::VideoPlayer(Phonon::MusicCategory, this);
    7.  
    8. // this plays fine, meaning the video player does work:
    9. // pl->load(Phonon::MediaSource(QUrl("http://localhost/hello.mp3")));
    10. // pl->play();
    11. // return;
    12.  
    13. loaded = false;
    14. networkManager = new QNetworkAccessManager(this);
    15. connect(networkManager, SIGNAL(finished(QNetworkReply*)),
    16. this, SLOT(networkManagerFinished(QNetworkReply*)));
    17. connect(networkManager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
    18. this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*)));
    19.  
    20. networkReply = networkManager->get(QNetworkRequest(QUrl("http://localhost/hello.mp3")));
    21. connect(networkReply, SIGNAL(downloadProgress(qint64, qint64)),
    22. this, SLOT(downloadProgress(qint64, qint64)));
    23. }
    24.  
    25. test::~test()
    26. {
    27. }
    28.  
    29. void test::downloadProgress(qint64 cur,qint64 tot)
    30. {
    31. if(!loaded && cur > 1024)
    32. {
    33. qDebug() << "Loading...";
    34. pl->load(Phonon::MediaSource((QIODevice*)sender()));
    35. connect(pl->mediaObject(), SIGNAL(stateChanged(Phonon::State,Phonon::State)),
    36. this, SLOT(onStateChanged(Phonon::State, Phonon::State)));
    37. loaded = true;
    38. }
    39. }
    40.  
    41. void test::onStateChanged(Phonon::State newState,Phonon::State oldState)
    42. {
    43. // here we always get Phonon::ErrorState
    44. qDebug() << "New state: " << newState;
    45. }
    46.  
    47. void test::networkManagerFinished(QNetworkReply *reply)
    48. {
    49. // this works, it saves the file in full, and I can play it
    50. QByteArray byteArray = reply->readAll();
    51. byteArrayToFile(&byteArray, "d:/hello.mp3");
    52. }
    53.  
    54. void test::authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
    55. {
    56. // this is not used on localhost, but it does authenticate properly when using with the site on line
    57. qDebug() << "Authenticating";
    58. authenticator->setUser("username");
    59. authenticator->setPassword("password");
    60. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and phonon - can it be done?

    What platform and phonon backend do you use? It seems you're trying this on Windows, I've been using the xine backend on Linux, maybe they work differently.

    Comparing my code to yours - try increasing the read buffer of the network reply (set it to 0). Also try dumping Phonon::MediaObject::errorString() to debug and see what the error is.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    serenti (16th June 2009)

  12. #11
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and phonon - can it be done?

    The error I'm getting from MediaObject::errorString() is:

    Error: "No combination of filters could be found to render the stream. (0x80040218)"

    I increased the read buffer of the network reply to 0, but it didn't help.

    As far as the backend, I only have DirectSound available. I tried all 4 possible output devices that exist on my computer (Windows XP), none of them work:

    Output device: Default DirectSound Device
    Output device: DirectSound: Realtek HD Audio output
    Output device: Realtek HD Audio output
    Output device: Default WaveOut Device

    I set them properly and then made sure they are used this way:

    Qt Code:
    1. void test::onStateChanged(Phonon::State newState,Phonon::State oldState)
    2. {
    3. // here we always get Phonon::ErrorState
    4. qDebug() << "New state: " << newState;
    5. qDebug() << "Error: " << pl->mediaObject()->errorString();
    6. qDebug() << "Output device: " << pl->audioOutput()->outputDevice().name();
    7. }
    To copy to clipboard, switch view to plain text mode 

    So I guess the unfortunate conclusion of our tests is that this particular functionality depends on the backend used. Which, in turn, means I can't use phonon for what I need. I'd be happy to use the default streaming capabilities of MediaObject if only there was a way to set the user name and password, so I don't get the default login dialog every time it connects. Well, maybe in the future versions they'll make this possible.

    Thank you for all your help. It's great to know you are there to help whenever we need it. I guess I'll just stick to using a Flash Player for now. I'd prefer using phonon, since it's more "native", but using Flash is not a bad solution, and it works well. (At least on Windows . I still need to check it on other platforms.). Thanks again.

Similar Threads

  1. I cannot run the phonon demos in the PXA270!Can someone help me?
    By Justin_W in forum Qt for Embedded and Mobile
    Replies: 8
    Last Post: 19th February 2010, 10:14
  2. Replies: 2
    Last Post: 24th April 2009, 12:13
  3. Problem with compiling qt on windows xp with phonon
    By junky in forum Installation and Deployment
    Replies: 3
    Last Post: 13th April 2009, 08:13
  4. Phonon in standalone app via py2app - backend plugin error
    By tory108 in forum Installation and Deployment
    Replies: 6
    Last Post: 20th February 2009, 18:36
  5. Phonon issues with DirectSound9
    By Zoltán in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2009, 02:48

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.