




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
关于采用UDP协议传输音频的代码请高手指教package, importpackage com.android.test;import java.io.File;import .DatagramPacket;import .DatagramSocket;import .InetAddress;import android.app.Activity;import android.media.AudioFormat;import android.media.AudioManager;import android.media.AudioRecord;import android.media.AudioTrack;import android.media.AudioTrack.OnPlaybackPositionUpdateListener;import android.media.MediaRecorder;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class TestRecroderUDP extends Activity implements OnClickListener /* Called when the activity is first created. */Button startRecording, stopRecording, startPlaying, stopPlaying;boolean isRecording = false;/ 是否录放的标记boolean isPlaying = false;RecordAudio recordTask;PlayAudio playTask;TextView statusTextView;File recordingFile;byte tmpBuf = null;Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);statusTextView = (TextView) this.findViewById(R.id.TextView);startRecording = (Button) this.findViewById(R.id.startRecording);startRecording.setOnClickListener(this);stopRecording = (Button) this.findViewById(R.id.stopRecording);stopRecording.setOnClickListener(this);startPlaying = (Button) this.findViewById(R.id.startPlaying);startPlaying.setOnClickListener(this);stopPlaying = (Button) this.findViewById(R.id.stopPlaying);stopPlaying.setOnClickListener(this);stopRecording.setEnabled(false);startPlaying.setEnabled(false);stopPlaying.setEnabled(false);/*File path = new File(Environment.getExternalStorageDirectory() .getAbsolutePath();path.mkdirs();try recordingFile = File.createTempFile(recording, .pcm, path); catch (IOException e) throw new RuntimeException(Could not create file on SD card, e);*/Overridepublic void onClick(View v) / TODO Auto-generated method stubif (v = startRecording) record(); else if (v = stopRecording) stopRecording(); else if (v = startPlaying) play(); else if (v = stopPlaying) stopPlaying();public void play() startPlaying.setEnabled(false);playTask = new PlayAudio();playTask.execute();stopPlaying.setEnabled(true);statusTextView.setText(playTask.getStatus().toString();public void stopPlaying() isPlaying = false;stopPlaying.setEnabled(false);startPlaying.setEnabled(true);/ playTask.cancel(isPlaying);statusTextView.setText(playTask.getStatus().toString();public void record() startRecording.setEnabled(false);stopRecording.setEnabled(true);startPlaying.setEnabled(true);recordTask = new RecordAudio();recordTask.execute();statusTextView.setText(recordTask.getStatus().toString();public void stopRecording() isRecording = false;/ startRecording.setEnabled(true);stopRecording.setEnabled(false);statusTextView.setText(recordTask.getStatus().toString();private class PlayAudio extends AsyncTask Overrideprotected Void doInBackground(Void. params) / TODO Auto-generated method stub isPlaying = true; int bufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); byte audiodata = new bytebufferSize; System.arraycopy(tmpBuf, 0, audiodata, 0, tmpBuf.length); try / DataInputStream dis = new DataInputStream( new / BufferedInputStream(new FileInputStream( recordingFile); AudioTrack audioTrack = new AudioTrack( AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM); DatagramSocket s = new DatagramSocket(8648); DatagramPacket packet = new DatagramPacket(audiodata, audiodata.length); boolean listen = true; audioTrack.play(); while (listen) s.receive(packet); s.close(); audioTrack.play(); audioTrack.setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener() Override public void onMarkerReached(AudioTrack audioTrack) / TODO Auto-generated method stub Override public void onPeriodicNotification(AudioTrack audioTrack) / TODO Auto-generated method stub );/audioTrack.write(audiodata,0,audiodata.length);/audioTrack.write(packet.getData(),0,audiodata.length); startPlaying.setEnabled(false); stopPlaying.setEnabled(true); catch (Exception e) Log.e(audioTrack, Playing failed); return null;class RecordAudio extends AsyncTask Overrideprotected Void doInBackground(Void. params) / TODO Auto-generated method stub isRecording = true; try / DataOutputStream dos = new DataOutputStream( / new BufferedOutputStream(new / FileOutputStream(recordingFile); int bufferSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); AudioRecord audioRecord = new AudioRecord( MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); byte buffer = new bytebufferSize; audioRecord.startRecording(); int bufferReadResult = 0; int r = 0; while (isRecording) bufferReadResult = audioRecord.read(buffer, 0, buffer.length); /* * for (int i = 0; i bufferReadResult; i+) * dos.writeByte(bufferi); */ tmpBuf = new bytebufferReadResult; System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult); publishProgress(new Integer(r); r+; audioRecord.stop(); / dos.close(); DatagramSocket ds = new DatagramSocket(); DatagramPacket pack = new DatagramPacket(tmpBuf, bufferReadResult, InetAddress.getByName(), 8648); ds.send(pack); ds.close(); catch (Throwable t) Log.e(AudioRecord, Recording failed); return null;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 养殖基地采买合同范本
- 沙石供应合同范本
- 车辆工程劳务合同范本
- 个人雇佣合同范本 开车
- 多项机械租赁合同范本
- 房屋购买合同范本
- 农村协议买房合同范本
- 家具吊线转让合同范本
- 电厂供煤合同范本
- 早餐门面分租合同范本
- 幼小衔接资料合集汇总
- GB/T 42195-2022老年人能力评估规范
- GB/T 4909.4-2009裸电线试验方法第4部分:扭转试验
- GB/T 15155-1994滤波器用压电陶瓷材料通用技术条件
- 复变函数与积分变换全套课件
- 做一名优秀教师课件
- 企业标准编写模板
- 商场开荒保洁计划书
- 设备出厂检验报告
- DBJ 53-T-46-2012 云南省城镇道路及夜景照明工程施工验收规程
- 西方文明史(第五版)英文版全书ppt完整版课件整本书电子教案最全教学教程
评论
0/150
提交评论