Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [61]

By Root 2518 0
throw a runtime error:

"android.permission.WRITE_EXTERNAL_STORAGE"/>

NOTE

When the native camera application launches, it looks for the SD card. If your device is connected to your development computer with USB storage turned on, the card is not accessible. Make sure you turn off USB storage to test your application during development.

The BLOB type


At the time of this writing, there is no native library in which to save an MP3 file that can be played back into the application at runtime. As an alternative, you can save the bytes in an SQLite database as BLOB data. The BLOB type is raw binary data that stores information exactly as it was input. To review the use of SQLite, please read Chapter 6.

In this section, we will compress the file to reduce its size. First, let’s create a database and a table to store the audio files:

import flash.data.SQLConnection;

import flash.events.SQLEvent;

import flash.data.SQLStatement;

import flash.errors.SQLError;

import flash.filesystem.File;

var connection:SQLConnection;

// open connection to database

connection = new SQLConnection();

connection.addEventListener(SQLEvent.OPEN, openDatabase);

var file:File = File.documentsDirectory.resolvePath("Dictaphone.db");

connection.open(file);

function openDatabase(event:SQLEvent) {

connection.removeEventListener(SQLEvent.OPEN, openDatabase);

createTable();

}

// create or open table

function createTable():void {

var statement:SQLStatement = new SQLStatement();

statement.sqlConnection = connection;

var request:String =

"CREATE TABLE IF NOT EXISTS mySounds (" +

"id INTEGER PRIMARY KEY AUTOINCREMENT, " +

"audio BLOB )";

statement.text = request;

try {

statement.execute();

} catch(error:SQLError) {

trace(error.message, error.details);

}

}

Now we’ll compress the audio and save it in the database. Here we are using ZLIB compression, which provides good results but is somewhat slow to execute:

import flash.utils.CompressionAlgorithm;

var statement:SQLStatement;

function saveItem():void {

// compress the bytes

bytes.position = 0;

bytes.compress(CompressionAlgorithm.ZLIB);

var command:String =

"INSERT INTO mySounds(audio) VALUES (?)";

statement = new SQLStatement();

statement.sqlConnection = connection;

statement.text = command;

statement.parameters[0] = bytes;

try {

statement.execute();

} catch(error:SQLError) {

trace(error.message, error.details);

}

}

Retrieve the first audio item from the database, and decompress it to use it:

import flash.data.SQLResult;

function getItem(id:Number):ByteArray {

var command:String = "SELECT * FROM mySounds WHERE id=:id;"

var statement:SQLStatement = new SQLStatement();

statement.sqlConnection = connection;

statement.text = command;

statement.parameters[":id"] = id;

statement.execute(1);

var result:SQLResult = statement.getResult();

if (result.data != null) {

return result.data[0];

}

return new ByteArray();

}

// to read the data back, decompress it

bytes = getItem(1).audio;

bytes.uncompress(CompressionAlgorithm.ZLIB);

bytes.position = 0;

// play audio

Use the bytes to play the audio in a Sound object, as in the previous example.

WAV files


You can save your recording as a WAV file on your device. Download the Adobe.audio.format.WAVWriter class from the audio_sampler.zip file located at http://www.adobe.com/devnet/air/flex/articles/using_mic_api.html, and import it to your project.

In this example, we are encoding our previous recording as a WAV file and saving it on the SD card in a directory called mySounds. For a review on saving local persistent data, read Chapter 6:

import com.adobe.audio.format.WAVWriter;

import flash.filesystem.File;

import flash.filesystem.FileStream;

import flash.filesystem.FileMode;

function saveWav(bytes:ByteArray):void {

// point to mySounds directory on the SD card.

var directory:File = File.documentsDirectory.resolvePath("mySounds");

// if directory does not exist yet, create it

if (!directory.exists) {

directory.createDirectory();

}

//

Return Main Page Previous Page Next Page

®Online Book Reader