Tutorial: Play a .wav file using QSA

The Play a .wav sample lets you open, read, and play a .wav file using the libasound library. When you run the app, a sample .wav file is played automatically.

You will learn to:

  • Open a sound file in your app
  • Determine the properties of a sound file
  • Prepare a sound file for playback
  • Play a sound file
Device image showing the finished PlayWav sample app.

Before you begin

You should have the following things ready:

  • The BlackBerry 10 Native SDK
  • A BlackBerry 10 device or the BlackBerry 10 Device Simulator

Downloading the full source code

This tutorial builds the Play a .wav sample app from scratch using a step-by-step approach. If you want to look at the complete source code for the sample app, you can download the complete project and import it into the Momentics IDE for BlackBerry. To learn how, see Importing and exporting projects.

Set up your project

Before we start creating our app, create an empty Core Native project in the Momentics IDE.

  1. In the Momentics IDE, on the File menu, click New > BlackBerry Project.
  2. In the Project Type section, select Core Native. Click Next.
  3. In the Templates section, select the Empty template. Click Next.
  4. In the Basic Settings section, enter the name for your project (for example, MyProject). Select Makefile under the Build option. Click Next.
  5. In the API Level section, select the API level that you want to use and click Finish.

Include libraries for the PlayWav sample app

Create a main.c file in your project. On the File menu, click New > File from Template and select the default text file template.

In your main.c file, you must include the following header files:

#include <errno.h>
#include <fcntl.h>
#include <gulliver.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/slogcodes.h>
#include <ctype.h>
#include <limits.h>
#include <screen/screen.h>
#include <sys/asoundlib.h>

#include <bps/bps.h>
#include <bps/audiomixer.h>
#include <bps/audiodevice.h>
#include <bps/dialog.h>
#include <bps/navigator.h>

#include "dialogutil.h"

Configure your linker settings for the libraries

You must configure the linker to include the nonstandard libraries that your project requires. If you want to add any additional libraries, you can use the Add Library Wizard.

For a makefile build:

  1. In the Project Explorer, double-click the common.mk file.
  2. Add the following libraries: LIBS+=bps screen asound
  3. On the File menu, click Save.

Specify a .wav file as an asset

You must add a .wav file to the project assets in the bar-descriptor.xml file.

  1. In the Project Explorer view, double-click your project's bar-descriptor.xml file.
  2. On the Assets tab, click Add Files.
  3. Locate a .wav file (for example: sample.wav file) using either File System or Workspace.
  4. Click OK and then click Save.

Location of the audio file

The audio file is saved in the /app/native folder relative to where you installed the app. In the Play a .wav sample app, the name of the audio file is sample.wav.

Last modified: 2015-07-24

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus