Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

c++ - Qt: How to play sound witout blocking main thread?

I'm wondering is there a simple way in Qt to play sound without blocking the main thread ?

I know that normaly I should start a seperate thread to do this. But maybe there is a shortcut ? :)

Thanks for help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The standard QSound and the Phonon library both have significant problems when it comes to playing sound. They both require the main message loop which means either a slight block in the main thread or a delay in playing the sound. Ultimately they both use a background thread, but the lag is easy to notice. Not to mention that Phonon has questionable support on many platforms.

I've worked with both for a while and tried to a build a simple sound system for playing user interface sounds (just to beep and pop occasionally). It just isn't possible using the standard Qt Library.

I'd recommend using OpenAL, which I eventually did. It's a relatively straight-forward library and seems to have good cross-platform support. The other option is SDL_mixer, but OpenAL seemed easier to get installed on Windows (probably not by much though). So basically if QSound::play doesn't do what you want then the standard Qt API won't work for you.


Another important note, don't bother trying to fix the Qt sound api using a separate thread. It won't work. Phonon at least requires to be instantiated and called in the main thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...