




已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
關(guān)于采用UDP協(xié)議傳輸音頻的代碼請高手指教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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學(xué)美術(shù)學(xué)科培訓(xùn)
- ICU護理學(xué)習(xí)文獻匯報
- 電梯安全知識教育
- 建筑企業(yè)質(zhì)量安全月培訓(xùn)
- 海關(guān)監(jiān)管體系課件
- 個人舞蹈教室租賃合同模板
- 罐頭食品HACCP體系評估與優(yōu)化合同
- 企業(yè)股權(quán)收購撤銷及利益分配合同
- 餐飲行業(yè)食品安全事故處理協(xié)議
- 知名餐飲品牌總經(jīng)理任職及品牌推廣合同
- 紫銅材質(zhì)證明
- 新產(chǎn)品評審管理辦法
- (參考)菲達公司國內(nèi)電除塵器業(yè)績表
- 游泳池水質(zhì)檢測記錄表
- 大學(xué)生職業(yè)生涯規(guī)劃與就業(yè)指導(dǎo)教案第5講:興趣探索
- 門店電表記錄表
- 七年級勞技 花卉種植 花卉用途 PPT學(xué)習(xí)教案
- 隧道換拱專項施工方案
- 國際金融托馬斯普格爾復(fù)習(xí)資料整理
- 基于單片機的報警器與旋轉(zhuǎn)燈設(shè)計(共21頁)
- 中國農(nóng)業(yè)銀行房地產(chǎn)押品價值評估操作模板
評論
0/150
提交評論