




已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
關(guān)于采用UDP協(xié)議傳輸音頻的代碼請(qǐng)高手指教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;/ 是否錄放的標(biāo)記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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- Chitinovorin-A-生命科學(xué)試劑-MCE
- 自身免疫性關(guān)節(jié)炎治療新突破:2025年免疫治療應(yīng)用案例分析
- 物聯(lián)網(wǎng)設(shè)備安全漏洞防護(hù)策略與智能交通安全報(bào)告2025
- 工業(yè)互聯(lián)網(wǎng)平臺(tái)邊緣計(jì)算硬件架構(gòu)創(chuàng)新設(shè)計(jì)研究報(bào)告
- 2025年不良資產(chǎn)處置行業(yè)市場(chǎng)格局與創(chuàng)新模式發(fā)展策略研究
- 低碳城市規(guī)劃與城市交通擁堵治理案例解析
- 電商知識(shí)產(chǎn)權(quán)保護(hù)與電子商務(wù)平臺(tái)知識(shí)產(chǎn)權(quán)保護(hù)與知識(shí)產(chǎn)權(quán)保護(hù)法律法規(guī)實(shí)施報(bào)告
- 審計(jì)處突發(fā)事件應(yīng)急預(yù)案突發(fā)事件應(yīng)急預(yù)案【六篇】
- 華晨寶馬供應(yīng)商管理制度
- 智慧食堂個(gè)人管理制度
- 煤礦安全規(guī)程露天部分參考題庫(kù)(含答案)
- 有趣的英漢互譯-課件
- 紫銅材質(zhì)證明
- (參考)菲達(dá)公司國(guó)內(nèi)電除塵器業(yè)績(jī)表
- 步進(jìn)式加熱爐耐材砌筑施工方案
- GB-T12232-2005- 通用閥門 法蘭連接鐵制閘閥
- 大學(xué)生職業(yè)生涯規(guī)劃與就業(yè)指導(dǎo)教案第5講:興趣探索
- 2022年中國(guó)電信店長(zhǎng)技能四級(jí)認(rèn)證教材
- 門店電表記錄表
- 七年級(jí)勞技 花卉種植 花卉用途 PPT學(xué)習(xí)教案
- 常見(jiàn)散料堆積密度匯總-共10
評(píng)論
0/150
提交評(píng)論