1

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

我尝试了以下方法:

安卓手机:

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);

我希望有人能帮助我。当然,任何其他想法都值得赞赏。

4

1 回答 1

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;
}

}

于 2013-04-16T06:45:08.720 回答