puks1978
puks1978

Reputation: 3697

Play MP3 in AIR app for iOS and Android

I was wondering if someone could shed some light on the best method to embed, select and play audio files in an AIR app built for iOS and Android?

I have an app where a user can select a file to play from a list of 10 audio files. These are selected when a slider moves left or right.

I currently would create 10 individual embed snippets and classes

[Embed(source="assets/audio/file1.mp3)]
public static const file1:Class;

[Embed(source="assets/audio/file2.mp3)]
public static const file2:Class;
...

And then in the app initialise each class so I can reference them. Then simply call

file1.play();

Issue is this only plays the sound once where I would like the sound to look until the user selects another sound.

I guess a couple of questions: 1. Is having 10 embed/classes the best way to handle 10 different MP3 files? 2. How would I loop the MP3 seamlessly

Thanks

Upvotes: 0

Views: 1678

Answers (1)

bitmapdata.com
bitmapdata.com

Reputation: 9600

You stored Array or Vector mp3 files URL which user selected. and playing mp3 filed ended. load next URL from Array, Vector.

var sound:Sound = new Sound();
var soundChannel:SoundChannel;
var currentIndex:int = 0;

var mp3List:Vector.<String> = new Vector.<String>();
//only stored user selected mp3 url

sound.addEventListener(Event.COMPLETE, onMp3LoadComplete);

sound.load(mp3List[currentIndex]);

function onMp3LoadComplete(e:Event):void
{
    sound.removeEventListener(Event.COMPLETE, onMp3LoadComplete);
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}

function onSoundChannelSoundComplete(e:Event):void
{
    e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
    currentIndex++;
    if(currentIndex==mp3List.length) currentIndex = 0;
    sound.load(mp3List[currentIndex]);
    soundChannel = sound.play();
    sound.addEventListener(Event.COMPLETE, onMp3LoadComplete);
}

Upvotes: 1

Related Questions