java播放声音的方法

更新时间:2023-07-10 01:25:06 阅读: 评论:0

我的房东叫别扭
课程设计用的方法
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;

本文发布于:2023-07-10 01:25:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1088273.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:播放   方法   分为   建议
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图