1

I have a problem with my code where each time I run the project, this is thrown. Now I have narrowed it down to that the sound does play when I call the play() method but does not when I call the playL() method.

package net.chrypthic.Ball;
import sun.audio.*;
import java.io.*;

public class SoundManager {

AudioPlayer ap = AudioPlayer.player;
AudioStream as;
ContinuousAudioDataStream loop = null;
public SoundManager(String music)
{
    try
    {
        InputStream input = new FileInputStream("./"+music);
        as = new AudioStream(input);
        AudioData ad = as.getData();
        loop = new ContinuousAudioDataStream(ad);
    }catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}
public void play()
{
    ap.start(as);
}
public void stop()
{
    ap.start(as);
}
public void playL()
{
    ap.start(loop);
}
public void stopL()
{
    ap.start(loop);
}
}

Why? I pass sound/gsong1b.wav to it which has a size of 6.2MB, is 2 minutes long and has a bit rate of 16000Hz. I have heard that sounds have to be less that 4mb big but it plays, and only errors when I loop.... Any Help would be greatly appreciated.

chrypthic
  • 579
  • 1
  • 6
  • 15

3 Answers3

2

Those classes you use (AudioPlayer, AudioStream), even though they are from the official Java JDK, are in fact reserved classes, meaning that Oracle (and Sun before them) reserves the right to change them without notice. You should use the official sound API instead:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

//...

public static void main(String[] args) throws Throwable {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(SoundManager.class.getResourceAsStream("C://temp/my.mp3"));
        clip.open(inputStream);
        clip.start(); 
    }
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • Thanks for the quick reply, now it throws javax.sound.sampled.LineUnavailableException: Failed to allocate clip data: Requested buffer too large. Any ideas? – chrypthic Oct 06 '11 at 15:59
  • Do I have to change the buffer size, and if how do I do it? – chrypthic Oct 06 '11 at 16:16
  • That's kind of a puzzler for me as well I'm afraid. It could be because of your sound card drivers, try updating those, or it could be your sound file that is not good as far as Java is conerned. If it's the latter I'm afraid you're on own, try googling it a little... – Shivan Dragon Oct 06 '11 at 19:14
  • Well, thanks though. I think ill just make my sound 4 mins long and add a timer to the level (its a game). – chrypthic Oct 07 '11 at 14:21
0

Using an IDE goto Action performed metthod of your sound button. For continuous playing of .wav files. I have use the following code and it works fine using thread. Make sure you import thev following. import sun.audio.; and import java.io.;

        Thread sound;
        sound = new Thread(){

           public void run(){

             AudioPlayer MGP=AudioPlayer.player;
             AudioStream BGM;
             AudioData MD;
             ContinuousAudioDataStream loop=null;
  for(;;){

        try{ 
         BGM=new AudioStream(new FileInputStream("C:\\Users\\HAMMED\\01FATIHA         (New).wav"));//enter the sound directory and name here
         AudioPlayer.player.start(BGM);

         //MD=BGM.getData();//not necessary
         //loop=new ContinuousAudioDataStream(MD);//not necessarry

        sleep(48000);// enter the elapse time of ur sond to avoid noise
    }catch(Exception e){

       JOptionPane.showMessageDialog(null, e);
    }


    MGP.start(loop);// It does nothing.I was trying to use this but no success.
        }
     }
  };
    sound.start();        
0

Have a try with HeadspaceMixer instead. javax.sound is not a completed implementation.

Marc
  • 351
  • 4
  • 13