BlackBerry 10/playbook OS 2.0 has a built-in OpenAL class library, so it's convenient to use the OpenAL class library to play sounds in your program.
The example program cowbell provided on the BlackBerry 10 developer site includes examples of how to code.
Basic Sound Handling
A cow, a bell, and Cascades is the only things so need to build a simple instrument app.
In this sample, a rotation animation are used to animate a bell hanging around the neck of a cow.
You'll learn how to:
Handle Rotation Animations Trigger C + + functions from QML use of a absolutelayout Play a basic sound with OpenAL
Good
A Rotat
Well, we download the example program cowbell code cowbell.zip
Importing projects in BlackBerry 10 development tools:
Select Menu File--Import ...
Select existing Projects into Workspace
Choose the Select Archive file, select the Browse button, locate the Cowbell.zip files on the file system, select the Finish button, and import the project cowbell.
Build Project
Run configurations ...
The result of the operation is shown in the following figure, note that on the BlackBerry 10 simulator is a horizontal screen display, according to the Cow (cow) on the body of the Bell (Bell), the bell will sway around, and make a clang (cowbell.wav) sound, according to other areas of cattle, the cow will emit mu (moo.wav) voice.
Now, let's take this code out and see how the QT program is used.
We created a new BlackBerry Cascades C + + Project.
Copy from the Cowbell project
Program Files: cowbellapp.cpp, Cowbellapp.h, Soundmanager.cpp, Soundmanager.h (where the red two files are slightly changed in the original code of Cowbell.zip)
Sound files: cowbell.wav, moo.wav
The final directory structure is as follows:
Because we are the QT program, so to modify the Bar-descriptor.xml file, join
<env var= "Qt_qpa_fontdir" value= "/usr/lib/qt4/lib/fonts"/>
Edit the Pro file under the project root directory, and add a libs line after the config line.
CONFIG + = qt warn_on debug_and_release cascades
LIBS + =-lopenal-lalut-lasound
Note: If you do not have this line, the project compiles an error and cannot find the OpenAL class library.
I suspect that this is a bug in the development tool, supposedly in the project, using the wizard to add the OpenAL class library. The following are the specific practices:
OK, now let's take a look at the code.
In the main program main.cpp, when you click the button PlayButton, the playclicked method that triggers the Cowbell object emits the bell sound.
#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include < cowbellapp.h>
int main (int argc, char *argv[])
{
qapplication app (argc, argv);
Qwidget window;
Window.resize (768);
Qpushbutton PlayButton ("Play Sound (WAV)", &window);
Playbutton.setgeometry (+, 280, +);
Cowbellapp cowbell;
Qobject::connect (&playbutton, SIGNAL (clicked ()), &cowbell, SLOT (playclicked ()));
Window.show ();
return app.exec ();
}
Let's look at the CowBellApp.h header file. It declares the playclicked () method to be invoked by the main program Main.cpp trigger event.
#ifndef cowbellapp_h
#define COWBELLAPP_H
#include "soundmanager.h"
class Cowbellapp:public Qobject
{
q_object public
:
Cowbellapp ();
~cowbellapp ();
Public slots:
void PlaySound (const QString &msg);
void playclicked ();
Private:
//The sound manager.
Soundmanager *msoundmanager;
};
#endif//Ifndef Cowbellapp_h
In the Cowbellapp.cpp program
1) playclicked () method plays the Cow bell sound file cowbell.wav.
2) Do object Lifecycle management on sound manager and delete sound manager when exiting.
#include "Cowbellapp.h"
Cowbellapp::cowbellapp ()
{
//sound Manager, we initialize the sound manager with a directory that resides in the
//assets directory and that directory is only contains playable files.
Msoundmanager = new Soundmanager ("sounds/");
}
Cowbellapp::~cowbellapp ()
{
//Destroy the sound manager.
Delete Msoundmanager;
}
void Cowbellapp::p laysound (const QString &msg)
{
msoundmanager->play (msg);
}
void Cowbellapp:: playclicked () {
msoundmanager->play ("Cowbell.wav");
Msoundmanager->play ("Moo.wav");
}
Finally, the core Sound manager program, we simply look at the soundmanager.h header file, CPP program will not look.
We see that the header file first introduced the OpenAL three header file, and then introduced the QT header file. This causes the openal to be unrelated to QT, but this example code for the sound manager can only be run in a qt/cascades program and not in a non-QT program.
#ifndef _soundmanager_h #define _soundmanager_h #include <AL/al.h> #include <AL/alc.h> #include <al/alut
.h> #include <QtCore/qstring.h> #include <qhash.h>//The number of max number of sound sources. #define SOUNDMANAGER_MAX_NBR_OF_SOURCES/** * Soundmanager * * A very basic Sound manager class for playing sounds
Using OpenAL. */class Soundmanager {public:/** * Constructor, initializes the sound manager. This function sets up OpenAL * and loads all sounds from the specified directory.
This directory should * is a folder only containing valid sounds.
* * @param sounddirectory directory where all sounds is kept (with to is located in the Assets/folder) */
Soundmanager (QString sounddirectory);
/** * destructor destroys all buffers and sources.
*/~soundmanager ();
/** * Called by the constructor, loads all sounds from the specified directory. * * @param sounddirectoRy directory where all sounds is kept (with to is located in the Assets/folder) */BOOL Init (QString Sounddirec
Tory);
/** * Plays a sound.
* * @param fileName The name of the file in the sounddirectory.
*/bool Play (QString fileName);
/** * Plays a sound, with modified pitch and gain. * * @param fileName The name of the file in the sounddirectory * @param pitch Specifies the pitch-be applied To a sound Range: [0.5-2.0] * @param gain sound gain (volume amplification) Range:]0.0-] */bool Play (QSt
Ring fileName, float pitch, float gain);
Private://sound buffers.
Qhash<qstring, aluint> msoundbuffers;
Sound sources.
Aluint Msoundsources[soundmanager_max_nbr_of_sources];
};
#endif//_soundmanager_h