简体   繁体   English

Flash播放/暂停声音切换按钮

[英]Flash Play/Pause sound toggle button

I've googled and googled but got no where or outdated tutorials. 我已经在Google和Google上搜索过,但是没有任何地方或过时的教程。 Anyone know how to make it so I can toggle audio with buttons using ActionScript 3 on Flash? 谁知道该怎么做,以便可以在Flash上​​使用ActionScript 3通过按钮切换音频?

To toggle play/pause you will need to record the position of where the user has paused the audio. 要切换播放/暂停,您需要记录用户暂停音频的位置。

To use a sound from the library like in your screenshot, you need to make that sound file available to your Actionscript. 要使用库中的声音(如屏幕截图中的声音),您需要使该声音文件可用于Actionscript。

First, Right click the sound file in your Library and click on Properties... . 首先,右键单击库中的声音文件,然后单击Properties... Inside the Properties window, check the box to Export for Actionscript . 在“属性”窗口中,选中“ Export for Actionscript ”框。 Change the Class name to something of your own, like MySong . 将类名称更改为您自己的名称,例如MySong

Now inside your code instead of pointing to an external Sound file, you will make mySound an instance of MySong . 现在你的代码,而不是指向一个外部声音文件里面,你会mySound的实例MySong

var isPlaying:Boolean;
var pausePosition:Number;
var myChannel:SoundChannel = new SoundChannel();
// edited mySound to use an internal sound file with Class of MySong
var mySound:Sound = new MySong();
var myButton:MovieClip;

myButton.addEventListener(MouseEvent.CLICK, playPauseClicked);

myChannel = mySound.play();
isPlaying = true;

function playPauseClicked(e:MouseEvent):void
{
    if (isPlaying) {
        pausePosition = myChannel.position;
        myChannel.stop();
        isPlaying = false;
        // change the display of your button to show the pause state
    } else {
        myChannel = mySound.play(pausePosition);
        isPlaying = true;
        // change the display of your button to show the playing state
    }
}

To use an external file 使用外部文件

You would need to use the URLRequest class to point to where the mp3 file is located. 您将需要使用URLRequest类来指向mp3文件所在的位置。 If the file was in the same directory as your published swf file it would look like this. 如果该文件与您发布的swf文件位于同一目录中,则它将看起来像这样。

var mySound:Sound = new Sound(new URLRequest("whatever.mp3"));

You need to use the SoundTransform (flash.media) and SoundChannel (flash.media). 您需要使用SoundTransform(flash.media)和SoundChannel(flash.media)。

var mySound:Sound = new Sound(req);
var mySC:SoundChannel = mySound.play(1);
var myST:SoundTransform = mySC.soundTransform;
myST.volume = 0; // To mute
myST.volume = 1; // To unmute
mySC.soundTransform = myST;

This uses the soundTransform property of a SoundChannel, which lets you, among other things, control the volume. 这使用SoundChannel的soundTransform属性,除其他功能外,它还可以控制音量。 Keep in mind that you have to keep mySound and mySC, while myST will be just a variable created, for example, in a function. 请记住,您必须保留mySound和mySC,而myST只是例如在函数中创建的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM