Results 1 to 3 of 3

Thread: How do I wait for QMediaPlayer::setMedia(.. to finish?

  1. #1
    Join Date
    Aug 2020
    Posts
    19
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default How do I wait for QMediaPlayer::setMedia(.. to finish?

    Title summarizes my question. I'm using Qt 5.15 in MinGW, gcc 10.2, C++17.

    I have a UI with a QVideoWidget with video output to a QMediaPlayer. The UI layout/size automatically rescales to the loaded media, i.e. it depends on the size of that media.
    Because of this, I need to finish loading the media set with QMediaPlayer::setMedia(.. to avoid unneccessary layout/resize calls.
    However QMediaPlayer::setMedia(.. returns immediately and I can only check if loading is finished by inspecting QMediaPlayer::MediaStatus with the appropriate slot.

    Currently, on my load call, I put all size-dependent arguments in a buffer struct and then call the actual layout/resize function either if loading is already finished, or whenever
    Qt Code:
    1. QMediaPlayer::MediaStatus == QMediaPlayer::LoadedMedia
    To copy to clipboard, switch view to plain text mode 
    But this a bit messy and still means I have to return from my loading function possibly before loading is finished, thus doing all layout/resizing twice..

    So: is there any way to specifically wait for loading of the media to finish after calling QMediaPlayer::setMedia(..? As far as I understand, everything is handled by events, not threads, so doing something like
    Qt Code:
    1. while(!loaded){}
    To copy to clipboard, switch view to plain text mode 
    would simply lock forever. Do I need to somehow request for a specific event to be processed immediately? Or do I call QMediaPlayer::setMedia(.. itself on a new thread and then lock the main thread with a mutex, would this be defined behavior to do in Qt? Or something else entirely?

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I wait for QMediaPlayer::setMedia(.. to finish?

    And why don't / can't you connect to QMediaPlayer::mediaStatusChanged() signal and do the stuff you want to in there?

  3. #3
    Join Date
    Aug 2020
    Posts
    19
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do I wait for QMediaPlayer::setMedia(.. to finish?

    That is what I am doing right now, but my problem is that this is not guaranteed to be called when I want it to. My problem is that I do not want to call QMediaPlayer::setMedia(.., then return from the function making that call, and then only sometime later have QMediaPlayer::mediaStatusChanged(.. called with QMediaPlayer::MediaStatus == QMediaPlayer::LoadedMedia.

    Currently, this first makes the UI initialize in a weird unusable (but responsive) state exactly because setMedia immediately returns. I do not want this, and I cannot put the UI in a useable state without the media having finished loading, because I need some data from it to do so. Only after some waiting, once QMediaPlayer::mediaStatusChanged(.. is called after loading has finished, I update the UI a second time, in a proper state.

    Instead, it would be much easier if I can guarantee that when I return from the function that calls QMediaPlayer::setMedia(.., the media has definitely finished loading. But I cannot sleep, because that will block the main thread, so the call to QMediaPlayer::mediaStatusChanged(.. will never happen to begin with.. unless I'm mistaken somewhere?

    I have some "solutions" in mind, but they feel somewhat convoluted and hard to understand in code to me:
    • 1. Emit another signal from inside QMediaPlayer::mediaStatusChanged(.. to call a slot in the caller that finishes the remaining UI layout.
      My problem with this is that the UI will still be responsive in a bad state until loading is finished. I could hide the window, or copy it to a temporary, or something along those lines, to get around this - but that is quite complicated for such a simple task, not to mention the additional needed signal / slot.
    • 2. Use a different library to load the media, extract the needed data, use that to do the layout. QMediaPlayer can then load its own seperate media and I don't need to wait for it. This seems a little more sane, but in my use case the loading is generally fast enough that having to double the memory usage for a minor speedup is questionable. Though it would only be for a short time, I guess.


    Maybe one of these is fine, but simply waiting for the loading to finish would surely be the simplest and more maintainable way, I just don't know if this is possible..

Similar Threads

  1. How to wait for process to finish
    By roseicollis in forum Newbie
    Replies: 4
    Last Post: 22nd December 2014, 16:17
  2. createRequest does not finish
    By brcontainer in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2013, 22:07
  3. Wait Condition Indication to User Using Loading/Wait Message
    By vivek.panchal in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2012, 17:40
  4. Wait for a function to finish
    By fitzy in forum General Programming
    Replies: 3
    Last Post: 26th October 2009, 19:20
  5. Finish of PaintEvent
    By jimfan in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 08:50

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.