Play MP3 in AIR app for iOS and Android

两盒软妹~` 提交于 2020-01-06 08:15:26

问题


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


回答1:


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);
}


来源:https://stackoverflow.com/questions/14741169/play-mp3-in-air-app-for-ios-and-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!