国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android音頻錄制MediaRecorder之簡易的錄音軟件實現代碼

2020-04-11 11:55:49
字體:
來源:轉載
供稿:網友

使用MediaRecorder的步驟:
1、創建MediaRecorder對象
2、調用MediRecorder對象的setAudioSource()方法設置聲音的來源,一般傳入MediaRecorder.MIC
3、調用MediaRecorder對象的setOutputFormat()設置所錄制的音頻文件的格式
4、調用MediaRecorder對象的setAudioRncoder()、setAudioEncodingBitRate(int bitRate)、setAudioSamlingRate(int SamplingRate)設置所錄音的編碼格式、編碼位率、采樣率等,
5、調用MediaRecorder對象的setOutputFile(String path)方法設置錄制的音頻文件的保存位置
6、調用MediaRecoder對象的Prepare()方法準備錄制
7、調用MediaRecoder對象的start()方法開始錄制
8、調用MediaRecoder對象的stop()方法停止錄制,并調用release()方法釋放資源

實例:

復制代碼 代碼如下:

    <uses-permission  android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/>
    <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission  android:name="android.permission.RECORD_AUDIO"/>

復制代碼 代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   
    <LinearLayout
        android:id="@+id/li1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/start"/>
         <Button android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/stop"/>
    </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_below="@id/li1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</RelativeLayout>

復制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/show_file_name" />

    <Button
        android:id="@+id/bt_list_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play"/>
    <Button  android:id="@+id/bt_list_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/list_stop"/>

</LinearLayout>

復制代碼 代碼如下:

package com.android.xiong.mediarecordertest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

 private Button start;
 private Button stop;
 private ListView listView;
 // 錄音文件播放
 private MediaPlayer myPlayer;
 // 錄音
 private MediaRecorder myRecorder;
 // 音頻文件保存地址
 private String path;
 private String paths = path;
 private File saveFilePath;
 // 所錄音的文件
 String[] listFile = null;

 ShowRecorderAdpter showRecord;
 AlertDialog aler = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  start = (Button) findViewById(R.id.start);
  stop = (Button) findViewById(R.id.stop);
  listView = (ListView) findViewById(R.id.list);
  myPlayer = new MediaPlayer();
  myRecorder = new MediaRecorder();
  // 從麥克風源進行錄音
  myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  // 設置輸出格式
  myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
  // 設置編碼格式
  myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  showRecord = new ShowRecorderAdpter();
  if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {
   try {
    path = Environment.getExternalStorageDirectory()
      .getCanonicalPath().toString()
      + "/XIONGRECORDERS";
    File files = new File(path);
    if (!files.exists()) {
     files.mkdir();
    }
    listFile = files.list();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

  start.setOnClickListener(this);
  stop.setOnClickListener(this);
  if (listFile != null) {
   listView.setAdapter(showRecord);
  }

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 class ShowRecorderAdpter extends BaseAdapter {

  @Override
  public int getCount() {
   return listFile.length;
  }

  @Override
  public Object getItem(int arg0) {
   return arg0;
  }

  @Override
  public long getItemId(int arg0) {
   return arg0;

  }

  @Override
  public View getView(final int postion, View arg1, ViewGroup arg2) {
   View views = LayoutInflater.from(MainActivity.this).inflate(
     R.layout.list_show_filerecorder, null);
   TextView filename = (TextView) views
     .findViewById(R.id.show_file_name);
   Button plays = (Button) views.findViewById(R.id.bt_list_play);
   Button stop = (Button) views.findViewById(R.id.bt_list_stop);

   filename.setText(listFile[postion]);
   // 播放錄音
   plays.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
     try {
      myPlayer.reset();
      myPlayer.setDataSource(path + "/" + listFile[postion]);
      if (!myPlayer.isPlaying()) {

       myPlayer.prepare();
       myPlayer.start();
      } else {
       myPlayer.pause();
      }

     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   });
   // 停止播放
   stop.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
     if (myPlayer.isPlaying()) {
      myPlayer.stop();
     }
    }
   });
   return views;
  }

 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.start:
   final EditText filename = new EditText(this);
   Builder alerBuidler = new Builder(this);
   alerBuidler
     .setTitle("請輸入要保存的文件名")
     .setView(filename)
     .setPositiveButton("確定",
       new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog,
          int which) {
         String text = filename.getText().toString();
         try {
          paths = path
            + "/"
            + text
            + new SimpleDateFormat(
              "yyyyMMddHHmmss").format(System
              .currentTimeMillis())
            + ".amr";
          saveFilePath = new File(paths);
          myRecorder.setOutputFile(saveFilePath
            .getAbsolutePath());
          saveFilePath.createNewFile();
          myRecorder.prepare();
          // 開始錄音
          myRecorder.start();
          start.setText("正在錄音中。。");
          start.setEnabled(false);
          aler.dismiss();
          // 重新讀取 文件
          File files = new File(path);
          listFile = files.list();
          // 刷新ListView
          showRecord.notifyDataSetChanged();
         } catch (Exception e) {
          e.printStackTrace();
         }

        }
       });
   aler = alerBuidler.create();
   aler.setCanceledOnTouchOutside(false);
   aler.show();
   break;
  case R.id.stop:
   if (saveFilePath.exists() && saveFilePath != null) {
    myRecorder.stop();
    myRecorder.release();
    // 判斷是否保存 如果不保存則刪除
    new AlertDialog.Builder(this)
      .setTitle("是否保存該錄音")
      .setPositiveButton("確定", null)
      .setNegativeButton("取消",
        new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog,
           int which) {
          saveFilePath.delete();
          // 重新讀取 文件
          File files = new File(path);
          listFile = files.list();
          // 刷新ListView
          showRecord.notifyDataSetChanged();
         }
        }).show();

   }
   start.setText("錄音");
   start.setEnabled(true);
  default:
   break;
  }

 }

 @Override
 protected void onDestroy() {
  // 釋放資源
  if (myPlayer.isPlaying()) {
   myPlayer.stop();
   myPlayer.release();
  }
  myPlayer.release();
  myRecorder.release();
  super.onDestroy();
 }

}

源碼下載:http://xiazai.VeVB.COm/201401/yuanma/MediaRecorderTest(VeVB.COm).rar

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 攀枝花市| 商水县| 大城县| 寻乌县| 大渡口区| 策勒县| 桓台县| 什邡市| 大宁县| 古蔺县| 澜沧| 乌苏市| 宁海县| 达孜县| 汾阳市| 汶川县| 武宣县| 上犹县| 汝州市| 理塘县| 民权县| 星子县| 承德市| 新宁县| 宜兰县| 文水县| 赤峰市| 靖州| 疏勒县| 乐业县| 南郑县| 古浪县| 德格县| 武乡县| 周口市| 兰考县| 龙州县| 安徽省| 牡丹江市| 萨嘎县|