package com.xzz.applet.service;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class SimpleAudioPlayer {
public SourceDataLine sdLine;
public AudioFormat audioFormat;
private PlayThread playThread;
private Socket soc;
public boolean stop = true;
public SimpleAudioPlayer(Socket soc) throws LineUnavailableException {
audioFormat = MyAudioFormat.getInstance();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
audioFormat);
sdLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sdLine.open();
this.soc = soc;
}
public void start() throws IllegalRecordStateException {
if (stop) {
sdLine.start();
playThread = new PlayThread();
playThread.start();
} else {
throw new IllegalRecordStateException(
"Illegal playing state! Player is active at the time.");
}
}
public void stop() {
stop = true;
playThread.stopService();
playThread = null;
sdLine.stop();
}
public void close(){
sdLine.close();
}
class PlayThread extends Thread {
private BufferedInputStream in;
@Override
public void run() {
try {
in = new BufferedInputStream(soc.getInputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
byte[] buf = new byte[1024 * 1];
int readed = -1;
try {
while (stop) {
if ((readed = in.read(buf)) > 0) {
sdLine.write(buf, 0, readed);
} else
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopService(){
try {
in.close();
} catch (Exception e) {
}
}
}
}
java sound 简单播放器代码
4星 · 超过85%的资源 需积分: 9 32 浏览量
2011-02-19
15:50:05
上传
评论
收藏 813B RAR 举报
Lostdroid
- 粉丝: 1
- 资源: 3
最新资源
- 基于matlab实现说话人语音识别源码+项目说明+PPT+报告(高分项目).zip
- VSc++编程助手1.0 - 1
- 基于matlab的说话人语音识别源码+PPT+报告(优质项目).zip
- 语音识别基于matlab说话人识别系统源码+报告PPT(高分优质项目).zip
- 毕业设计: 校园失物招领小程序的设计与实现论文(源码 + 数据库 + 说明文档)
- 美易ME-DUI自绘模块3.3-易语言模块
- 毕业设计:小程序消防知识每天学(源码 + 数据库 + 说明文档)
- 2024-NEW.zip
- 软件.zip
- 华为FPGA设计技巧FPGA设计规范设计经验设计流程指南华为面经硬件工程师手册等资料(15个).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