我的房东叫别扭
课程设计用的方法
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.MalformedURLException;
世界最大的平原import java.URL;
public class Music extends Frame{
public static String imagePath=System.getProperty("ur.dir")+"/Music/";
public Music(){
try {
URL cb;
//File f = new File(imagePath+"mario.midi");
//File f = new File(imagePath+"1000.ogg");
File f = new File(imagePath+"失败音效.wav");
//File f = new File("d:\\铃声.mp3");
cb = f.toURL()肉丸子汤的家常做法;
AudioClip aau;
少年游苏轼 aau = Applet.newAudioClip(cb);
川西自驾
aau.play();//循环播放 aau.play() 单曲 aau.stop()停止播放
//aau.loop();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new Music();
}
}
因为最近在研究java的语音聊天问题,所以刚刚好写了几个,给你三个播放的方法,分为三个类,建议采用第二或第三个:
package org.bling.music;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.audio.AudioPlayer;
public class MusicTest2 {
private InputStream inputStream = null;
private String file = "./intel.wav";
public MusicTest2(){
} 关于天气的诗句
public void play() throws IOException{
inputStream = new FileInputStream(new File(file));
AudioPlayer.player.start(inputStream);
}
public static void main(String[] args) {
try {
new MusicTest2().play();
} catch (IOException e) {
e.printStackTrace();
}
}
}
----------------------------------------------------------------
package org.bling.music;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class MusicTest {
private AudioFormat audioFormat = null;
private SourceDataLine sourceDataLine = null;
private DataLine.Info dataLine_info = null;
private String file = "./intel.wav";
private AudioInputStream audioInputStream = null;
public MusicTest() throws LineUnavailableException, UnsupportedAudioFileException, IOException{
audioInputStream = AudioInputStream(new File(file));
audioFormat = Format();
dataLine_info = new DataLine.Info(SourceDataLine.class,audioFormat);
sourceDataLine = (Line(dataLine_info);
监测方案
}
public void play() throws IOException, LineUnavailableException{
byte[] b = new byte[1024];
int len = 0;
sourceDataLine.open(audioFormat, 1024);
sourceDataLine.start();
while ((len = ad(b)) > 0){
sourceDataLine.write(b, 0, len);
}
audioInputStream.clo();
sourceDataLine.drain();
sourceDataLine.clo();
}
public static void main(String[] args) {
try {
new MusicTest().play();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------
package org.bling.music;
import java.io.File; 蓝色行星
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;