Java字节数组播放声音 [英] java byte array play sound

查看:223
本文介绍了Java字节数组播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,它从数据线读取话筒向字节数组
我不是100%肯定它的工作原理,但只是通过打印数组它似乎昌当我发出一点声音麦克风​​旁的一部分:)

I have a small program that reads from dataline mic to byte array I am not 100% sure it works but just by printing part of the array it seem to chang when am making a sound next to the mic:)

我喜欢玩的声音回我如何从缓冲区(字节数组音)播放数据??

I like to play the sound back how do i play the data from the buffer(byte array to sound)??

 package mic; 
import javax.sound.sampled.*;

public class Mic extends Thread{
    boolean flag;
    TargetDataLine mic;
    byte[] buffer;
    AudioFormat format;

    public static void main(String[] args) {
       Mic a=new Mic();
       a.start();
    }
    @Override
    public void run()
    {
        flag=true;
        startMic();
        while(flag)
        {
            send();
        }
    }
    public void send()
    {
        try{
            mic.read(buffer,0,512);
            System.out.println("1. "+buffer[0]);
        }catch(Exception ex)
        {

        }
    }
    public void startMic()
    {
        try{
            format=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,8000.0F,16,2,4,8000.0F,true);
            DataLine.Info info=new DataLine.Info(TargetDataLine.class,format);
            mic=(TargetDataLine)AudioSystem.getLine(info);
            mic.open();
            mic.start();
            buffer=new byte[512];
        }catch(Exception exx)
        {
            System.out.println("exx");
        }

    }
}

编辑:结果
我想到底做的是字节数组发送到其他的应用程序和游戏马上结果
就像一个电台现场直播


what i wanted to do in the end is send the byte array to other app and play right away
like live stream of a radio

推荐答案

根据

// Create the AudioData object from the byte array
AudioData audiodata = new AudioData(byteArray);
// Create an AudioDataStream to play back
AudioDataStream audioStream = new AudioDataStream(audioData);
// Play the sound
AudioPlayer.player.start(audioStream);

这篇关于Java字节数组播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