Android Media Recorder錄音播放原始碼

來源:互聯網
上載者:User

  View Code
 
 1 package irdc.ex07_11;
 2
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6
 7 import android.app.Activity;
 8 import android.content.Intent;
 9 import android.media.MediaRecorder;
10 import android.net.Uri;
11 import android.os.Bundle;
12 import android.os.Environment;
13 import android.view.View;
14 import android.widget.AdapterView;
15 import android.widget.ArrayAdapter;
16 import android.widget.CheckedTextView;
17 import android.widget.ImageButton;
18 import android.widget.ListView;
19 import android.widget.TextView;
20 import android.widget.Toast;
21
22 public class EX07_11 extends Activity
23 {
24   private ImageButton myButton1;
25   private ImageButton myButton2;
26   private ImageButton myButton3;
27   private ImageButton myButton4;
28
29   private ListView myListView1;
30   private String strTempFile = "ex07_11_";
31   private File myRecAudioFile;
32   private File myRecAudioDir;// 得到Sd卡path
33   private File myPlayFile;
34   private MediaRecorder mMediaRecorder01;
35
36   private ArrayList<String> recordFiles;
37   private ArrayAdapter<String> adapter;// 用於ListView的適配器
38   private TextView myTextView1;
39   private boolean sdCardExit;
40   private boolean isStopRecord;
41
42   /** Called when the activity is first created. */
43   @Override
44   public void onCreate(Bundle savedInstanceState)
45   {
46     super.onCreate(savedInstanceState);
47     setContentView(R.layout.main);
48     //主要是4個控制按鈕(錄製,停止,播放,刪除)
49     myButton1 = (ImageButton) findViewById(R.id.ImageButton01);
50     myButton2 = (ImageButton) findViewById(R.id.ImageButton02);
51     myButton3 = (ImageButton) findViewById(R.id.ImageButton03);
52     myButton4 = (ImageButton) findViewById(R.id.ImageButton04);
53     //列表出指定檔案夾中所有amr格式音頻檔案
54     myListView1 = (ListView) findViewById(R.id.ListView01);
55     myTextView1 = (TextView) findViewById(R.id.TextView01);
56
57     myButton2.setEnabled(false);
58     myButton3.setEnabled(false);
59     myButton4.setEnabled(false);
 
布局檔案main.xml
  View Code
 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="fill_parent"
 4     android:layout_height="fill_parent" android:background="@drawable/white">
 5     <LinearLayout android:id="@+id/LinearLayout01"
 6         android:layout_width="wrap_content" android:layout_height="wrap_content">
 7         <ImageButton android:id="@+id/ImageButton01"
 8             android:layout_width="wrap_content" android:layout_height="wrap_content"
 9             android:src="@drawable/record">
10         </ImageButton>
11         <ImageButton android:id="@+id/ImageButton02"
12             android:layout_width="wrap_content" android:layout_height="wrap_content"
13             android:src="@drawable/stop">
14         </ImageButton>
15         <ImageButton android:id="@+id/ImageButton03"
16             android:layout_width="wrap_content" android:layout_height="wrap_content"
17             android:src="@drawable/play">
18         </ImageButton>
19         <ImageButton android:id="@+id/ImageButton04"
20             android:layout_width="wrap_content" android:layout_height="wrap_content"
21             android:src="@drawable/delete">
22         </ImageButton>
23     </LinearLayout>
24     <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
25         android:layout_height="wrap_content" android:textColor="@drawable/black">
26     </TextView>
27     <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
28         android:layout_height="wrap_content" android:background="@drawable/black">
29     </ListView>
30 </LinearLayout>
 
my_simple_list_item.xml檔案
  View Code
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" />

 

摘自 myphoebe

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.