Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 錄音與播放功能的簡單實例

Android 錄音與播放功能的簡單實例

編輯:關於Android編程

 Android 錄音與播放功能的簡單實例

最近在研究Android中一些常用的功能,像地圖、拍照、錄音和播放的實現等等,還有一些側滑、動畫等是如何實現的。
今天就把錄音和播放的實現分享一下,錄音和播放比較簡單,利用android內部的類即可實現。

1、先看下運行後的界面:

以下三張圖分別是進入、錄音、播放時的。

 

2、Layout布局文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" > 
 
  <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/switcherbar_bg" 
    android:gravity="center" 
    android:text="@string/audio_record_title" 
    android:textColor="#ffffff" 
    android:textSize="16sp" /> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:orientation="vertical" 
    android:paddingLeft="10dip" 
    android:paddingRight="10dip" > 
 
    <TextView 
      android:id="@+id/audio_record_time" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="00:00" /> 
 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dip" 
      android:orientation="horizontal" > 
 
      <Button 
        android:id="@+id/audio_record_start" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/searchbtn_normal" 
        android:text="開始錄音" 
        android:textSize="14sp" /> 
 
      <Button 
        android:id="@+id/audio_record_stop" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dip" 
        android:layout_weight="1" 
        android:background="@drawable/searchbtn_bg" 
        android:text="結束錄音" 
        android:textSize="14sp" /> 
    </LinearLayout> 
 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dip" 
      android:orientation="horizontal" > 
 
      <Button 
        android:id="@+id/audio_record_play" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/searchbtn_normal" 
        android:text="播放錄音" 
        android:textSize="14sp" /> 
 
      <Button 
        android:id="@+id/audio_record_select" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dip" 
        android:layout_weight="1" 
        android:background="@drawable/searchbtn_bg" 
        android:text="確定選擇" 
        android:textSize="14sp" /> 
    </LinearLayout> 
  </LinearLayout> 
 
</RelativeLayout> 

3 Activity類

錄音涉及到二個Activity,第一個Activity比較簡單,我這裡大概說下,其實就是有個按鈕,點擊後轉移第二個Activity,錄音返回後,在第一個Activity中獲取錄音的文件名、時長等。

第一個Activity部分代碼:

 // 錄音事件 
  ksly_btn.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      Intent intent = new Intent(MaintainVisitEditActivity.this, AudioRecordActivity.class); 
      intent.setAction(Intent.ACTION_VIEW); 
      intent.putExtra("duration", entity.getVoiceDuration()); 
      intent.putExtra("fileName", entity.getVoiceRecord()); 
      startActivityForResult(intent, VOICE_RECODE); 
    } 
  }); 
 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
  super.onActivityResult(requestCode, resultCode, data); 
 
  if (requestCode == VOICE_RECODE && resultCode == AudioRecordActivity.SUCCESS) { 
    entity.setVoiceDuration(data.getLongExtra("duration", 0));// 時長 
    entity.setVoiceRecord(data.getStringExtra("fileName"));// 文件名(絕對路徑) 
    ksly_time.setText(DateTimeUtils.formatToMillisecond(entity.getVoiceDuration())); 
  } 
} 

第二個Activity代碼:

這裡要注意一下,就是需要捕獲返回鍵,處理一下,就是點擊返回鍵時,也返回個狀態碼,以表示沒有錄音成功。

package com.whowii.ct.cm.activity; 
 
import java.io.File; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Timer; 
import java.util.TimerTask; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnCompletionListener; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import com.whowii.ct.cm.R; 
import com.whowii.ct.cm.command.QueryParams; 
import com.whowii.ct.cm.utils.DateTimeUtils; 
import com.whowii.ct.cm.utils.SDCardUtils; 
 
/** 
 * 錄制音頻 
 * 
 * @author Administrator 
 * 
 */ 
