仿微信音訊錄入和播放
阿新 • • 發佈:2019-01-11
仿微信音訊錄入和播放
lib中匯入so檔案和lib庫
libs檔案下載地址
連結: https://pan.baidu.com/s/1D1q2PV1IQArK79zAt1cfOw 提取碼: f7jg 複製這段內容後開啟百度網盤手機App,操作更方便哦
頁面主類
package com.example.test_voaacencoder; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Looper; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; import com.example.test_voaacencoder.util.HttpOperateUtil; import com.sinaapp.bashell.VoAACEncoder; public class MainActivity extends Activity { private AudioRecord recordInstance; private boolean isStartRecord; private FileOutputStream fos; private int SAMPLERATE = 8000; private ImageButton imageButton = null; private Button mStartPlayBtn; private Button mStopPlayBtn; private MediaPlayer mMediaPlayer; private String mRecordFileName; String URL_UPLOAD_FILE = "http://192.168.1.147/receive_file.php"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initView imageButton = (ImageButton) findViewById(R.id.imageButtonDial); imageButton.setBackgroundResource(R.drawable.btn_speak_normal); imageButton.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { Log.i("keanbin", "onLongClick()"); setTalkBtnBackground(true); // 停止播放聲音 stopPlay(); // 開始錄音 startRecord(); return true; } }); imageButton.setOnTouchListener(new MyClickListener()); mStartPlayBtn = (Button) findViewById(R.id.btn_mian_startPlay); mStartPlayBtn.setOnClickListener(mOnClickListener); mStopPlayBtn = (Button) findViewById(R.id.btn_mian_stopPlay); mStopPlayBtn.setOnClickListener(mOnClickListener); } private OnClickListener mOnClickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_mian_startPlay: if (isStartRecord) { stopRecord(); } startPlay(); break; case R.id.btn_mian_stopPlay: stopPlay(); break; default: break; } } }; public void startRecord() { if (!Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { Toast.makeText(MainActivity.this, "請插入SD卡!", Toast.LENGTH_SHORT) .show(); return; } try { // mRecordFileName = Environment.getExternalStorageDirectory() // .toString() // + "/testAAC" // + System.currentTimeMillis() // + ".aac"; mRecordFileName = Environment.getExternalStorageDirectory() .toString() + "/testAAC.aac"; fos = new FileOutputStream(mRecordFileName); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("fos = " + fos); new Thread(new Runnable() { @Override public void run() { VoAACEncoder vo = new VoAACEncoder(); vo.Init(SAMPLERATE, 16000, (short) 1, (short) 1);// 取樣率:16000,bitRate:32k,聲道數:1,編碼:0.raw // 1.ADTS int min = AudioRecord.getMinBufferSize(SAMPLERATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); if (min < 2048) { min = 2048; } byte[] tempBuffer = new byte[2048]; recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLERATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, min); recordInstance.startRecording(); isStartRecord = true; while (isStartRecord) { int bufferRead = recordInstance.read(tempBuffer, 0, 2048); byte[] ret = vo.Enc(tempBuffer); if (bufferRead > 0) { System.out.println("ret:" + ret.length); try { fos.write(ret); } catch (IOException e) { e.printStackTrace(); } } } recordInstance.stop(); recordInstance.release(); recordInstance = null; vo.Uninit(); try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } public void stopRecord() { isStartRecord = false; } public void startPlay() { new Thread(new Runnable() { @Override public void run() { Looper.prepare(); // TODO Auto-generated method stub String fileUrl = "http://192.168.1.147/upload/testAAC.aac"; String fileName = HttpOperateUtil.downLoadFile(fileUrl, fileUrl.substring(fileUrl.lastIndexOf("/") + 1)); Log.i("keanbin", "fileName = " + fileName); File file = new File(fileName); if (!file.exists()) { Toast.makeText(MainActivity.this, "沒有音樂檔案!", Toast.LENGTH_SHORT) .show(); return; } mMediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(fileName)); mMediaPlayer.setLooping(false); mMediaPlayer.start(); Looper.loop(); } }).start(); } public void stopPlay() { if (mMediaPlayer != null) { mMediaPlayer.stop(); } } @Override protected void onDestroy() { stopPlay(); stopRecord(); super.onDestroy(); } class MyClickListener implements OnTouchListener { public boolean onTouch(View v, MotionEvent event) { Log.i("keanbin", "event.getAction() = " + event.getAction()); switch (event.getAction()) { case MotionEvent.ACTION_UP: setTalkBtnBackground(false); // TODO 正常放開,接下來一般做以下事情:傳送錄音檔案到伺服器 if (isStartRecord) { // 停止錄音 stopRecord(); Toast.makeText(MainActivity.this, "開始上傳檔案!", Toast.LENGTH_SHORT).show(); // 上傳檔案 HttpOperateUtil .uploadFile(URL_UPLOAD_FILE, mRecordFileName); Toast.makeText(MainActivity.this, "上傳完檔案!", Toast.LENGTH_SHORT).show(); } break; case MotionEvent.ACTION_CANCEL: setTalkBtnBackground(false); // TODO 異常放開,接下來一般做以下事情:刪除錄音檔案 // 停止錄音 stopRecord(); break; default: break; } return false; } } public void setTalkBtnBackground(boolean isTalk) { if (isTalk) { imageButton.setBackgroundResource(R.drawable.btn_speak_pressed); } else { imageButton.setBackgroundResource(R.drawable.btn_speak_normal); } } }
Utli類 上傳音訊檔案和下載音訊檔案
package com.example.test_voaacencoder.util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.os.Environment; import android.util.Log; public class HttpOperateUtil { public static boolean uploadFile(String uploadUrl, String srcPath) { String end = "\r\n"; String twoHyphens = "--"; String boundary = "******"; try { URL url = new URL(uploadUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); // 設定每次傳輸的流大小,可以有效防止手機因為記憶體不足崩潰 // 此方法用於在預先不知道內容長度時啟用,沒有進行內部緩衝的 HTTP 請求正文的流。 httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K // 允許輸入輸出流 httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); // 使用POST方法 httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream( httpURLConnection.getOutputStream()); dos.writeBytes(twoHyphens + boundary + end); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end); dos.writeBytes(end); // 上傳檔案內容 FileInputStream fis = new FileInputStream(srcPath); byte[] buffer = new byte[8192]; // 8k int count = 0; // 讀取檔案 while ((count = fis.read(buffer)) != -1) { dos.write(buffer, 0, count); } fis.close(); dos.writeBytes(end); dos.writeBytes(twoHyphens + boundary + twoHyphens + end); dos.flush(); // 伺服器返回結果 InputStream is = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String result = br.readLine(); // Toast.makeText(this, result, Toast.LENGTH_LONG).show(); Log.i("uploadFile", "uploadFile result = " + result); try { dos.close(); } catch (Exception e) { e.printStackTrace(); // setTitle(e.getMessage()); } is.close(); return true; } catch (Exception e) { e.printStackTrace(); // setTitle(e.getMessage()); } return false; } public static String downLoadFile(String fileUrl, String fileName) { String fileDir = ""; try { // 判斷SD卡是否存在,並且是否具有讀寫許可權 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 獲得儲存卡的路徑 String sdpath = Environment.getExternalStorageDirectory() + "/"; String savePath = sdpath + "download"; URL url = new URL(fileUrl); // 建立連線 HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.connect(); // 獲取檔案大小 int contentLength = conn.getContentLength(); // 建立輸入流 InputStream is = conn.getInputStream(); File file = new File(savePath); // 判斷檔案目錄是否存在 if (!file.exists()) { file.mkdir(); } File apkFile = new File(savePath, fileName); FileOutputStream fos = new FileOutputStream(apkFile); int count = 0; // 快取 byte buf[] = new byte[1024]; // 寫入到檔案中 do { int numread = is.read(buf); // count += numread; // // 計算進度條位置 // mPercent = (int) (((float) count / contentLength) * 100); // // 更新進度 // mHandler.sendEmptyMessage(DOWNLOAD_PERCENT); if (numread <= 0) { // 下載完成 fileDir = savePath + "/" + fileName; // if (checkUpdateResult(apkFile)) { // // 下載成功 // // } else { // // 下載失敗 // fos.close(); // is.close(); // // throw new RuntimeException("下載檔案出錯"); // } break; } // 寫入檔案 fos.write(buf, 0, numread); } while (true);// !mCancelUpdate);// 點選取消就停止下載. fos.close(); is.close(); } } catch (IOException e) { e.printStackTrace(); } return fileDir; } }
main.xml 檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="仿照米聊和微信中語音通話時按住按鈕是一個狀態和鬆開手是一個狀態的效果,加入防止使用者手jian狂按按鈕導致的異常" /> <Button android:id="@+id/btn_mian_startPlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="播放錄音" /> <Button android:id="@+id/btn_mian_stopPlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止播放" /> <ImageButton android:id="@+id/imageButtonDial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="20dip" android:layout_marginTop="20dip" /> </LinearLayout>
錄音按鈕圖片
在這裡插入圖片描述