I’m having trouble trying to manipulate music using buttons
-
I can’t seem to make
music1autoplay and loop when the movie starts. -
I want to stop
music1and playmusic2when I press a button:music1in main timeline- button to change music is inside a movie clip
so far here is the code I found on the internet but I’m not sure how I can change it to do what I need:
var mySound:Sound;
var myChannel:SoundChannel;
var isPlaying:Boolean = false;
var isPaused:Boolean = false;
var p:uint = 0;
var songfile:String;
var songtitle:String;
song1_btn.addEventListener(MouseEvent.CLICK, playSound);
song2_btn.addEventListener(MouseEvent.CLICK, playSound);
song3_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function stopSound(myEvent:MouseEvent):void {
if (isPlaying) {
myChannel.stop();
p = 0;
isPlaying = false;
isPaused = false;
}
}
function playSound(myEvent:MouseEvent):void {
switch(myEvent.target.name) {
case "song1_btn":
songfile = "bgm1.mp3";
songtitle = "one";
break;
case "song2_btn":
songfile = "bgm2.mp3";
songtitle = "two";
break;
case "song3_btn":
songfile = "bgm3.mp3";
songtitle = "Three";
break;
}
mySound = new Sound;
mySound.load(new URLRequest(songfile));
title_txt.text = songtitle;
if (isPlaying) {
myChannel.stop();
myChannel = mySound.play(0);
} else {
myChannel = mySound.play(0);
isPlaying = true;
}
}
function pauseSound(myEvent:MouseEvent):void {
if (isPlaying) {
p = Math.floor(myChannel.position);
myChannel.stop();
isPlaying = false;
isPaused = true;
} else if (isPaused) {
myChannel = mySound.play(p);
isPlaying = true;
isPaused = false;
}
}
title_txt.text = "This text will be replaced.";
Note: this code is in the main timeline.
I hope this code is similar to what you are looking for, I tried to keep things simple in case you want to modify anything to better suit your needs: