網路對講機C#伺服器 Android用戶端(三) android用戶端程式碼分析 附加

來源:互聯網
上載者:User

完整的工程(源碼):http://download.csdn.net/detail/zhujinghao09/5313666 

錄音功能的實現,開始的時候使用android系統內建錄音類,但他的編碼方式,pc的windowsmediaPlayer 不識別無法播放,所以從網上找一個無壓縮的錄音類,錄音檔案格式和windows 一樣.

這個錄音類宅這裡就不貼出來了,有興趣可以eoe論壇上找一找,也可以下載我的工程源碼,裡面有

在這簡單的使用其實現無壓縮錄音檔案產生:

 

/** * 錄製wav格式檔案 * @param path : 檔案路徑 */public static File recordChat(ExtAudioRecorder extAudioRecorder,String savePath,String fileName) {File dir = new File(savePath);//如果該目錄沒有存在,則建立目錄if(dir.list() == null){dir.mkdirs();}//擷取錄音檔案File file=new File(savePath+fileName);//設定輸出檔案extAudioRecorder.setOutputFile(savePath+fileName);extAudioRecorder.prepare();//開始錄音extAudioRecorder.start();return file;}/** * 停止錄音 * @param mediaRecorder 待停止的錄音機 * @return 返回 */public static void stopRecord(final ExtAudioRecorder extAudioRecorder){extAudioRecorder.stop();extAudioRecorder.release();}

//錄音按鈕

btnRecod.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View arg0) {int h = new Date().getHours();int m = new Date().getMinutes();int s = new Date().getSeconds();String strTime = String.valueOf(h)+"-"+String.valueOf(m)+"-"+String.valueOf(s);//獲得系統時間命名檔案String savePath="sdcard/MyVoice/";String fileName="ExtAudio"+strTime+".wav";// TODO Auto-generated method stubextAudioRecorder = ExtAudioRecorder.getInstanse(false); //未壓縮的錄音(WAV)recordChat(extAudioRecorder,savePath,fileName);//mRecorder.onStart("er"+strTime);  //  lastRecordPath="sdcard/MyVoice/"+"er"+strTime+".amr";lastRecordPath=savePath+fileName;           //全域變數}});

停止錄音按鈕

 

 

btnStop.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//mRecorder.onStop();stopRecord(extAudioRecorder);vPathArrayList.add(lastRecordPath);UpdateVoiceListView(vPathArrayList);             //把錄音檔案路徑添加到聲音列表 }});

播放功能是採用android 系統內建的mediaPlayer

 

播放類:

 

package er.fly;import java.io.IOException;import android.media.MediaPlayer;public class MyMediaPlay {MediaPlayer mediaPlayer ;public void onPlay(String namePath){mediaPlayer = new MediaPlayer();if(mediaPlayer.isPlaying()){mediaPlayer.reset();}try {mediaPlayer.setDataSource(namePath);mediaPlayer.prepare();mediaPlayer.start();mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){@Overridepublic void onCompletion(MediaPlayer arg0) {// TODO Auto-generated method stubmediaPlayer.release();}});} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void onStop(){mediaPlayer.stop();mediaPlayer.release();mediaPlayer=null;}}

播放和發送的檔案路徑都是從聲音列表中讀取的

 

 

btnPlay.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View arg0) {Log.i(Tag, "Play   "+playSendPath);if(playSendPath!=null){mPlay.onPlay(playSendPath);tvRecvCount.setText("");}elseToast.makeText(MainActivity.this, "請選擇要播放的聲音!!", Toast.LENGTH_LONG).show();playSendPath=null;}});btnSend.setOnClickListener(new Button.OnClickListener(){String Path=null;File file;// = new File("sdcard/MyRecvVoice/androidrecv.mp3");@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif(playSendPath!=null){file=new File(playSendPath);}else{Toast.makeText(MainActivity.this, "請選擇要發送的聲音!!", Toast.LENGTH_LONG).show();return;}playSendPath=null;UpFile upFile = new UpFile(client);upFile.SetFile(file);upFile.start();}});

其中

playSendPath 是全域變數通過挑選清單控制項中元組來百變它的值
public void UpdateVoiceListView(ArrayList<String> al)    {    String[]str =new String[al.size()];    for(int i=0;i<al.size();i++)    {    str[i]=al.get(i);    }    ArrayAdapter<String> aad= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, str);    setListAdapter(aad);    }    public void onListItemClick(ListView parent, View v,         int position, long id) {            CheckedTextView item = (CheckedTextView) v;            String path=vPathArrayList.get(position);            Log.i(Tag, path);            playSendPath=path;        }


 


最後  就是主線程處理來自子線程的訊息類:  主要功能是更新主介面控制項狀態

 

 

package er.fly.nettandclient;import java.util.ArrayList;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.util.Log;import android.widget.TextView;public class MyHandler extends Handler {TextView tvRecvCount;String Tag ="MyHandler";//ArrayList<String> voiceAList;public MyHandler(Looper looper){        super (looper);} public void SetHandlerArgs(TextView tv) { tvRecvCount = tv; } @Override public void handleMessage(Message msg) { // 處理訊息 MainActivity ma = new MainActivity(); String path; String Dflag="DownloadFile:"; String strMsg=msg.obj.toString(); Log.i(Tag, strMsg); if(strMsg.indexOf(Dflag)==0) { int index1 = strMsg.indexOf("#"); int index2 = strMsg.indexOf("$"); if(index1>0) { path = strMsg.substring(Dflag.length(),index1); Log.i(Tag, path); MainActivity.vPathArrayList.add(path); Log.i(Tag, strMsg.substring(index1+1)); tvRecvCount.setText(strMsg.substring(index1+1)); } if(index2>0) { Log.i(Tag, strMsg.substring(index2+1)); tvRecvCount.setText(strMsg.substring(index1+1)); }  } }        }
到此,整個項目的準系統已經可以實現了,有興趣的朋友可以參照一下。

 

完整的工程(源碼):http://download.csdn.net/detail/zhujinghao09/5313666

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.