比如
//A.java
import java.applet.AudioClip;
public class A
{
AudioClip bg_sound;
A()
{
//???创建??
}
public static void main(String[] args)
{
bg_sound.loop();
}
}
//A.java
import java.applet.AudioClip;
public class A
{
AudioClip bg_sound;
A()
{
//???创建??
}
public static void main(String[] args)
{
bg_sound.loop();
}
}
import com.sun.media.sound.*;public class FramAudio{
public FramAudio(){JavaSoundAudioClip player;
try{
FileInputStream ff = new FileInputStream("a.wav");
player= new JavaSoundAudioClip(ff);
player.play();
}catch(Exception e) {
System.out.println("error");
e.printStackTrace();
}}
public static void main(String arg[]){
FramAudio fa=new FramAudio();
}
}
可以放wav和midau没有试过
恩。能运行。奇怪了,com.sun.media.sound包和里边的JavaSoundAudioClip类。我怎么找不到API。
不是J2SE自带的吗?那又如何运行得了?
我想看看API,找找那个类的其他方法。比如循环播放啊,停止啊什么的。
里也有支持的媒体的类,但没用过,不过倒是有帮助.还有就是jmf了,在sun可以下载到.
import java.io.File;import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class Audio extends Thread
{
public AudioInputStream currentSound;
public AudioFormat format;
public Audio() throws Exception
{
try{
File soundFile = new File("ring.wav");
currentSound = AudioSystem.getAudioInputStream(soundFile);
format = currentSound.getFormat();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void play()throws Exception
{
try{
//Sequencer sequencer= MidiSystem.getSequencer();
//sequencer.open();
Clip clip = (Clip)currentSound;
clip.start();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void loop() throws Exception
{
try
{
Clip clip = (Clip)currentSound;
clip.loop(60);
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
try{
Audio ds = new Audio();
ds.play();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
APPLICATION还没
但肯定可以的
java.lang.ClassCastException: javax.sound.sampled.AudioInputStream
俺不懂
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.SourceDataLine;public class TestBase implements Runnable {
private static final int BUFFER_SIZE = 64000;
private String fileToPlay = "ring.wav";
private boolean pause = false;
private boolean stop = false;
private static boolean threadExit = false;
private static boolean stopped = true;
private static boolean paused = false;
private static boolean playing = false;
public static Object synch = new Object();
private Thread playerThread = null; public TestBase() {
} public void run() {
while (! threadExit) {
waitforSignal();
if (! stopped)
playMusic();
}
} public void endThread() {
threadExit = true;
synchronized(synch) {
synch.notifyAll();
}
try {
Thread.sleep(500);
} catch (Exception ex) {}
} public void waitforSignal() {
try {
synchronized(synch) {
synch.wait();
}
} catch (Exception ex) {}
} public void play() {
if ((!stopped) || (paused)) return; if (playerThread == null) {
playerThread = new Thread(this);
playerThread.start();
try {
Thread.sleep(500);
} catch (Exception ex) {}
} synchronized(synch) {
stopped = false;
synch.notifyAll();
}
} public void setFileToPlay(String fname) {
fileToPlay = fname;
} public void playFile(String fname) { setFileToPlay(fname);
play();
} public void playMusic() {
byte[] audioData = new byte[BUFFER_SIZE];
AudioInputStream ais = null;
SourceDataLine line = null;
AudioFormat baseFormat = null;
try {
ais = AudioSystem.getAudioInputStream(new File (fileToPlay));
}
catch (Exception e) {
} if (ais != null) {
baseFormat = ais.getFormat();
line = getLine(baseFormat);
if (line == null) {
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
line = getLine(decodedFormat);
}
}
if (line == null) return; // 不能播放此文件 playing = true;
line.start();
int inBytes = 0;
while ((inBytes != -1) && (!stopped) && (!threadExit)) {
try {
inBytes = ais.read(audioData, 0, BUFFER_SIZE);
}
catch (IOException e) {
e.printStackTrace();
} if (inBytes >= 0) {
int outBytes = line.write(audioData, 0, inBytes);
} if (paused)
waitforSignal();
}
line.drain();
line.stop();
line.close();
playing = false;
} public void stop() {
if(paused) return;
stopped = true;
waitForPlayToStop();
} public void waitForPlayToStop() {
while( playing)
try {
Thread.sleep(500);
} catch (Exception ex) {}
} public void pause() {
if (stopped) return;
synchronized(synch) {
paused = !paused;
synch.notifyAll();
}
} private SourceDataLine getLine(AudioFormat audioFormat) {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(
SourceDataLine.class,
audioFormat);
try {
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
}
catch (Exception e) { }
return res;
}
public static void main(String[] args)
{
try{
System.out.println("test");
TestBase ds = new TestBase();
}catch(Exception e)
{
System.out.println(e);
}
}
}
import sun.audio.*;
import java.io.*;public class TestSound
{
public TestSound()
{
} public void openSound()
{
try {
FileInputStream fileau=new FileInputStream("1.wav");
AudioStream as=new AudioStream(fileau);
AudioPlayer.player.start(as);
}
catch (Exception e) {}
}
public static void main(String args[])
{
TestSound t=new TestSound();
t.openSound();
}
}
把1.wav放在sound目录的上一个目录中。就可以