Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [66]

By Root 2510 0


// define the stream client property

var metaObject:Object = new Object();

metaObject.onMetaData = onGetMetaData;

stream.client = metaObject;

stream.play("someAudio.m4a");

function onGetMetaData(data:Object):void {

// audio duration

trace(data.duration*1000, "milliseconds");

// codec

trace(data.audiocodecid);

for (var prop:String in data) {

trace(prop, data[prop]);

}

}

Audio Example Using Multitouch


You can develop a musical instrument or device to trigger audio and music. Android devices only support two simultaneous touches; however, they support a range of gestures. Review Chapter 7 for more information.

You can, for instance, use a left-to-right swipe gesture to turn the volume up and a right-to-left swipe gesture to turn the volume down.

ID3 Tags

ID3 is an audio file tagging format used in software and hardware around the world. The tag is a metadata container stored within MP3 audio files in a predictable format. The ID3Info class stores its properties.

To check that your file has an ID3 region, set a listener for the Event.ID3 event:

var sound:Sound = new Sound();

sound.addEventListener(Event.ID3, onMetaData);

function onMetaData(event:Event):void {

var metaData:ID3Info = Sound(event.target).id3;

// var metaData:ID3Info = event.target.id3;

for (var property in metaData) {

trace(property, metatData[property]);

}

}

Some of the information stored in the ID3 region is the name of the artist, the name of the album, the song title, and the year it was recorded. This is particularly useful if you want to build an audio catalog.

You can read more about ID3 at http://www.id3.org/.

Modifying Sound

The SoundTransform class is used to control volume and panning for the SoundChannel and the SoundMixer.

Controlling Volume


The SoundTransform object can be applied to the SoundChannel in two ways.

In this example, it is passed as a parameter when the channel is first created:

var sound:Sound = new Sound();

var volume:Number = 0.75;

var soundTransform:SoundTransform = new SoundTransform(volume);

var soundChannel:SoundChannel = sound.play(0, 0, soundTransform);

If you change the volume property, the SoundTransform object needs to be reapplied:

transform.volume = 0.50;

soundChannel.soundTransform = transform;

If you want to change the volume over time, use a Timer or an Enter Frame event. In this example, we bring the volume from silence to full volume, and then back to silence, and so forth. The volume gets reset on the upper and lower bounds because, due to float errors, it may not end up exactly on zero or one:

import flash.media.SoundTransform;

import flash.net.URLRequest;

import flash.media.SoundChannel;

import flash.events.Event;

var volume:Number = 0;

var direction:int = 1;

var transform:SoundTransform;

transform = new SoundTransform();

var sound:Sound = new Sound();

sound.load(new URLRequest("mySound.mp3"));

var soundChannel:SoundChannel = sound.play();

stage.addEventListener(Event.ENTER_FRAME, changeVolume);

function changeVolume(event:Event):void {

volume += 0.01*direction;

// when reaching upper volume bounds, change direction

if (volume > 1) {

volume = 1.0;

direction *= -1;

// when reaching lower volume bounds, change direction

} else if (volume < 0) {

volume = 0;

direction *= -1;

}

transform.volume = volume;

soundChannel.soundTransform = transform;

}

To change volume globally, use the SoundMixer. In the following example, we mute all the channels at once. Make sure not to confuse the syntax of the SoundTransform class and the soundTransform property which has a lowercase s:

var globalTransform:SoundTransform = new SoundTransform();

globalTransform.volume = 0;

SoundMixer.soundTransform = globalTransform;

Panning


Panning is the position of the audio signal between the right and left channels in a stereo sound field. Pan has a value from ‒1 all the way to the left channel to 1 all the way to the right channel.

The external speaker on Android devices is mono. Make sure to inform your users to use their headphones,

Return Main Page Previous Page Next Page

®Online Book Reader