How to add Music in AS3 Android App - android

I am a noob in Adobe Flash Action Script 3..
I want a music player (with start, pause buttons) code in AS3, I have imported the music in the library and I have added the following code
var qMySound:Sound = new mySound1();
qMySound.play(0, 9999);
mySound1 is the AS Linkage
and it works.
I made a stop button using the code snippet's code
button_7.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds_6);
function fl_ClickToStopAllSounds_6(event:MouseEvent):void
{
SoundMixer.stopAll();
}
And it works too, but now i want to start it again, I tried to use this code:
button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_28);
function fl_ClickToGoToAndStopAtFrame_28(event:MouseEvent):void
{
gotoAndStop(1);
}
The music AS3 code is in Frame 1 btw.
And that doesn't work.. so any ideas?
edit:
This is my code:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform:SoundTransform = new SoundTransform();
var lastPosition:Number = 0;
var mySound:Sound = new mySound1(0,999);
myChannel = mySound.play();
// here is to learn how to deal with volume as well (between 0 and 1)
myTransform.volume = .5;
// setting the SoundTransform instance to the myChannel object
myChannel.soundTransform = myTransform;
pause_btn.addEventListener(MouseEvent.CLICK, onClickPauseHandler, false, 0, true);
play_btn.addEventListener(MouseEvent.CLICK, onClickPlayHandler, false, 0, true);
function onClickPauseHandler(event:MouseEvent):void
{
// getting the current position of the sound
lastPosition = myChannel.position;
// stopping the current sound
myChannel.stop();
}
function onClickPlayHandler(event:MouseEvent):void
{
// playing from the saved position
myChannel = mySound.play(0,999);
}
And this error pops out.
Line 8 | 1151: A conflict exists with definition of mySound in namespace internal.

You need to use the classes SoundChannel/SoundTransform to have more functionalities. When you stop all sounds using SoundMixer.stopAll(), you are not stopping that respective sound.
Follow some example code, that you guide you to achieve what you are expecting, plus, give you some extra knowledge about how to deal with sound.
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform:SoundTransform = new SoundTransform();
var lastPosition:Number = 0;
mySound.load(new URLRequest('yourMp3FileName.mp3'));
myChannel = mySound.play();
// here is to learn how to deal with volume as well (between 0 and 1)
myTransform.volume = .5;
// setting the SoundTransform instance to the myChannel object
myChannel.soundTransform = myTransform;
pause_btn.addEventListener(MouseEvent.CLICK, onClickPauseHandler, false, 0, true);
play_btn.addEventListener(MouseEvent.CLICK, onClickPlayHandler, false, 0, true);
function onClickPauseHandler(event:MouseEvent):void
{
// getting the current position of the sound
lastPosition = myChannel.position;
// stopping the current sound
myChannel.stop();
}
function onClickPlayHandler(event:MouseEvent):void
{
// playing from the saved position
myChannel = mySound.play(lastPosition);
}

Related

How to play a sound when a user clicks the right answer

