Unsolved how to play/stop audio file(eg. mp3 file) using multi-threads?
-
how to play/stop audio file(eg. mp3 file) using multi-threads?
is QMediaPlayer thread-safe?
what i want is: open/play a mp3 in one Thread; while pause/stop/close the mp3 file in the other Thread. is there any good way to do this?
thank you! -
Why do you want to use it in a thread at all?
-
yep. i need to play it in a callback which is not mainThread.
and while playing the mp3, i want to pause/stop the mp3 in mainThread by press a button. -
@opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:
i need to play it in a callback
There is no reason for this to play an audio file in a separate thread since it's async...
-
Play & Stop event may trigger at the same time from different threads.....
-
so should i open a new thread for QMediaPlayer. and callback-thread & mainThread both send msg to the mediaPlayer thread?? and i just use the reference/pointer of the QMediaPlayer(means the QMeidaPlayer is thread-safe & can be used at different threads at the same time)?
-
@opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:
so should i open a new thread for QMediaPlayer
No. As @Christian-Ehrlicher already said playing media files is ASYNCH already. Playing media files does not block your main thread -> no need for any threads, don't overengeneer the task. To pause/stop simply call pause()/stop()...
-
@jsulm ok. so you mean i can just play/pause/stop in both mainThread and the other thread?
i am worried about : sometimes at the same time, stop() is trigered in mainThread & play() is triggered in the other thread... -
@opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:
is QMediaPlayer thread-safe?
No.
-
@opengpu Why do you have two threads? If you really need two threads then simply send signals for pause/stop from the other thread and pause/stop in the main thread (where your QMediaPlayer is)...
-
@jsulm i now have 2 threads, one is mainThread, and the other thread belong to some Callback func by some lib.
and play/stop can be both happen on these 2 thread... -
So why don't you simply do what @jsulm suggest and use signals/slots?
-
@opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:
QMediaPlayer
how to play .mp3 file in Windows. the filePath is C:\test\aaa.mp3
-
Already answered here.