import
org
.
apache
.
tomcat
.
util
.
http
.
fileupload
.
IOUtils
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMapping
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
RestController
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
javax
.
servlet
.
http
.
HttpServletResponse
;
import
java
.
io
.
InputStream
;
import
java
.
net
.
URL
;
import
java
.
net
.
URLConnection
;
@RequestMapping
(
"/iodemo"
)
@RestController
public
class
IODemo
{
@RequestMapping
(
"/test"
)
public
void
test
(
HttpServletRequest request
,
HttpServletResponse response
)
throws
Exception
{
URL url
=
new
URL
(
"http://媒体文件url"
)
;
URLConnection conn
=
url
.
openConnection
(
)
;
conn
.
setConnectTimeout
(
1000
)
;
conn
.
setReadTimeout
(
5000
)
;
conn
.
connect
(
)
;
InputStream inputStream
=
conn
.
getInputStream
(
)
;
IOUtils
.
copy
(
inputStream
,
response
.
getOutputStream
(
)
)
;
response
.
addHeader
(
"Content-Type"
,
"audio/mpeg;charset=utf-8"
)
;
response
.
flushBuffer
(
)
;
Html代码片段
<!DOCTYPE HTML>
<audio src="http://127.0.0.1:8766/iodemo/test" controls="controls">
Your browser does not support the audio element.
</audio>
</body>
</html>
桌面PC的性能日益提高,Java虚拟机的优化技术也不断获得突破,这一切使得用Java处理实时信号成为可能。本文将通过设计和构造一个支持实时MP3、WAV和Ogg音频格式解码/回放的Java音乐播放器,阐述用JavaSound API编写音频处理程序的思路和一般过程。
JavaSound是一个小巧的低层API,支持数字音频和MIDI数据的记录/回放。在JDK 1.3.0之前,JavaSound是一个标准的Java扩展API,但从Java 2的1.3.0版开始,JavaSound就被包含到JDK之中。由于Java有着跨平台(操作系统、硬件平台)的特点,基于JavaSound的音频处理程序(包括本文的程序)能够在任何实现了Java 1.3+的系统上运行,无需加装任何支持软件
目前在做一个JavaWeb的开发工作,前端传过来一段文本,JavaWeb作为中间层,将文本转发给下一级服务,接收下一级服务返回的音频流,并返回给前端。
在网上查看了一些人写的文件流接收方法,结果总是报错,于是自己摸索着写了一下,希望能给小伙伴们一个参考。
* 文本转语音请求
* @param url
* @param paramMap
* @param textToSpeechResponse
public s
uri: 'sip:example@sip.example.com',
transportOptions: {
wsServers: ['wss://sip.example.com:7443']
authorizationUser: 'example',
password: 'password',
displayName: 'Example User'
2. 注册用户代理
```javascript
userAgent.start();
3. 监听来电事件
```javascript
userAgent.on('invite', (session) => {
// 处理来电
4. 接听来电并获取音频流
```javascript
session.on('accepted', () => {
const remoteAudio = document.createElement('audio');
remoteAudio.autoplay = true;
document.body.appendChild(remoteAudio);
const remoteStream = session.getRemoteStreams()[0];
const remoteAudioTrack = remoteStream.getAudioTracks()[0];
const remoteAudioStream = new MediaStream([remoteAudioTrack]);
remoteAudio.srcObject = remoteAudioStream;
5. 播放音频流
```javascript
const localAudio = document.createElement('audio');
localAudio.autoplay = true;
document.body.appendChild(localAudio);
const localStream = await navigator.mediaDevices.getUserMedia({ audio: true });
const localAudioTrack = localStream.getAudioTracks()[0];
const localAudioStream = new MediaStream([localAudioTrack]);
localAudio.srcObject = localAudioStream;
这些步骤将使您能够使用SIP.js获取音频流并播放。