I'm creating a quiz in flash using AS3. I'm having trouble trying to include sound when a user clicks a right or wrong answer.
How can I correctly play sound?
This is the code below
import flash.media.Sound;
//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)
{
if (myTextField11.text == answer1){
trace("Correct answer!!");
gotoAndPlay(2,"quiz1");
count = count + 10;
scoreBox.text = (count).toString();
setTimeout(gotoAndPlay, 1250, 1, "quiz2");
} else {
trace(myTextField11);
gotoAndPlay(3,"quiz1");
var mySound:Sound = new WrongAnswer();
WrongAnswer.play();
setTimeout(gotoAndPlay, 1250, 1,"quiz2");
}
}
I've now done this but no luck:
import flash.media.Sound;
//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)
{
if (myTextField11.text == answer1){
trace("Correct answer!!");
gotoAndPlay(2,"quiz1");
count = count + 10;
scoreBox.text = (count).toString();
setTimeout(gotoAndPlay, 1250, 1, "quiz2");
} else {
trace(myTextField11);
gotoAndPlay(3,"quiz1");
MediaPlayer player = MediaPlayer player=MediaPlayer.create(YourActivity.this,R.raw.WrongAnswer);
player.start();
setTimeout(gotoAndPlay, 1250, 1,"quiz2");
}
MediaPlayer player=MediaPlayer.create(YourActivity.this,R.raw.nameofile);
player.start();
This code helps to play the music.
Place the code inside the Onclick() method.
If you declared mySound as variable name for the WrongAnswer(); sound then use that exact same name when accessing by code.
import flash.media.Sound;
//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)
{
if (myTextField11.text == answer1)
{
trace("Correct answer!!");
gotoAndPlay(2,"quiz1");
count = count + 10;
scoreBox.text = (count).toString();
setTimeout(gotoAndPlay, 1250, 1, "quiz2");
}
else
{
trace(myTextField11);
gotoAndPlay(3,"quiz1");
var mySound:Sound = new WrongAnswer();
//WrongAnswer.play();
mySound.play();
setTimeout(gotoAndPlay, 1250, 1,"quiz2");
}
}
If there's still issues then let me know. The alternative I was going to give was to just embed sound by code but that may not be necessary here...

Re-loading Swf file from sdcard as3 Android

I am working on an android app where I wish to download swf files from an external server, save them to sdcard and load them later in the app. Downloading works fine and the swf is saved in the application directory. Here is my code that loads the swf from the sdcard :
var myloader:Loader = new Loader();
var myhomeButton:btnHome = new btnHome();
addChild(myloader);
var swfFilePath:File = File.applicationStorageDirectory.resolvePath("Android/data/myswffile.swf");
var inFileStream:FileStream = new FileStream();
inFileStream.open(swfFilePath, FileMode.READ);
var swfBytes:ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, mycompleteHandler);
myloader.loadBytes(swfBytes, loaderContext);
function mycompleteHandler(evt:Event):void
{
myloader.contentLoaderInfo.removeEventListener(Event.COMPLETE, mycompleteHandler);
addChild(myhomeButton);
myhomeButton.height = _height * 0.08;
myhomeButton.width = myhomeButton.height;
myhomeButton.x = 10;
myhomeButton.y = 10;
myhomeButton.addEventListener(MouseEvent.CLICK, myexitfftn);
}
function myexitftn(evt:Event):void
{
myloader.unloadAndStop(true);
removeChild(myhomeButton);
gotoAndStop(1, "SomeOtherFrame");
}
the problem is when I click the exit button the swf unloads but when I reload it, it starts from the second frame of the loaded swf, the third time from the third frame and so on... Where am I going wrong or what is an alternative solution, please guide.
Try, addChild(myloader) in Complete handler and removeChild and null while exiting, so that loaded swf is completely removed from the memory like so:
function mycompleteHandler(evt:Event):void
{
myloader.contentLoaderInfo.removeEventListener(Event.COMPLETE, mycompleteHandler);
addChild(myloader);
//... Rest of code remains same
}
function myexitftn(evt:Event):void
{
myloader.unloadAndStop();
removeChild(myloader);
myloader = null;
removeChild(myhomeButton);
gotoAndStop(1, "SomeOtherFrame");
}

actionscript 3 - Play sound once then delay

I am a complete newcomer to as3, having started about 3 months ago as part of a university project, so apologies if I am asking an obvious question or I provide the wrong information.
I am creating a game where a 'monster' from an array collides with a static movie clip which the user has to avoid by killing the monster before they get there.
When the monster reaches the movie clip, I want it to play a 'munch' sound once, then not play it again for a few seconds, but the code as it is means that the sound plays over and over again while the array item and movie clip collide, which does not sound right.
I tried to correct this by adding a timer which allows the sound to continue for half a second and then stops the sound channel, but this does not seem to have worked.
How can I make the sound play once, not play again for the next few seconds and then play again if the objects are still colliding?
Thanks in advance.
Heres my code:
var sc: SoundChannel;
var munch: Sound = new Sound(new URLRequest("audio/munch.mp3"));
var muteTest: Boolean = false;
var munchStop:Timer = new Timer (500, 1);
munchStop.addEventListener(TimerEvent.TIMER, afterMunchStop);
function afterMunchStop(event: TimerEvent): void {
sc.stop();
}
if (monster1Array[i].hitTestObject(centre_mc)) {
if (muteTest == false) {
sc = munch.play();
munchStop.start();
}
var sc: SoundChannel;
var munch: Sound = new Sound(new URLRequest("audio/munch.mp3"));
var soundIsPlaying: Boolean = false;
//sound should loop until objects no longer collide
var munchStop:Timer = new Timer(500);
munchStop.addEventListener(TimerEvent.TIMER, afterMunchStop);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
//play sound if monsters collide and sound is not already playing
if (monster1Array[i].hitTestObject(centre_mc) && !soundIsPlaying)
{
sc = munch.play();
soundIsPlaying = true;
//start the timer if it's not running
if(!munchStop.running)
munchStop.start();
} else {
//stop the sound if it's playing
if(sc)
sc.stop();
soundIsPlaying = false;
//reset the timer when the objects are no longer colliding
if(munchStop.running)
munchStop.reset();
}
}
function afterMunchStop(event: TimerEvent): void {
soundIsPlaying = false;
}

Record Sound in Android using ActionScript 3

i build a application that record sound in Desktop using ActionScript 3 , now i convert the application to Andriod Application but there is a problem that SampleDataEvent.SAMPLE_DATA event doesn't receive any data to record
Here is the code :
private var _microphone:Microphone;
private var _buffer:ByteArray = new ByteArray();
private var _difference:uint;
public function record():void
{
if ( _microphone == null )
_microphone = Microphone.getMicrophone();
_difference = getTimer();
_microphone.setSilenceLevel(_silenceLevel, _timeOut);
_microphone.gain = _gain;
_microphone.rate = _rate;
_buffer.length = 0;
_microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
_microphone.addEventListener(StatusEvent.STATUS, onStatus);
}
private function onSampleData(event:SampleDataEvent):void
{
_recordingEvent.time = getTimer() - _difference;
dispatchEvent( _recordingEvent );
var buteData:Number;
while(event.data.bytesAvailable > 0)
{
buteData = event.data.readFloat();
_buffer.writeFloat(buteData);
soundBytes.writeFloat( buteData);
}
}
anyone can help here
Thanx
I think, maybe you don't check out about AIR for Android settings. If you not checked in the RECORD_AUDIO. you should check it.
refer a below image.

[AS3][Air for Android] Get streaming mic input?

Is there a way to get true mic streaming input?
The example code I have at the moment looks to be getting the mic input data, and saving it to a sound object and playing it right away.
Is there a way to stream it properly?
If not, in my example, is there a way to get the mic input data, but mute the audio, as it is causing a feedback loop (despite setLoopBack being set to false..)
Code below:
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.media.SoundTransform;
import flash.utils.*;
var _soundBytes:ByteArray = new ByteArray();
var _micBytes:ByteArray;
var son:Sound;
var sc:SoundChannel;
var pow:int = 0;
var myBar:Sprite;
stage.quality = "LOW";
// this code ended up muting the mic input oddly?
//SoundMixer.soundTransform = new SoundTransform(0);
init();
function init()
{
myBar = new Sprite;
micInit();
soundInit();
addEventListener(Event.ENTER_FRAME, visualise);
}
function micInit()
{
var mic:Microphone = Microphone.getMicrophone();
if(mic != null) {
//mic.setUseEchoSuppression(true);
mic.setLoopBack(false);
mic.setSilenceLevel(0);
mic.rate = 44;
mic.gain = 60;
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
}
}
function micSampleDataHandler(event:SampleDataEvent):void
{
_micBytes = event.data;
sc = son.play();
}
function soundInit():void {
son = new Sound();
son.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
}
function soundSampleDataHandler(event:SampleDataEvent):void {
for (var i:int = 0; i < 8192 && _micBytes.bytesAvailable > 0; i++) {
var sample:Number = _micBytes.readFloat();
event.data.writeFloat(sample);
event.data.writeFloat(sample);
}
}
function drawLines(e:Event):void{
SoundMixer.computeSpectrum(_soundBytes, true);
myBar.graphics.clear();
myBar.graphics.lineStyle(2,0xabc241);
for (var i=0; i < 256; i++) {
pow = _soundBytes.readFloat()*200;
pow = Math.abs(pow);
myBar.graphics.drawRect(i*2, 0, 2, pow);
addChild(myBar);
}
}
Hope you can help!
To use acoustic echo cancellation, call Microphone.getEnhancedMicrophone() to get a reference to an enhanced Microphone object. Set the Microphone.enhancedOptions property to an instance of the MicrophoneEnhancedOptions class. Here is an article that discusses it all. Article about enhanced microphone options at Adobe
EDIT: I spoke too soon. I have used enhanced mic many times before, but I decided to read the article myself to see if there were any interesting things I could learn new from it... and I found this near the end
AEC is computationally expensive. Currently, only desktop platforms are supported for Flash Player and AIR
Although I just looked at the date... last year, so maybe give it a try and it is supported now?!?

Categories

Resources