Could not create AudioData object?

你离开我真会死。 提交于 2019-12-13 02:31:17

问题


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.


回答1:


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(); 
    }



回答2:


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




回答3:


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();        


来源:https://stackoverflow.com/questions/7676723/could-not-create-audiodata-object

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