Flash Play/Pause sound toggle button

筅森魡賤 提交于 2020-01-03 06:05:11

问题


I've googled and googled but got no where or outdated tutorials. Anyone know how to make it so I can toggle audio with buttons using ActionScript 3 on Flash?


回答1:


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.

First, Right click the sound file in your Library and click on Properties.... Inside the Properties window, check the box to Export for Actionscript. Change the Class name to something of your own, like MySong.

Now inside your code instead of pointing to an external Sound file, you will make mySound an instance of 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. If the file was in the same directory as your published swf file it would look like this.

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



回答2:


You need to use the SoundTransform (flash.media) and 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. Keep in mind that you have to keep mySound and mySC, while myST will be just a variable created, for example, in a function.



来源:https://stackoverflow.com/questions/3690178/flash-play-pause-sound-toggle-button

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