How to check if Phonon sound is already playing? - c++

I'm relatively new to Qt so I'll explain this question and what I hope to achieve as clear as possible.
Currently I have buttons that, when clicked, play a sound file located in the applications directory.
I did a little research and found many users say using Phonon is better than QSound. This application is just for the Windows platform so nothing special is required.
Here's the code I have in mainwindow.cpp:
void MainWindow::on_obj_button_gandalf_clicked()
{
Phonon::MediaObject *music =
Phonon::createPlayer(Phonon::MusicCategory,Phonon::MediaSource("sound_file.mp3"));
music->play();
}
The sound plays perfectly. However, if the user had to click the button a second time whilst the sound is still playing from the first click it plays one over the other.
Is there some sort of isplaying() function or something to determine if the same sound is indeed playing already? If so it shouldn't play it again, if not it should then play the song as requested.
I'm using Qt 4.7.0 32bit

Reading the documentation, it appears it's as simple as calling music->state() and ... checking the state.
if (music->state() == Phonon::PlayingState) {
...
You'll also note there's a number of Signals that would let you manage this in your application, specifically the stateChanged signal.

Related

Qt how to properly do a screensaver function windows C++

I have to do an touchscreen app for windows, and if the customer hasnt click for 60sec on the screen, a little video or advertsing appears (and leave right after the customer's clik).
I've already made my own thread function for that (a simple loop with a usleep), but i dont think its a good way.
I'm seeking for an obj or something else with Qt that could be better, any ideas?
I'm not sure I fully understand your question but it sounds like you want to listen to QTMouseEvents. Specifically it sounds like you'll be interested in events of type QEvent::MouseButtonPress. Start a timer on a click, once it reaches 60 seconds (note: reset the timer on each click) then start playing the video. Another event of that type will then allow you to 'restore' the main window app (if the video is playing)

How to detect a Windows event in Qt?

I am trying to build an app to detect Windows events, in particular events related to multimedia (playing video, playing audio and images).
For instance, if Windows Media Player is opened, the related event should be detected.
There is no 'events' for that.
You can detect the lauches of media players (by winapi ::FindWindow) or image viewers.
I don't think it's possible to do this with QT's built in functions alone. You'll have to use the Windows API. Depending on what you actually want to do this can get quite complicated.
If you just want to check if a certain application has been started yet, you could use the FindWindow function. I'd suggest to use a qt timer to create signals that you can use to check if the Window has opened yet.
QTimer::singleShot(200, this, SLOT(checkForMediaPlayer()));
Just add this to your QObject along with the checkForMediaPlayer member function that'll do whatever you want once the MediaPlayer has been detected.

Qt4: Making fullscreen window impossible to get around (a lock screen)?

My application is an OS lock screen (like GDM's lock screen or KDE's), so I'm trying to make it function like one.
I am trying to make my application's window hover above all other windows and disable/intercept all keyboard shortcuts (ALT-TAB, CTRL-ALT-D, etc.) that would cause it disappear.
Is there any way to do this? I'm 100% sure there is, as lock screens with GUIs exist, but I just can't find the place to look...
I don't know how to do it with Qt, but what you are looking for is called grabbing. You can grab the pointer input device as well as the keyboard.
Edit: Looking in to the Qt4 docs, have you tried to use QWidget::grabMouse? It looks like this function does exactly what you want.
I don't know if this is the best solution, but you can try an event handler using QObject::installEventFilter().
If you are using Windows, you can install an event filter that handles messages where event->type() == QEvent::WinEventAct.
I don't really know much about other OSs, but Qt probably has something for that too.
inherit Qwidget class with parameter Qt::WindowStaysOnTopHint see below
myclass::myclass(QWidget *parent) : QWidget(parent,Qt::WindowStaysOnTopHint)

Making a GUI using Qt for a text-based game

Me and my fellow classmates decided to make a game over the summer. At first we decided to make it a text-based console app, but then I though that it would be possible to create a GUI for the game using Qt. I played around with it a bit, being able to create widgets and what-not, but was unable to do one thing. For an example, our game contains a Player class. I created a dummy player in the main.cpp and afterwards tried to make the players info to display on a label on a press of a button. After numerous attempts, the only way I was able to do it is by making the player in the mainwindow.cpp . Is there a way to display the info of a player made in the main.cpp? How would it be possible to make the data accessible by different windows, such as the battle window, inventory window, etc.
It's a good thing to let the main.cpp as lean as possible, it should not contain much code in your case.
If I understand correctly you have several windows (could be simple QWidgets with no parent or QMainWindows) running on the same QApplication and your want to share data between them ? Why not just share a Player pointer in both your windows ?
Your main.cpp will look like this:
QApplication app(argc,argv);
YourWindows1 mw1;
YourWindows2 mw2;
Player player;
mw1.setPlayer(&player);
mv2.setPlayer(&player);
mw1.show();
mw2.show();
app.exec();
A more fancy way, could be to use a singleton. This singleton could own and hold your Player instance, every windows can access this singleton statically anytime to fetch Player information. It could be something useful for you to learn.
NOTE: I don't see exactly what is the problem here, perhaps you could share more details about what's causing you trouble ...
In a typical Qt app, main.cpp is only there to start the single application UI object and then turn control over to Qt's event handler. It's possible to deviate from that model, but unless you're quite experienced with Qt it's not something that would be recommended.
I agree with the other posters in this thread in that your main function should be kept absolutely as lean as possible. You want it simply to spawn a new instance of your game, and the let the game loop or state control manager take care of the rest. It basically boils down to something like this:
class Game{
Game();
~Game();
int OnExecute();
};
int Game::OnExecute(){
while(running){
// do game stuff
}
return 0; // No errors, game closed correctly.
}
int main(int argc, char* argv[]){
Game myGame;
myGame.OnExecute();
}
All of your initialization methods/code is contained within the OnExecute function, prior to the game entering its main loop. Within that main while(running) loop, you put your calls to your per-frame Update and Rendering functions which control logic calculations like entity position, physics updating, AI updating, etc and then you render everything for that frame. This is obviously an ultra-simplistic strategy, but it definitely helped me grasp exactly how it is that game engines work.
Now, as far as Qt is concerned, I've tried exactly what you are trying and it is not easy. Qt, though it can be rigged to work for in-game interfaces and the like, seems to me to be primarily intended for use in applications more than games. It provides excellent functionality if you need simple forms and buttons in your program, but as far as custom-designed HUDs and such, you're going to want to look somewhere else in order to save yourself a great deal of hassle. There are, however, a huge number of extensible GUI libraries specifically meant for games. I would suggest looking for one based simply on OpenGL since most graphics/game engines are built upon it and would integrate quite nicely.

Passing the "enter key" event to flash player in an ATL Window?

I have a Flash player (flash9.ocx) embedded in an ATL window and have coded functionality into the swf to respond to the return/enter key being pressed.
Works fine from the standalone swf player but as soon as its played from within my embedded player it doesn't execute. It's as if my window is getting in the way somehow?
Is there any way to pass the keypress through to the player?
FYI, there isn't anything to weird in place on the form.
Thanks!
I'm not VC++ developer, but I use Flash a lot.
Though not sure, it seems that the embedded player doesn't have the focus. Make sure you've got this part covered on the Flash side of things:
the stage exists ( you movie is properly initialized)
you set the KeyboardEvent listener to the stage.
You could use the FocusManager to make sure you've got the focus.
I don't know if you can pass the focus from you app to the SWF OLE through some tabIndex or something.
If still this doesn't work you can try using the External Interface to add callbacks from your app to flash player ( basically call and actionscript function from your app ).
This was achieved through fscommand before, but External Interface seems to be the thing to use now.
Good luck!
I don't use flash a lot but i'm a C++ programmer. =)
Let's see if I can help you.
I believe that your application is catching all the events before your flash movie. I don't know if there is a better way to do this but you could listen for any keyboard event on your form and use the SetVariable of your ActiveX component to set a variable inside Flash. Then, in the Flash Movie you could set a watch for any changes on this variable and trigger your Enter event.
Hope this helps.