最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android音頻錄制MediaRecorder之簡易的錄音軟件實(shí)現(xiàn)代碼

 更新時(shí)間:2014年01月24日 15:11:50   作者:  
這篇文章主要介紹了Android音頻錄制MediaRecorder之簡易的錄音軟件實(shí)現(xiàn)代碼,有需要的朋友可以參考一下

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

實(shí)例:

復(fù)制代碼 代碼如下:

    <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"/>

復(fù)制代碼 代碼如下:

<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>

復(fù)制代碼 代碼如下:

<?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>

復(fù)制代碼 代碼如下:

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();
  // 從麥克風(fēng)源進(jìn)行錄音
  myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  // 設(shè)置輸出格式
  myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
  // 設(shè)置編碼格式
  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.jb51.net/201401/yuanma/MediaRecorderTest(jb51.net).rar

相關(guān)文章

  • Android實(shí)現(xiàn)靜默安裝的兩種方法

    Android實(shí)現(xiàn)靜默安裝的兩種方法

    這篇文章主要介紹了Android實(shí)現(xiàn)靜默安裝的兩種方法,一種是利用root權(quán)限靜默安裝實(shí)現(xiàn),另一種是非root權(quán)限提示用戶安裝,兩種方法分別給出了示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • WebView設(shè)置WebViewClient的方法

    WebView設(shè)置WebViewClient的方法

    這篇文章主要介紹了 WebView設(shè)置WebViewClient的方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • android仿支付寶密碼輸入框效果

    android仿支付寶密碼輸入框效果

    這篇文章主要為大家詳細(xì)介紹了android仿支付寶密碼輸入框效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android Activity啟動模式之singleTask實(shí)例詳解

    Android Activity啟動模式之singleTask實(shí)例詳解

    這篇文章主要介紹了Android Activity啟動模式之singleTask,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-01-01
  • Android自定義組件ListPopWindow

    Android自定義組件ListPopWindow

    這篇文章主要介紹了Android自定義組件ListPopWindow的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android學(xué)習(xí)筆記(一)環(huán)境安裝及第一個(gè)hello world

    Android學(xué)習(xí)筆記(一)環(huán)境安裝及第一個(gè)hello world

    最近在學(xué)習(xí)安卓開發(fā),記錄下環(huán)境安裝和第一個(gè)hello world的誕生過程,希望對大家有所幫助
    2014-07-07
  • Android Mms之:聯(lián)系人管理的應(yīng)用分析

    Android Mms之:聯(lián)系人管理的應(yīng)用分析

    本篇文章是對Android中的聯(lián)系人管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android 5.0以上Toast不顯示的解決方法

    Android 5.0以上Toast不顯示的解決方法

    最近在開發(fā)中我們經(jīng)常會在適配5.0以后的機(jī)型遇到各種各樣的問題,其中有一個(gè)不大不小的問題就是:Toast不顯示問題,這篇文章就給大家總結(jié)了Android 5.0以上Toast不顯示的原因與解決方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • Android中Intent習(xí)慣用法

    Android中Intent習(xí)慣用法

    這篇文章主要介紹了Android中Intent習(xí)慣用法,演示了如何在Android中利用Intent進(jìn)行拍照、攝像、打電話、發(fā)短信、發(fā)郵件等,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android RecyclerView 滾動到中間位置的方法示例

    Android RecyclerView 滾動到中間位置的方法示例

    這篇文章主要介紹了Android RecyclerView 滾動到中間位置的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03

最新評論

渭源县| 天气| 越西县| 巨鹿县| 定结县| 分宜县| 梓潼县| 乌拉特中旗| 南阳市| 福州市| 邳州市| 七台河市| 五大连池市| 杭锦旗| 阳新县| 讷河市| 阿尔山市| 阳朔县| 崇明县| 怀柔区| 宁乡县| 定安县| 南京市| 什邡市| 陆良县| 陆川县| 华蓥市| 甘洛县| 南乐县| 图片| 微博| 读书| 太原市| 阿克苏市| 鄂伦春自治旗| 安义县| 四子王旗| 海宁市| 诸城市| 垦利县| 新源县|