public class AudioRecordActivity extends Activity { 
  private TextView audio_record_time; 
  private Button audio_record_start, audio_record_stop, audio_record_play, audio_record_select; 
  private MediaRecorder mediaRecorder; 
  private final String TAG = AudioRecordActivity.class.getSimpleName(); 
  private boolean isIdle = true;// 當前是否空閒,false:表示正在錄音 
  private long startTime = 0, stopTime = 0, duration = 0;// 開始時間、結束時間、錄音時長 
  private String fileName = null;// 存儲錄音文件的路徑 
  private Timer timer = null;// Timer計時器 
  public static final int SUCCESS = 1;// 錄制成功; 
  public static final int FAILURE = 0;// 錄制失敗 
  private MediaPlayer mediaPlayer; 
  private TimerTask task = new TimerTask() { 
    final Handler handler = new Handler() { 
      public void handleMessage(Message message) { 
        Bundle data = message.getData(); 
        audio_record_time.setText(DateTimeUtils.formatToMillisecond(data.getLong("time"))); 
      } 
    }; 
 
    public void run() { 
      Message message = new Message(); 
      long t = System.currentTimeMillis(); 
      Bundle data = new Bundle(); 
      data.putLong("time", t - startTime); 
      message.setData(data); 
      handler.sendMessage(message); 
    } 
  }; 
 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); // 設置無標題欄 
 
    setContentView(R.layout.audio_record); 
    mediaPlayer = new MediaPlayer(); 
 
    initControl(); 
    setListener(); 
 
    timer = new Timer(true); 
    fileName = getIntent().getStringExtra("fileName"); 
    duration = getIntent().getLongExtra("duration", 0); 
  } 
 
  private void initControl() { 
    audio_record_time = (TextView) findViewById(R.id.audio_record_time); 
    audio_record_start = (Button) findViewById(R.id.audio_record_start); 
    audio_record_stop = (Button) findViewById(R.id.audio_record_stop); 
    audio_record_play = (Button) findViewById(R.id.audio_record_play); 
    audio_record_select = (Button) findViewById(R.id.audio_record_select); 
  } 
 
  private void setListener() { 
    // 播放完成事件 
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
        isIdle = true; 
        audio_record_play.setText("播放錄音"); 
        audio_record_play.setBackgroundResource(R.drawable.searchinput_bg); 
      } 
    }); 
    // 開始錄音 
    audio_record_start.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        if (!isIdle) { 
          return; 
        } 
        if (!SDCardUtils.sdCardExists()) { 
          Toast.makeText(AudioRecordActivity.this, "缺少SD卡,請先插入後再操作!", Toast.LENGTH_LONG).show(); 
          return; 
        } 
        audio_record_start.setText("開始錄音"); 
        audio_record_start.setEnabled(true); 
 
        duration = 0; 
        startTime = System.currentTimeMillis(); 
        fileName = QueryParams.CACHE_AUDIO_PATH; 
        fileName += new SimpleDateFormat("yyyyMMdd_hhmmss").format(new Date(startTime)) + ".amr"; 
        File file = new File(fileName); 
 
        mediaRecorder = new MediaRecorder(); 
        mediaRecorder.setOutputFile(file.getAbsolutePath()); 
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
        try { 
          mediaRecorder.prepare(); 
          mediaRecorder.start(); 
          isIdle = false; 
          audio_record_start.setBackgroundResource(R.drawable.searchbtn_pressed); 
          timer.schedule(task, 0, 100); 
        } catch (IOException e) { 
          startTime = 0; 
          Log.e(TAG, e.toString()); 
          Toast.makeText(AudioRecordActivity.this, "錄制時發生異常!", Toast.LENGTH_LONG).show(); 
        } 
      } 
    }); 
    // 結束錄音 
    audio_record_stop.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        if (mediaRecorder != null) { 
          stopTime = System.currentTimeMillis(); 
          duration = stopTime - startTime; 
          timer.cancel(); 
          mediaRecorder.stop(); 
          mediaRecorder.release(); 
          mediaRecorder = null; 
          audio_record_start.setBackgroundResource(R.drawable.searchbtn_normal); 
          isIdle = true; 
        } 
      } 
    }); 
    // 播放錄音 
    audio_record_play.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        if (!isIdle) { 
          return; 
        } 
 
        if (audio_record_play.getText().equals("播放錄音")) { 
          if (fileName == null || fileName.equals("") || duration == 0) { 
            Toast.makeText(AudioRecordActivity.this, "沒有錄音文件!", Toast.LENGTH_LONG).show(); 
            return; 
          } 
 
          try { 
            mediaPlayer.reset(); 
            mediaPlayer.setDataSource(fileName); 
            mediaPlayer.prepare(); 
            mediaPlayer.start(); 
            isIdle = false; 
            audio_record_play.setText("終止播放"); 
            audio_record_play.setBackgroundResource(R.drawable.searchbtn_pressed); 
          } catch (Exception e) { 
            e.printStackTrace(); 
            Toast.makeText(AudioRecordActivity.this, "播放錄音時遇到錯誤!", Toast.LENGTH_LONG).show(); 
          } 
        } else { 
          if (mediaPlayer != null && mediaPlayer.isPlaying()) { 
            mediaPlayer.stop(); 
            isIdle = true; 
          } 
          audio_record_play.setText("播放錄音"); 
          audio_record_play.setBackgroundResource(R.drawable.searchinput_bg); 
        } 
      } 
    }); 
    // 確認選擇 
    audio_record_select.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        if (fileName == null || fileName.equals("") || duration == 0) { 
          Toast.makeText(AudioRecordActivity.this, "沒有錄音文件!", Toast.LENGTH_LONG).show(); 
          return; 
        } 
 
        Intent intent = new Intent(); 
        intent.putExtra("fileName", fileName); 
        intent.putExtra("duration", duration); 
        setResult(SUCCESS, intent);// 返回成功標識 
        isIdle = true; 
        if (mediaPlayer != null) { 
          if (mediaPlayer.isPlaying()) { 
            mediaPlayer.stop(); 
          } 
          mediaPlayer = null; 
        } 
        finish();// 結束當前的activity,等於點擊返回按鈕 
      } 
    }); 
  } 
 
  // 捕獲返回鍵,關閉當前頁面時返回失敗標識 
  @Override 
  public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
      setResult(FAILURE); 
      isIdle = true; 
      if (mediaPlayer != null) { 
        if (mediaPlayer.isPlaying()) { 
          mediaPlayer.stop(); 
        } 
        mediaPlayer = null; 
      } 
      finish(); 
      return true; 
    } 
    return super.onKeyDown(keyCode, event); 
  } 
 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

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