哪位有能播放声音文件的java代码?多谢了 

解决方案 »

  1.   

       //applet程序 
        import java.applet.*;
        import java.awt.*;
        class A extends Applet
    {
        public void init()
        {
            AudioClip=ac;
         setSize(500,460);
         ac=getAudioClip(getCodeBase(),"Music/a.mid");
        }
        public   void   start()   
        {   
         ac.loop();   
        }   
    }
    //在此代码的地方建一个文件夹Music,在其中放入声音文件注意名字
      

  2.   

    学习。这是.mid格式的
    如果要求播放.mp3格式的呢?
      

  3.   

    去网上找JMF的资料。有好多的。
    我还找到有MP3播放器的代码呢。
      

  4.   

    public Demo(String file_name){
    try{
    AudioPlayer.player.start(new FileInputStream(file_name));
    }catch(Exception e){
    }
    }
      

  5.   

    import javax.media.bean.playerbean.*;   //这个包要用到JMF
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class mp31 extends Applet implements ActionListener
    { Button b1,b2;
    MediaPlayer player;
    public void init()
    {
    player=new MediaPlayer();

    setLayout(new FlowLayout());
    player.setMediaLocation("file:/F:\\音乐风\\东风破.mp3");//音频文件路径
    b1=new Button("播放");
    b2=new Button("停止");
    add(b1);
    add(b2);

    b1.addActionListener(this);
    b2.addActionListener(this);
    setSize(200,200);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    player.start();

    }
    else if(e.getSource()==b2)
    {
    player.stop();

    }
    }
    }
      

  6.   

    谢谢purerabbit,我试了上面的代码,确实可以!
      

  7.   

    import java.io.File;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Sequence;
    import javax.sound.midi.Sequencer;public class MediaPlayer{
    public static void main(String[] args)throws Exception {
    Sequencer midi2 = MidiSystem.getSequencer(false);
    String filename="D:/My Documents/My Music/778616.mid";
    midi2.open();
            Sequence s1 = MidiSystem.getSequence(new File(filename));
            midi2.setSequence(s1);
            midi2.start();
            Thread.sleep(midi2.getMicrosecondLength()/1000);
            System.exit(1);
    }
    }
      

  8.   

    /*
     * Created on Jun 11 ,2008
     * 
     * Copyright by cuckoo
     */
    package prepare;import static org.jouvieje.FmodEx.Defines.FMOD_TIMEUNIT.FMOD_TIMEUNIT_MS;
    import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_ERR_CHANNEL_STOLEN;
    import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_ERR_INVALID_HANDLE;
    import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_OK;
    import static org.jouvieje.FmodEx.Misc.BufferUtils.newFloatBuffer;import java.io.File;
    import java.nio.ByteBuffer;
    import java.nio.FloatBuffer;
    import java.nio.IntBuffer;
    import org.jouvieje.FmodEx.Channel;
    import org.jouvieje.FmodEx.FmodEx;
    import org.jouvieje.FmodEx.Init;
    import org.jouvieje.FmodEx.Sound;
    import org.jouvieje.FmodEx.System;
    import org.jouvieje.FmodEx.Defines.FMOD_INITFLAGS;
    import org.jouvieje.FmodEx.Defines.FMOD_MODE;
    import org.jouvieje.FmodEx.Defines.INIT_MODES;
    import org.jouvieje.FmodEx.Enumerations.FMOD_CHANNELINDEX;
    import org.jouvieje.FmodEx.Enumerations.FMOD_DSP_FFT_WINDOW;
    import org.jouvieje.FmodEx.Enumerations.FMOD_RESULT;
    import org.jouvieje.FmodEx.Exceptions.InitException;
    import org.jouvieje.FmodEx.Misc.BufferUtils;
    import org.jouvieje.FmodEx.Structures.FMOD_CREATESOUNDEXINFO;/**
     * 
     * @author cuckoo
     *
     */public class CreateAudioSystem {

    public static void main(String args[]){
    new CreateAudioSystem().createContents();
    }
    public void createContents(){
    initSystem();

    _soundFile = new File("media\\In a Glade.mp3");
    String soundFilename = _soundFile.getAbsolutePath();
    java.lang.System.out.println("-> file name :"+soundFilename);
    int mode = FMOD_MODE.FMOD_2D | FMOD_MODE.FMOD_SOFTWARE | FMOD_MODE.FMOD_CREATESTREAM;
    //        mode |= FMOD_MODE.FMOD_HARDWARE;
            
            //create a exinfo
            FMOD_CREATESOUNDEXINFO exinfo = FMOD_CREATESOUNDEXINFO.create();
            //set this found`s length 
    //        exinfo.setLength( (int)_soundFile.length());
            _audioSystem.createStream(soundFilename, mode, null, _sound);
            ByteBuffer buffer = BufferUtils.newByteBuffer(1024);
            _sound.getLength(buffer.asIntBuffer(), FMOD_TIMEUNIT_MS);
            java.lang.System.out.println("-> sound length is "+buffer.getInt(0));
            java.lang.System.out.println("-> start play");
            startPlay();
    }
    public void startPlay(){
     _audioSystem.playSound(FMOD_CHANNELINDEX.FMOD_CHANNEL_FREE, _sound, true, _channel);
            java.lang.System.out.println("-> play");
            _channel.setPaused(false);
    do
    {
    _audioSystem.update();
    // int ms = 0;
    // boolean playing = false;

    if(_channel != null && !_channel.isNull())
    {
    Sound currentsound = new Sound();
    /*
    _result = _channel.isPlaying(_buffer);
    if((_result != FMOD_OK) && (_result != FMOD_ERR_INVALID_HANDLE) && (_result != FMOD_ERR_CHANNEL_STOLEN))
    java.lang.System.out.println("-> error");
    playing = _buffer.get(0) != 0;
    */
    _result = _channel.getPaused(_buffer);
    if((_result != FMOD_OK) && (_result != FMOD_ERR_INVALID_HANDLE) && (_result != FMOD_ERR_CHANNEL_STOLEN))
    java.lang.System.out.println("-> error");
    _paused = _buffer.get(0) != 0;

    /*_result = _channel.getPosition(_buffer.asIntBuffer(), FMOD_TIMEUNIT_MS);
    if((_result != FMOD_OK) && (_result != FMOD_ERR_INVALID_HANDLE) && (_result != FMOD_ERR_CHANNEL_STOLEN))
    java.lang.System.out.println("-> error");
    ms = _buffer.getInt(0);*/

    _channel.getCurrentSound(currentsound);
    FloatBuffer floatBuffer = newFloatBuffer(512);
    _channel.getSpectrum(floatBuffer, floatBuffer.limit(), 0, FMOD_DSP_FFT_WINDOW.FMOD_DSP_FFT_WINDOW_TRIANGLE);
    for(int i = 0 ; i < floatBuffer.limit() ; i ++){
    float value = floatBuffer.get(i);
    if(value != 0f){
    java.lang.System.out.println("----------------"+value);
    }else{
    java.lang.System.out.println("--------2--------");
    }
    }
    if(!currentsound.isNull())
    {
    _result = currentsound.getLength(_buffer.asIntBuffer(), FMOD_TIMEUNIT_MS);
    if((_result != FMOD_OK) && (_result != FMOD_ERR_INVALID_HANDLE) && (_result != FMOD_ERR_CHANNEL_STOLEN))
    java.lang.System.out.println("-> error");
    _songLength = _buffer.getInt(0);
    }
    _channel.getPosition(_buffer.asIntBuffer(), FMOD_TIMEUNIT_MS);
    _currentLength = _buffer.getInt(0);
    if( _currentLength > _previous){
    _previous = _currentLength;
    }else{
    if( _previous > 4){
    _paused = true; 
    }
    }
    }
    // java.lang.System.out.println("-> play location "+_currentLength);
    // java.lang.System.out.println("-> song length "+ _songLength);
    try {
    //sleep 10 ms
    Thread.sleep(10);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }while( true);
    }
    public void initSystem(){
    if( _audioSystem == null ){
    /**
     * Load libraries
     */
    try {
    Init.loadLibraries(INIT_MODES.INIT_FMOD_EX);
    } catch (InitException e) {
    // TODO Auto-generated catch block
    java.lang.System.out.println("-> load not success");
    }
    java.lang.System.out.println("-> load libraries is end ");
    /*
     * Create a system and initial it
     */
    System audioSystem = new System();
    _result = FmodEx.System_Create( audioSystem );
    IntBuffer intBuffer = BufferUtils.newIntBuffer( 1 );
    audioSystem.getVersion(intBuffer);
                java.lang.System.out.println("version "+intBuffer.get(0));
                audioSystem.init(_maxChannels, FMOD_INITFLAGS.FMOD_INIT_NORMAL, null);
                _audioSystem = audioSystem ;
    }
    }

    private System      _audioSystem   = null ;
    private File        _soundFile     = null;
    private int         _maxChannels   = 32;
    private int         _currentLength = 0;
    private int         _previous      = 4;
    private int         _songLength    = 0;
    private boolean     _paused        = false;
    private Sound       _sound         = new Sound();
    private Channel     _channel       = new Channel();
    private FMOD_RESULT _result        = null;
    private ByteBuffer  _buffer       = BufferUtils.newByteBuffer(1024);

    }