4

I embedded an mp3 file be used as background music for my app. Though it works fine, the problem is that it doesn't play the whole track, it just plays the first 32 seconds of it (the mp3 file is 1:30 min).

Does anyone here has any idea why?

I've read here that maybe the sound doesn't fit into the supported flash player sound format, but I don't think that's the problem! The file is not that big, but maybe I am wrong?

Any idea whats causing the problem? Or how to fix it? The code is fine, am sure of it (its pretty simple. Just embedded the mp3, initialized the required variable and played the sound. Nothing fancy)

EDIT: the mp3 is encoded at 44100 KHz

EDIT Here is the code, just incase

package 
{
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;

    public class BackgroundMusic extends Sprite
    {
        [Embed(source="swfs/bg.mp3")]       
        private var BG:Class;

        public function BackgroundMusic() 
        {   
            var backgroundMusic:Sound = new BG();
            backgroundMusic.play();
        }

    }
}
Community
  • 1
  • 1
r3x
  • 2,125
  • 5
  • 23
  • 39
  • Have you tried with another mp3 (just to identify the cause)? – Kodiak Jan 10 '12 at 17:26
  • I just checked. I added a random song from my library and now it doesn't play at all. trace(backgroundMusic.length) actually gives out 0 ...Thats weird – r3x Jan 10 '12 at 17:37
  • The new mp3 I just tried is larger than the original one (the one that played for 30). So maybe its a size issue? – r3x Jan 10 '12 at 17:41
  • What does the length give you on your original mp3 file? – meddlingwithfire Jan 10 '12 at 19:17
  • 36301.49659863945, which is 36 seconds, it plays for 36 seconds not 30 (or 32 or whatever) as i said (my mistake, i miscalculated it). The weird thing is when i just switched to another mp3 (a larger one) the got a length of 0... – r3x Jan 10 '12 at 19:41
  • Try a very short mp3 (~10 seconds or so) as well as one that's ~30 seconds. It might be a format problem, a size problem, or something else entirely. I don't know what else it might be though. It's really odd that the length is different from the actual file size. – Mar Jan 10 '12 at 22:08
  • Also, try something other than mp3, such as wav. – Mar Jan 10 '12 at 22:08

4 Answers4

2

Well, it's been 2 years since original question, but I had the same problem, but only with short (under 2 seconds) files. Turned out the problem was with metadata. If metadata says sound is 1 second long and in reality it's 1.5 seconds, Flash will only play 1 second of sound cutting the rest off.

I solved the problem by not including metadata with the file when converting from wav to mp3.

Hope that helps someone.

dimoniy
  • 5,820
  • 2
  • 24
  • 22
2

As the aforementioned link suggests, the problem was indeed with the mp3 file itself. Basically it was too big. So after reducing it from 44kHz stereo 32 bit to 44kHz stereo 16 bit it worked fine, and now it runs all the way through. With that said its kinda weird that we can't embed higher quality mp3 files. I presume that this problem wouldn't be an issue with loading (rather than embedding) but I haven't tested it. If anyone here has an idea on how to fix this problem without reducing the quality of the mp3 please share

r3x
  • 2,125
  • 5
  • 23
  • 39
0

I am pretty sure music will restart every time you tell it to play. Is it possible that you are telling it to play several times? If it is always EXACTLY 32 seconds then I don't know but if it is always AROUND the same time maybe pay attention to what you are doing around that time and check if it is possible that you are calling the play() function again.

If your App has some sort of 30 seconds timer somewhere it can definitely be responsible.

EDIT: hehe If the sound is the only thing present in the entire app then never mind. But it's good to keep in mind anyway.

Reafexus
  • 333
  • 1
  • 4
  • 17
  • I added the code if that helps. and yes it stops at EXACTLY 32 seconds. Am calling play() so it just plays the file once and it is not tied to any timer that's for sure. – r3x Jan 10 '12 at 17:31
0

Your background Sound instance is probably getting garbage collected., since you're not maintaining a reference to it. Try this:

package 
{
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;

    public class BackgroundMusic extends Sprite
    {
        [Embed(source="swfs/bg.mp3")]       
        private var BG:Class;

        private var _backgroundMusic:Sound;

        public function BackgroundMusic() 
        {   
            _backgroundMusic = new BG();
            _backgroundMusic.play();
        }
   }
}
meddlingwithfire
  • 1,437
  • 8
  • 12
  • I don't think it is a garbage collection problem, since am calling play() from backgroundMusic. Regardless I tried it out and it doesn't fix the problem. It's the same, it just stops at 30 sec. Worth a shot though, thanks anyways =) – r3x Jan 10 '12 at 19:05