Media file can not be switched and played - c++

I am trying to play a music file on S60 5th edition with the following code:
_LIT(KMusicFilename, "C:\\Data\\Music.mp3");
TApaTaskList iTaskList(CCoeEnv::Static()->WsSession());
TBool iExists;
TApaTask iApaTask = iTaskList.FindApp(TUid::Uid(0x102072C3));
iExists = iApaTask.Exists();
if(iExists)
{
// Music player already running
iApaTask.SwitchOpenFile(KMusicFilename);
iApaTask.BringToForeground();
}
else
{
// music player is not running and needs to be launched
RApaLsSession iAplsSession;
User::LeaveIfError(iAplsSession.Connect());
TThreadId thread;
iAplsSession.StartDocument( KMusicFilename,
thread,
RApaLsSession::ESwitchFiles );
iAplsSession.Close();
}
The problem is that this code sample does not work if the music player is already running. The media file that was already playing keeps playing, the function SwitchOpenFile does not have any effect on it.
Is there a workaround for this?
Thank you.

I'm not sure why it doesn't work, but one thing I notice about your code: this call:
iApaTask.SwitchOpenFile(KMusicFilename);
does not check the error code; see if you get an non-zero error code and this may help determine what the problem is. (The same applies to the iAplsSession.StartDocument(...) call).

Related

Qt debugging stops on deserialize

hi i'm working with dlib and I have this error about deserialize.
My exe file works fine, but it only stops when I try to debug it on my Qt creator.
It doesn't give me any error message, it just stops and does not works. I can't even press exit on my project, I can only make it stop by pressing 'stop debugging' button on Qt.
It does works when I run my exe file, so I have no idea what can be wrong. I tried to find out where the error occurs, and this is my code.
if (set)
{
//DNN for shape predictor
dlib::shape_predictor sp;
qDebug("on_pushButton_clicked"); // I get this
dlib::deserialize("shape_predictor_5_face_landmarks.dat") >> sp;
qDebug("destination"); // I don't get this message
//shape detector extracts face images from image
auto shape = sp(spimg, dets[0]);
dlib::matrix<dlib::rgb_pixel> face_chip;
dlib::extract_image_chip(spimg, dlib::get_face_chip_details(shape, 150, 0.25), face_chip);
//Add face to global variable
std_faces.push_back(std::move(face_chip));
//send signal to open the mainwindow
emit Photo::ClosedProperly();
keep_sending = true;
}
as I get the first qDebug message and it stops, so I suspect that in deserialize function my debugging stops for some reason. I'm keep searching about dlib and deserialize but I couldn't get satisfactory answer yet.. I'll keep looking on it, but I post this question that someone might experienced similar problem and has a clue to figure this out.
Thanks for reading this.
p.s. I think my debugging breakpoint doesn't work around that spot... this code is a part of on_pushButton_clicked() and it gives a qDebug message but breakpoint doesn't work. I'm not sure it's relative to the problem I have, but I add this information so it might help.

How to fully/correctly exit a Qt program from the main form?

I'm writing a Qt program (using Qt 5.4) that reads frames from a webcam based on a QTimer, not a separate thread (interval set to 20 ms, of course it takes much longer than 1/50 of a second to read a frame from the webcam and process it, I'd approximate the frame rate is perhaps 20 fps. Anyhow, the function which runs when the timer cycles is a slot and is as follows:
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::processFrameAndUpdateGUI() {
bool blnFrameReadSuccessfully = capWebcam.read(matOriginal); // get next frame from the webcam
if (!blnFrameReadSuccessfully || matOriginal.empty()) { // if we did not get a frame
QMessageBox::information(this, "", "unable to read from webcam \n\n exiting program\n");
QApplication::quit();
}
// process frame here . . .
The idea being if the webcam can be successfully read at the beginning of the program, but then cannot be (webcam stops working, user accidentally disconnects webcam, etc.) the program should show a message box to this effect and then close itself entirely.
With the above, if I unplug the webcam while the program is running for testing purposes, the message box appears as intended, but after choosing OK, a debug error screen appears. If I choose "Abort" the form is still there and will not respond. After attempting to close the form multiple times Windows asks "the program does not seem to be responding, would you like to close?" at which time I can close the form. Clearly this is not achieving the intended effect.
After various Googling I found the suggestion to modify as follows:
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::closeEvent(QCloseEvent *) {
QApplication::quit();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::processFrameAndUpdateGUI() {
bool blnFrameReadSuccessfully = capWebcam.read(matOriginal); // get next frame from the webcam
if (!blnFrameReadSuccessfully || matOriginal.empty()) { // if we did not get a frame
QMessageBox::information(this, "", "unable to read from webcam \n\n exiting program\n");
closeEvent(new QCloseEvent());
}
// process frame here . . .
When I first saw this code I was optimistic, however it gives me the same result as above (program hangs with the form still open). I'm using OpenCV 2.4.11 for my image processing and my program has 4 files:
frmmain.h (.h for the main form, which is a standard QMainWindow made with Qt Creator)
frmmain.cpp (.cpp for the main form, where the above code resides)
main.cpp (which I have not changed from how Qt Creator made it)
frmmain.ui (typical form with a small number of common widgets added via Qt Creator)
Yes, I realize that I could show an error message on one of the widgets that can show text, return from the function, and leave it to the user to close the program, but I'm looking for a more elegant solution. Can anybody offer further advice as to how to fully close a graphical Qt program? Please advise.
Two things that could possible solve your problem:
Before displaying the messagebox, stop the timer with the stop() method.
After the QApplication::quit(); exit the function with return; Your function might be running to the end one last time and accessing invalid objects.
For anybody else's reference Rafael Monteiro's answer was spot on. Here is the updated code (verified working):
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::closeEvent(QCloseEvent *) {
if(qtimer->isActive()) qtimer->stop(); // had to stop timer here !!!!!!!!
QApplication::quit();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::processFrameAndUpdateGUI() {
bool blnFrameReadSuccessfully = capWebcam.read(matOriginal); // get next frame from the webcam
if (!blnFrameReadSuccessfully || matOriginal.empty()) { // if we did not get a frame
QMessageBox::information(this, "", "unable to read from webcam \n\n exiting program\n");
closeEvent(new QCloseEvent());
return; // had to add return here !!!!!!!!!
}
// rest of function here . . .
I should mention I had to add both the return and stop the timer. Thanks Rafael!

Virtual machine and memory access issue

I'am trying to watch a directory and notify for file changing(creation), I'am using a virtual machine (VirtualBox) on windows 7 and Ubuntu 12.04 as the guest, when a file was added to my watched folder I test the existence of this file to get its size and manipulate it later, the problem is: sometimes it return 0 as size although the file exists and have a non zero size, here is my code:
void Watcher::OnFileChanged (char* FileBaseName)
{
QDir watchDir(RootToWatch);
QString fileN="";
fileN=QString::fromLocal8Bit(FileBaseName);
QFileInfo file(RootToWatch+"/"+fileN);
qDebug()<<"Watcher::OnFileChanged "<<fileN;
try
{
if(file.exists())
{
qDebug()<<"FileWatcher::OnFileChanged "<<fileN<<"exists";
qDebug()<<"OnFileChanged:fileName "<<file.fileName()
<<"\nOnFileChanged File().size"<<QFile(fileN).size()
<<"\nOnFileChanged QFileInfo Size:"<<file.size();
....
}
}
}
should I wait for laps of time to get the real size (if yes how match and how test if the file is ready to be read) or do I missed some thing.
Any help will be appreciated.
This does sound like a typical case of "watcher goes off before the file is finally written" - which would be perfectly expected behaviour really, as all you can say is "tell me if this file has changed" - getting size zero is definitely a change - whether it previously existed or is just coming into existance, it's a change from the previous state.
My approach in this case would be to set the watch again, and see if the file has "grown" next time around.

Error: cannot find World!.map: No such file or directory. Using Atmel studio 6 and CEENBot API

So I am making my Hello world program, and it is like this:
#include "lib-includes/capi324v221.h"
void CBOT_main(void)
{
LCD_open();
if (!LCD_OPEN) {
while (true); // loop forever
}
else {
LCD_printf("Hello, World!");
while(true); // loop forever
}
}
Can anyone help with this? I am sorry if it is a little vague; it is all the info I know for the most part.
First of all what are those while loops doing there? My first reaction is to get rid of those.
I have no personal experience with programming CEENBots\your IDE, but based on the error description in the question title I suspect your source file has a ! in it which messes up the build process (it complains about a World!.map file, best to keep special characters out of filenames).
edit:
To extend upon what Öö Tiib states below, get rid of the space and comma too :)

QAudioOutput always encountering UnderrunError

I'm using Qt 4.8 with Qt Creator 2.4.1 on Windows 7 Ultimate x64.
I'm taking audio input using QAudioInput class and playing it using QAudioOutput. There is a 2 seconds timeout after which I stop taking input and then setup the output as follows:
class MainWindow
{
// ...
QByteArray output_data;
QBuffer output_data_buffer;
QAudioOutput *audio_out;
// ...
};
MainWindow::MainWindow(QWidget *parent)
{
// ...
output_data_buffer.setBuffer(&output_data);
// ...
}
void MainWindow::audioInputStopped(QByteArray data)
{
output_data = data;
output_data_buffer.open(QIODevice::ReadOnly);
audio_out = new QAudioOutput(audio_format, this);
connect(audio_out, SIGNAL(stateChanged(QAudio::State)),
SLOT(audioOutputStateChanged(QAudio::State)));
audio_out->start(&output_data_buffer);
}
The audio format I'm using is supported by both input and output devices. I checked them using QAudioDeviceInfo::isFormatSupported(). The 2 seconds audio (data in audioInputStopped()) always plays fine.
In the slot audioOutputStateChanged, I'm always encountering QAudio::UnderrunError error from audio_out->error() after the buffer is finished playing. After audio_out->start() is called, the state (passed as parameter in audioOutputStateChanged()) and error goes as follows:
No error. Active state.
No error. Stopped state.
Underrun error. Idle state.
Note that I'm stopping audio_out in idle state following this example. Why the code is encountering underrun error? Is this normal?
This may seem kind of odd, but I've seen where the built-in arrays in Qt handle better when constructed on the heap, or at least when their elements are constructed on the heap (so they are just an array of pointers). The memory management is a little bit trickier, but items pushed into them don't go out of scope. The Qt Object Model also promotes putting most things on the heap and parenting them correctly. This might help.
After reading a little bit up on buffer underruns, it sounds like there is something still trying to read from the audio source while something else is writing to it or vice-versa. Check out some of the links below. You could try disconnecting the audio_in part from the buffer before reading the buffer. This is more likely to fix the error.
I would also construct your QAudioOutput pointer in the constructor for your main window (more as a style thing). Following some of how it is organized in the examples in Qt, it seems like a better organization. Here is the cpp for the QAudioInput example.
If you had a more complete example, I could try more with it to recreate the error and debug it.
Here is someone else to commiserate with:
http://qt-project.org/forums/viewthread/16729
And a wiki article:
http://en.wikipedia.org/wiki/Buffer_underrun
And the list of Multimedia examples on Qt:
http://doc.qt.nokia.com/4.7-snapshot/examples-multimedia.html
Hope that helps.