Copy link to clipboard
Copied
Hello how's everyone doing? I'm trying to achieve adding background music to a project. The timeline is pretty empty, it has 1 keyframe for each layer. I need to import this song and to make it repeat on loop infinitely until it loads another scene on the main game that I think is coded on "Visual Studio". All the scenes are individual .fla I don't have access.
I imported the sound and linked as "BackgroundMusic". As I didn't find any tutorials, I prompted ChatGpt and gave me this code:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
// Create a Sound object and a SoundChannel object
var bgMusic:BackgroundMusic = new BackgroundMusic();
var bgMusicChannel:SoundChannel;
// Function to start playing the music
function playMusic():void {
// Stop any previously playing sound
if(bgMusicChannel) {
bgMusicChannel.stop();
}
// Play the new sound in a loop
bgMusicChannel = bgMusic.play(0, int.MAX_VALUE);
}
// Function to stop playing the music
function stopMusic(event:Event):void {
if(bgMusicChannel) {
bgMusicChannel.stop();
}
}
// Start playing the music when the scene is loaded
playMusic();
// Stop the music when the scene is removed from the stage
this.addEventListener(Event.REMOVED_FROM_STAGE, stopMusic);
Thanks to everyone taking the time to read and help ♥
Copy link to clipboard
Copied
code looks ok for an as3 project.
is this an as3 project, or canvas?
if as3, show your library to confirm BackgroundMusic is correctly assigned.
Copy link to clipboard
Copied
Thanks for repying! Here it's a screenshot of my library!
Copy link to clipboard
Copied
that looks ok, too.
for testing, import a wav or mp3 file that's been opened in audition and resaved with 44.1 sample rate.
change BackgroundMusic linkage to bgm_bu
and assign linkage BackgroundMusic to the newly imported sound file
test