如何在AS3 Android App中添加音乐

时间:2015-03-04 05:46:24

标签: android actionscript-3 flash

我是Adobe Flash Action Script 3中的菜鸟。

我想要一个AS3中的音乐播放器(带有开始,暂停按钮)代码,我已经在库中导入了音乐,并且我添加了以下代码

var qMySound:Sound = new mySound1();
qMySound.play(0, 9999);

mySound1是AS Linkage 它有效。

我使用代码段的代码

制作了一个停止按钮
button_7.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds_6);

function fl_ClickToStopAllSounds_6(event:MouseEvent):void
{
    SoundMixer.stopAll();
}

它也有效,但现在我想再次启动它,我尝试使用这段代码:

button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_28);

function fl_ClickToGoToAndStopAtFrame_28(event:MouseEvent):void
{
    gotoAndStop(1);
}

音乐AS3代码位于第1帧btw。

这不起作用..所以任何想法?


编辑: 这是我的代码:

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

此错误会弹出。

第8行| 1151:内部命名空间中mySound的定义存在冲突。

1 个答案:

答案 0 :(得分:3)

您需要使用类SoundChannel / SoundTransform来提供更多功能。当您使用SoundMixer.stopAll()停止所有声音时,您无法停止相应的声音。

按照一些示例代码,您可以指导您实现您的期望,另外,为您提供有关如何处理声音的额外知识。

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