流音频Android - PC

时间:2013-04-16 06:29:52

标签: android audio stream

我正在尝试将麦克风输入数据流式传输到PC,该PC将在其扬声器上输出流。应该压缩流并稍后加密。

我尝试了以下内容:

Android手机:

AudioGroup audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_NORMAL);
AudioStream audioStream = new AudioStream(InetAddress.getLocalHost());
int port = audioStream.getLocalPort();
// tell other side which port to use (this works)
daos.writeInt(port);

audioStream.setCodec(AudioCodec.PCMU);
audioStream.setMode(RtpStream.MODE_SEND_ONLY);
audioStream.associate(adr, port);
audioStream.join(audioGroup);
AudioManager Audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Audio.setMode(AudioManager.MODE_IN_COMMUNICATION);

接收方的Java程序。如何使用UDP套接字进行传输?

DatagramSocket datagramSocket = new DatagramSocket(port);
System.out.println("Waiting for audio data...");  
source = AudioSystem.getAudioInputStream(????.getInputStream());
AudioSystem.getAudioInputStream(AudioFormat.Encoding.ULAW, source);
byte[] audioData = new byte[4096];
source.read(audioData, 0, 4096);
toSpeaker(audioData);

我希望有人可以帮助我。当然,任何其他想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

试试这段代码。

公共类StreamAudioDemo扩展了Activity实现OnClickListener,OnPreparedListener,OnCompletionListener,OnErrorListener {

private MediaPlayer mp;
private ProgressDialog pd;
private Button btnplay;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnplay=(Button)findViewById(R.id.play);
    btnplay.setOnClickListener(this);
}
@Override
public void onPrepared(MediaPlayer mp) {
      Log.i("StreamAudioDemo", "prepare finished");
      pd.setMessage("Playing.....");
      mp.start();
}

@Override
public void onClick(View v) {
    try
    {
        pd = new ProgressDialog(this);
        pd.setMessage("Buffering.....");
        pd.show();
        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnPreparedListener(this);
        mp.setOnErrorListener(this);  //http://www.youtube.com/watch?v=pj-T_LxSCng
        mp.setDataSource("http://www.robtowns.com/music/blind_willie.mp3");
        //mp.setDataSource("http://www.youtube.com/watch?v=pj-T_LxSCng");
        mp.prepareAsync();
        mp.setOnCompletionListener(this);
    }
    catch(Exception e)
    {
        Log.e("StreamAudioDemo", e.getMessage());
    }

}

@Override
public void onCompletion(MediaPlayer arg0) {
    pd.dismiss();
    Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();     

}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    pd.dismiss();
    return false;
}

}