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

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

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

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

在android中進(jì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();
  }
}

同時,新建一個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", "停止播放");
    }
  }
}

此時,功能代碼都已實現(xiàn),但是運行時會報錯!為什么呢,這個是被很多初學(xué)者會忘記的,那就是android開發(fā)中調(diào)用相應(yīng)的功能時,必須在主配置文件中授予相應(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源碼點擊此處本站下載

更多關(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é)

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

相關(guān)文章

  • 基于android實現(xiàn)五子棋開發(fā)

    基于android實現(xiàn)五子棋開發(fā)

    這篇文章主要為大家詳細(xì)介紹了基于android實現(xiàn)五子棋開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Android入門之Handler的使用教程詳解

    Android入門之Handler的使用教程詳解

    這篇文章主要為大家詳細(xì)介紹了Android中Handler機(jī)制的使用,文中的示例代碼講解詳細(xì),有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-11-11
  • Android操作Html打開其他APP

    Android操作Html打開其他APP

    這篇文章主要為大家詳細(xì)介紹了Android操作Html打開其他APP的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android 根據(jù)手勢頂部View自動展示與隱藏效果

    Android 根據(jù)手勢頂部View自動展示與隱藏效果

    這篇文章主要介紹了Android 根據(jù)手勢頂部View自動展示與隱藏效果,本文給大家介紹非常詳細(xì)包括實現(xiàn)原理和實例代碼,需要的朋友參考下吧
    2017-08-08
  • Flutter構(gòu)建自定義Widgets的全過程記錄

    Flutter構(gòu)建自定義Widgets的全過程記錄

    在Flutter實際開發(fā)中,大家可能會遇到flutter框架中提供的widget達(dá)不到我們想要的效果,這時就需要我們?nèi)プ远xwidget,下面這篇文章主要給大家介紹了關(guān)于Flutter構(gòu)建自定義Widgets的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Android IPC機(jī)制綁定Service實現(xiàn)本地通信

    Android IPC機(jī)制綁定Service實現(xiàn)本地通信

    本文主要介紹Android IPC機(jī)制綁定Service 實現(xiàn)本地通信,通過圖解,代碼等方式給大家解釋Android IPC機(jī)制,需要參考的同學(xué)可以看一下
    2016-07-07
  • Android開發(fā)筆記之:用Enum(枚舉類型)取代整數(shù)集的應(yīng)用詳解

    Android開發(fā)筆記之:用Enum(枚舉類型)取代整數(shù)集的應(yīng)用詳解

    本篇文章是對Android中用Enum(枚舉類型)取代整數(shù)集的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android 實現(xiàn)銀聯(lián)刷卡機(jī)消費后手動簽名的功能(示例代碼)

    Android 實現(xiàn)銀聯(lián)刷卡機(jī)消費后手動簽名的功能(示例代碼)

    在一些商場購物時,不需要用筆在銀聯(lián)機(jī)上簽名了,直接用手指觸摸實現(xiàn)消費簽名,非常方便,下面小編給大家分享Android 實現(xiàn)銀聯(lián)刷卡機(jī)消費后手動簽名的功能,需要的朋友參考下吧
    2017-12-12
  • android和js的交互之jsbridge使用教程

    android和js的交互之jsbridge使用教程

    這篇文章主要給大家介紹了關(guān)于android和js的交互之jsbridge使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • Android列表實現(xiàn)單選點擊縮放動畫效果

    Android列表實現(xiàn)單選點擊縮放動畫效果

    在android開發(fā),我們會常常使用到縮放動畫,這篇文章主要給大家介紹了關(guān)于Android列表實現(xiàn)單選點擊縮放動畫效果的相關(guān)資料,需要的朋友可以參考下
    2021-08-08

最新評論