欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)錄音及保存播放功能的方法【附demo源碼下載】

 更新時(shí)間:2018年01月29日 13:57:58   作者:總有刁民想愛朕  
這篇文章主要介紹了Android編程實(shí)現(xiàn)錄音及保存播放功能的方法,結(jié)合實(shí)例形式分析了Android基于MediaRecorder類進(jìn)行錄音機(jī)保存播放功能的相關(guān)操作技巧,并附帶demo源碼供讀者下載,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)錄音及保存播放功能的方法。分享給大家供大家參考,具體如下:

在android中進(jìn)行錄音相對(duì)來說是比較簡(jiǎn)單的,使用系統(tǒng)提供的MediaRecorder類進(jìn)行錄音并保存,然后調(diào)用MediaPlayer進(jìn)行播放。以下為xml配置文件代碼:

<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="com.example.kk.soundrecording.MainActivity" >
  <Button
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/start" />
  <Button
    android:id="@+id/stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/start"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/stop" />
  <Button
    android:id="@+id/paly"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/stop"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/paly" />
  <Button
    android:id="@+id/pause_paly"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/paly"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/pause_paly" />
  <Button
    android:id="@+id/stop_paly"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/pause_paly"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/stop_paly" />
</RelativeLayout>

在MainActivity中進(jìn)行錄音,代碼如下:

package com.example.kk.soundrecording;
import java.io.File;
import java.io.IOException;
import com.example.kk.util.RecordPlayer;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 *
 * @author kk
 *
 */
public class MainActivity extends Activity implements OnClickListener {
  // 開始錄音
  private Button start;
  // 停止按鈕
  private Button stop;
  // 播放按鈕
  private Button paly;
  // 暫停播放
  private Button pause_paly;
  // 停止播放
  private Button stop_paly;
  // 錄音類
  private MediaRecorder mediaRecorder;
  // 以文件的形式保存
  private File recordFile;
  private RecordPlayer player;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recordFile = new File("/mnt/sdcard", "kk.amr");
    initView();
    Listener();
  }
  private void initView() {
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    paly = (Button) findViewById(R.id.paly);
    pause_paly = (Button) findViewById(R.id.pause_paly);
    stop_paly = (Button) findViewById(R.id.stop_paly);
  }
  private void Listener() {
    start.setOnClickListener(this);
    stop.setOnClickListener(this);
    paly.setOnClickListener(this);
    pause_paly.setOnClickListener(this);
    stop_paly.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    player = new RecordPlayer(MainActivity.this);
    int Id = v.getId();
    switch (Id) {
    case R.id.start:
      startRecording();
      break;
    case R.id.stop:
      stopRecording();
      break;
    case R.id.paly:
      playRecording();
      break;
    case R.id.pause_paly:
      pauseplayer();
      break;
    case R.id.stop_paly:
      stopplayer();
      break;
    }
  }
  private void startRecording() {
    mediaRecorder = new MediaRecorder();
    // 判斷,若當(dāng)前文件已存在,則刪除
    if (recordFile.exists()) {
      recordFile.delete();
    }
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mediaRecorder.setOutputFile(recordFile.getAbsolutePath());
    try {
      // 準(zhǔn)備好開始錄音
      mediaRecorder.prepare();
      mediaRecorder.start();
    } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private void stopRecording() {
    if (recordFile != null) {
      mediaRecorder.stop();
      mediaRecorder.release();
    }
  }
  private void playRecording() {
    player.playRecordFile(recordFile);
  }
  private void pauseplayer() {
    player.pausePalyer();
  }
  private void stopplayer() {
    // TODO Auto-generated method stub
    player.stopPalyer();
  }
}

同時(shí),新建一個(gè)RecordPlayer類,用來播放保存好的錄音,如下:

package com.example.kk.util;
import java.io.File;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import com.example.kk.soundrecording.R;
/**
 *
 *
 * @author kk  錄音播放類
 *
 */
public class RecordPlayer {
  private static MediaPlayer mediaPlayer;
  private Context mcontext;
  public RecordPlayer(Context context) {
    this.mcontext = context;
  }
  // 播放錄音文件
  public void playRecordFile(File file) {
    if (file.exists() && file != null) {
      if (mediaPlayer == null) {
        Uri uri = Uri.fromFile(file);
        mediaPlayer = MediaPlayer.create(mcontext, uri);
      }
      mediaPlayer.start();
      //監(jiān)聽MediaPlayer播放完成
      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer paramMediaPlayer) {
          // TODO Auto-generated method stub
          //彈窗提示
          Toast.makeText(mcontext,
              mcontext.getResources().getString(R.string.ok),
              Toast.LENGTH_SHORT).show();
        }
      });
    }
  }
  // 暫停播放錄音
  public void pausePalyer() {
    if (mediaPlayer.isPlaying()) {
      mediaPlayer.pause();
      Log.e("TAG", "暫停播放");
    }
  }
  // 停止播放錄音
  public void stopPalyer() {
    // 這里不調(diào)用stop(),調(diào)用seekto(0),把播放進(jìn)度還原到最開始
    if (mediaPlayer.isPlaying()) {
      mediaPlayer.pause();
      mediaPlayer.seekTo(0);
      Log.e("TAG", "停止播放");
    }
  }
}

此時(shí),功能代碼都已實(shí)現(xiàn),但是運(yùn)行時(shí)會(huì)報(bào)錯(cuò)!為什么呢,這個(gè)是被很多初學(xué)者會(huì)忘記的,那就是android開發(fā)中調(diào)用相應(yīng)的功能時(shí),必須在主配置文件中授予相應(yīng)的權(quán)限,在配置文件中添加如下代碼:

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

另外,Android權(quán)限控制的詳細(xì)說明可以參考Android Manifest功能與權(quán)限描述大全

附:Demo源碼點(diǎn)擊此處本站下載。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論