Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> 安卓開發中Media Recorder錄音播放代碼

安卓開發中Media Recorder錄音播放代碼

編輯:Android開發實例

View Code
 package irdc.ex07_11;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 
 import android.app.Activity;
 import android.content.Intent;
 import android.media.MediaRecorder;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Environment;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.CheckedTextView;
 import android.widget.ImageButton;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;
 
 public class EX07_11 extends Activity
 {
   private ImageButton myButton1;
   private ImageButton myButton2;
   private ImageButton myButton3;
   private ImageButton myButton4;
 
   private ListView myListView1;
   private String strTempFile = "ex07_11_";
   private File myRecAudioFile;
   private File myRecAudioDir;// 得到Sd卡path
   private File myPlayFile;
   private MediaRecorder mMediaRecorder01;
 
   private ArrayList<String> recordFiles;
   private ArrayAdapter<String> adapter;// 用於ListView的適配器
   private TextView myTextView1;
   private boolean sdCardExit;
   private boolean isStopRecord;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     //主要是4個控制按鈕(錄制,停止,播放,刪除)
     myButton1 = (ImageButton) findViewById(R.id.ImageButton01);
     myButton2 = (ImageButton) findViewById(R.id.ImageButton02);
     myButton3 = (ImageButton) findViewById(R.id.ImageButton03);
     myButton4 = (ImageButton) findViewById(R.id.ImageButton04);
     //列表出指定文件夾中所有amr格式音頻文件
     myListView1 = (ListView) findViewById(R.id.ListView01);
     myTextView1 = (TextView) findViewById(R.id.TextView01);
 
     myButton2.setEnabled(false);
     myButton3.setEnabled(false);
     myButton4.setEnabled(false);

  布局文件main.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="fill_parent"
     android:layout_height="fill_parent" android:background="@drawable/white">
     <LinearLayout android:id="@+id/LinearLayout01"
         android:layout_width="wrap_content" android:layout_height="wrap_content">
         <ImageButton android:id="@+id/ImageButton01"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:src="@drawable/record">
         </ImageButton>
         <ImageButton android:id="@+id/ImageButton02"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:src="@drawable/stop">
         </ImageButton>
         <ImageButton android:id="@+id/ImageButton03"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:src="@drawable/play">
         </ImageButton>
         <ImageButton android:id="@+id/ImageButton04"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:src="@drawable/delete">
         </ImageButton>
     </LinearLayout>
     <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
         android:layout_height="wrap_content" android:textColor="@drawable/black">
     </TextView>
     <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
         android:layout_height="wrap_content" android:background="@drawable/black">
     </ListView>
 </LinearLayout>

  my_simple_list_item.xml文件:

  1 <?xml version="1.0" encoding="utf-8"?>

  2 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"

  3 android:id="@+id/myCheckedTextView1" android:layout_width="fill_parent"

  4 android:layout_height="fill_parent" android:textColor="@drawable/white" />

  權限設置

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved