2 lang20141228 Lang20141228 于 2014.11.17 16:27 提问

java中播放音频文件问题,为撒播放不了?
w l

import java.awt.*;

import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;

import javax.swing.*;
public class Music extends JFrame implements Runnable,ItemListener,ActionListener{
Thread thread;
JComboBox music;
AudioClip clip;
JButton bplay,bloop,bstop;
String str ;
@SuppressWarnings("unchecked")
Music(){
setVisible(true);
setSize(600,400);
thread=new Thread(this);
music=new JComboBox();
music.addItem("选择音频文件");
music.addItem("会呼吸的痛");
music.addItem("再次相信");
music.addItemListener(this);
bplay=new JButton("播放");
bloop=new JButton("循环");
bstop=new JButton("停止");
bplay.addActionListener(this);
bloop.addActionListener(this);
bstop.addActionListener(this);
setLayout(new FlowLayout());
add(music);
add(bplay);
add(bloop);
add(bstop);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}
public void itemStateChanged(ItemEvent e){
    str=music.getSelectedItem().toString();
    if(!(thread.isAlive())){
        thread=new Thread(this);

    }
    try{
        thread.start();
    }
    catch(Exception ee){}
}
public void run() {
    try{File file=new File(str);
        URI uri=file.toURI();
        URL url=uri.toURL();

        clip=Applet.newAudioClip(url);




    }
    catch(Exception e){}
}


public void actionPerformed(ActionEvent e) {
    if(e.getSource()==bplay)
        clip.play();
    else if( e.getSource()==bloop)
        clip.loop();
    else if( e.getSource()==bstop)
        clip.stop();
}



public static void main(String args[]){
    new Music();
}

}

1个回答

max_min_
max_min_   Rxr 2014.11.18 09:22

什么格式的音频数据,对应解码是否正确呢?

Csdn user default icon
上传中...
上传图片
插入图片