




已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(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ú)特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- C1跨學(xué)科學(xué)習(xí)活動(dòng)設(shè)計(jì)與文本閱讀心得體會(huì)
- 六年級(jí)應(yīng)急安全演練教學(xué)計(jì)劃
- 倍速生活視角下的2025高三作文范文
- 化工廠工程質(zhì)量目標(biāo)及安全措施
- 小學(xué)一年級(jí)語(yǔ)文教學(xué)評(píng)價(jià)心得體會(huì)
- 中學(xué)2024-2025學(xué)年度第一學(xué)期實(shí)驗(yàn)教學(xué)計(jì)劃
- 以形助數(shù)以數(shù)解形:數(shù)形結(jié)合法在高中數(shù)學(xué)教學(xué)中的深度融合與應(yīng)用
- 以實(shí)踐為翼:基于核心素養(yǎng)的高中地理野外考察-孝順中學(xué)的探索與啟示
- 教科版科學(xué)四年級(jí)上冊(cè)教學(xué)能力提升計(jì)劃
- 2024-2025年春季第二學(xué)期小學(xué)德育科技教育計(jì)劃
- GB/T 4074.8-2009繞組線試驗(yàn)方法第8部分:測(cè)定漆包繞組線溫度指數(shù)的試驗(yàn)方法快速法
- GB/T 28575-2020YE3系列(IP55)三相異步電動(dòng)機(jī)技術(shù)條件(機(jī)座號(hào)63~355)
- 國(guó)際公法學(xué) 馬工程課件 4 第四章
- 青海省西寧市《職業(yè)能力測(cè)試》事業(yè)單位國(guó)考真題
- 溝通中的提問技巧課件
- 2023年浙江黃龍?bào)w育發(fā)展有限公司招聘筆試模擬試題及答案解析
- 外科學(xué)骨折概論課件
- 阿片類藥物鎮(zhèn)痛機(jī)制課件
- 產(chǎn)品成本核算流程圖
- 母線槽安裝檢驗(yàn)批質(zhì)量驗(yàn)收記錄
- 生物吸附課件
評(píng)論
0/150
提交評(píng)論