Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android錄制聲音文件(音頻),並播放

Android錄制聲音文件(音頻),並播放

編輯:關於Android編程

readme:1、這個demo中沒有對多次點擊同一個聲音文件做詳細處理,偶爾會有崩潰,用的時候需要注意。2、按住錄音按鈕錄音過程中,只對豎直方向處理了一下,水平方向沒寫;3、沒有做刪除某個聲音文件的操作,但是測試的時候實現了功能,需要用到的話,在MainActivity—>onItemClick中的TODO中有詳細說明;4、這只是個demo,如果要在項目中使用,先寫出demo,沒問題了,再引入項目,在寫成demo後,在真機上運行的時候,如果出現獲取錄音權限,最好選擇“允許”,如果拒絕,可能會崩潰。

記得打開手機運行錄音的權限
先來效果圖:
這裡寫圖片描述

目錄結構:

這裡寫圖片描述

1、添加權限:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">

2、新建MediaRecorderUtils,復制以下源碼:

package com.chen.voicedemo;

import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;

/**
 * 錄音工具類
 */
public class MediaRecorderUtils {

    private static MediaRecorder recorder;
    static MediaRecorderUtils mediaRecorderUtils;
    static ImageView mimageView;
    private String path;

    /**
     * 獲得單例對象,傳入一個顯示音量大小的imageview對象,如不需要顯示可以傳null
     */
    public static MediaRecorderUtils getInstence(ImageView imageView) {
        if (mediaRecorderUtils == null) {
            mediaRecorderUtils = new MediaRecorderUtils();
        }
        mimageView = imageView;
        return mediaRecorderUtils;
    }

    /**
     * 獲得音頻路徑
     */
    public String getPath() {
        return path;
    }

    /**
     * 初始化
     */
    private void init() {

        recorder = new MediaRecorder();// new出MediaRecorder對象
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // 設置MediaRecorder的音頻源為麥克風  
        recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
        // 設置MediaRecorder錄制的音頻格式  
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        // 設置MediaRecorder錄制音頻的編碼為amr.  
        File file = new File(Utils.IMAGE_SDCARD_MADER);
        if (!file.exists()) {
            file.mkdirs();
        }
        path = Utils.IMAGE_SDCARD_MADER + Utils.getVoiceFileName() + "stock.amr";
        recorder.setOutputFile(path);
        // 設置錄制好的音頻文件保存路徑  
        try {
            recorder.prepare();// 准備錄制  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 開始錄音
     */
    public void MediaRecorderStart() {
        init();
        try {
            recorder.start();
            flag = true;
            if (mimageView != null) {
                updateMicStatus();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("chen", "錄制失敗");
        }
    }

    /**
     * 停止錄音
     */
    public void MediaRecorderStop() {
        try {
            recorder.stop();
            recorder.release(); //釋放資源
            flag = false;
            mimageView = null;
            recorder = null;
        } catch (Exception e) {
            e.toString();
        }

    }

    /**
     * 刪除已錄制的音頻
     */
    public void MediaRecorderDelete() {
        File file = new File(path);
        if (file.isFile()) {
            file.delete();
        }
        file.exists();
    }

    ;

    private final Handler mHandler = new Handler();
    private Runnable mUpdateMicStatusTimer = new Runnable() {
        public void run() {
            updateMicStatus();
        }
    };
    private int BASE = 1;
    private int SPACE = 1000;// 間隔取樣時間
    private boolean flag = true;

    /**
     * 更新話筒狀態
     */
    private void updateMicStatus() {
        if (recorder != null) {
            double ratio = (double) recorder.getMaxAmplitude() / BASE;
            double db = 0;// 分貝
            if (ratio > 1) {
                db = 20 * Math.log10(ratio);
            }
            int i = (int) db / 10;
            switch (i) {
                case 1:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_1);
                    break;
                case 2:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_2);
                    break;
                case 3:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_3);
                    break;
                case 4:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_4);
                    break;
                case 5:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_5);
                    break;
                case 6:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_6);
                    break;
                case 7:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_7);
                    break;
                case 8:
                    mimageView.setImageResource(R.drawable.rc_ic_volume_8);
                    break;
            }
            if (flag) {
                mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);
            }
        }
    }

}

3、創建MyChronometer,復制以下代碼

package com.chen.voicedemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.TextView;

public class MyChronometer extends TextView {

    private static final String TAG = "MyChronometer";

    /**
     * A callback that notifies when the MyChronometer has incremented on its
     * own.
     */
    public interface OnMyChronometerTickListener {

        /**
         * Notification that the MyChronometer has changed.
         */
        void onMyChronometerTick(int time);

    }

    public interface OnMyChronometerTimeListener {

        /**
         * Notification that the MyChronometer has changed.
         */
        void OnMyChronometerTimeListener(int time);

    }

    private OnMyChronometerTimeListener OnMyChronometerTimeListener;

    private long mBase;
    private boolean mVisible;
    private boolean mStarted;
    private boolean mRunning;
    private OnMyChronometerTickListener mOnMyChronometerTickListener;
    private long now_time;

    private static final int TICK_WHAT = 2;

    /**
     * Initialize this MyChronometer object. Sets the base to the current time.
     */
    public MyChronometer(Context context) {
        this(context, null, 0);
    }

    /**
     * Initialize with standard view layout information. Sets the base to the
     * current time.
     */
    public MyChronometer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * Initialize with standard view layout information and style. Sets the base
     * to the current time.
     */
    public MyChronometer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mBase = SystemClock.elapsedRealtime();
        updateText(mBase);
    }

    /**
     * Set the time that the count-up timer is in reference to.
     *
     * @param base Use the {@link SystemClock#elapsedRealtime} time base.
     */
    public void setBase(long base) {
        mBase = base;
        updateText(SystemClock.elapsedRealtime());
    }

    /**
     * Sets the listener to be called when the MyChronometer changes.
     *
     * @param listener The listener.
     */
    public void setOnMyChronometerTickListener(OnMyChronometerTickListener listener) {
        mOnMyChronometerTickListener = listener;
    }

    public void setOnMyChronometerTimeListener(OnMyChronometerTimeListener listener) {
        OnMyChronometerTimeListener = listener;
    }

    /**
     * Start counting up. This does not affect the base as set from
     * {@link #setBase}, just the view display.
     * 

* MyChronometer works by regularly scheduling messages to the handler, even * when the Widget is not visible. To make sure resource leaks do not occur, * the user should make sure that each start() call has a reciprocal call to * {@link #stop}. */ public void start() { mStarted = true; updateRunning(); } /** * Stop counting up. This does not affect the base as set from * {@link #setBase}, just the view display. *

* This stops the messages to the handler, effectively releasing resources * that would be held as the MyChronometer is running, via {@link #start}. */ public void stop() { mStarted = false; updateRunning(); now_time /= 10; if (OnMyChronometerTimeListener != null) { OnMyChronometerTimeListener.OnMyChronometerTimeListener((int) now_time); } } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mVisible = false; updateRunning(); } @Override protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); mVisible = visibility == VISIBLE; updateRunning(); } private synchronized void updateText(long now) { long seconds = now - mBase; seconds /= 10; now_time = seconds; int time_m = (int) (seconds / 100); if (mOnMyChronometerTickListener != null) { mOnMyChronometerTickListener.onMyChronometerTick(time_m); } int time_s = (int) (seconds % 100); setText(time_m + ""); } private void updateRunning() { boolean running = mVisible && mStarted; if (running != mRunning) { if (running) { updateText(SystemClock.elapsedRealtime()); mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000); } else { mHandler.removeMessages(TICK_WHAT); } mRunning = running; } } private Handler mHandler = new Handler() { public void handleMessage(Message m) { if (mRunning) { updateText(SystemClock.elapsedRealtime()); sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000); } } }; @SuppressLint("NewApi") @Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setClassName(MyChronometer.class.getName()); } @SuppressLint("NewApi") @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.setClassName(MyChronometer.class.getName()); } }

4、創建工具類

package com.chen.voicedemo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 工具
 */
public class Utils {

    /**
     * SD卡下語音目錄
     */
    public static final String IMAGE_SDCARD_MADER = Environment
            .getExternalStorageDirectory()
            + "/chen/voice/";

    /**
     * 檢查錄音權限6.0
     */
    public static boolean checkVoice(Context context) {

        try {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            return true;
        }

    }

    private static Toast toast;

    /**
     * 單例吐司
     */
    public static void showToast(Context context, String msg) {
        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    /**
     * 獲取指定文件夾下的所有文件路徑
     *
     * @param root 指定文件夾路徑
     * @return 指定文件夾下的所有文件
     */
    public static ArrayList getVideoFiles(String root) {
        if (root == null || root == "")
            return null;

        ArrayList list = new ArrayList<>();
        File file = new File(root);
        File[] fileList = file.listFiles();

        for (File f : fileList) {
            list.add(f.getPath());
        }

        return list;
    }

    /**
     * 獲取聲音文件名字
     *
     * @return 假如當前錄制聲音時間是2016年4月29號14點30分30秒。得到的文件名字就是20160429143030.這樣保證文件名的唯一性
     */
    public static String getVoiceFileName() {
        long getNowTimeLong = System.currentTimeMillis();
        SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss");
        String result = time.format(getNowTimeLong);
        return result;
    }


}

5、MainActivity

package com.chen.voicedemo;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnTouchListener, AdapterView.OnItemClickListener {

    /**
     * 開始錄音按鈕
     */
    private TextView voice;
    /**
     * 用於定位。使錄音時展示的popupwindow,展示在該控件 的下面
     */
    private TextView voice_popup;

    /**
     * 展示指定文件夾下所有錄制的聲音文件
     */
    private TextView show_voice_list;

    /**
     * 展示目標文件夾下,所有已錄制的聲音路徑
     */
    private ListView show_voices_listview;

    private List voiceList;

    /**
     * 停止播放聲音
     */
    private TextView stop_show_voice;

    /**
     * 播放聲音時,動的圖片
     */
    private ImageView voice_anim;

    /**
     * 系統播放器
     */
    private MediaPlayer mediaPlayer;

    private Boolean flag = true;
    private float int_x = 0;
    private float int_y = 0;

    /**
     * 用於限制最大錄音時常。單位是秒。意義是:最大錄60秒的音頻,到了60秒的是,自動停止
     */
    private int maxRecordTime = 60;

    /**
     * 用於顯示頻繁操作時間間隔。單位是毫秒。意義是:500毫秒內再次操作,就算是頻頻操作,做相應處理
     */
    private int oftenOperationTime = 500;

    private MyAdapter myAdapter;

    private AnimationDrawable animation;

    /**
     * 錄音popup
     */
    private PopupWindow voice_popupWindow;
    /**
     * 錄音時聲音變化
     */
    private ImageView voice_shengyin;
    /**
     * 錄音計時器
     */
    private MyChronometer mychronometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        voiceList = new ArrayList();

        voice = (TextView) findViewById(R.id.voice);
        voice_popup = (TextView) findViewById(R.id.voice_popup);
        voice_anim = (ImageView) findViewById(R.id.voice_anim);
        voice_anim.setImageResource(R.drawable.lcs_voice_receive);

        show_voice_list = (TextView) findViewById(R.id.show_voice_list);

        show_voice_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                voiceList = Utils.getVideoFiles(Utils.IMAGE_SDCARD_MADER);
                if (voiceList.size()>0){
                    myAdapter.notifyDataSetChanged();
                }else{
                    Utils.showToast(MainActivity.this, "沒有文件");
                }
            }
        });

        show_voices_listview = (ListView) findViewById(R.id.show_voices);
        show_voices_listview.setOnItemClickListener(this);
        myAdapter = new MyAdapter();

        stop_show_voice = (TextView) findViewById(R.id.stop_show_voice);

        /**
         * 停止播放的監聽器
         */
        stop_show_voice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e("chen", "點擊了停止播放按鈕");

                if (mediaPlayer != null) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.release();// 釋放資源
                    }
                    mediaPlayer = null;
                }
                if (animation != null && animation.isRunning()) {
                    animation.stop();
                }
                voice_anim.setImageResource(R.drawable.lcs_voice_receive);
            }
        });

        show_voices_listview.setAdapter(myAdapter);

        voice.setOnTouchListener(this);

    }

    /**
     * 聲音文件列表的item點擊事件,播放對應聲音文件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {

        //TODO 以下4行,是用來做測試,點擊item,手機SD卡上對應路徑下的聲音文件就會被刪除。如果錄制聲音失敗,或者不滿足條件,可以把以下4行寫成一個工具方法調用,刪除不滿意的文件。這裡不做詳細演示
        //File f_delete=new File(voiceList.get(position));
        //f_delete.delete();
        //voiceList.remove(voiceList.get(position));
        //myAdapter.notifyDataSetChanged();
        //TODO 以上4行,是用來做測試,點擊item,手機SD卡上對應路徑下的聲音文件就會被刪除。

        try {
            mediaPlayer = new MediaPlayer();

            /**
             * 播放過程中展示的動畫
             */
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (mp != null) {
                        mp.start();
                        voice_anim.setImageResource(R.drawable.voice_anim);
                    }
                }
            });

            /**
             *  播放完成監聽
             */
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (mp.isPlaying()) {
                        mp.release();// 釋放資源
                    }
                    animation = (AnimationDrawable) voice_anim.getDrawable();
                    if (animation != null && animation.isRunning()) {
                        animation.stop();
                    }
                    voice_anim.setImageResource(R.drawable.lcs_voice_receive);
                }
            });
            mediaPlayer.setDataSource(voiceList.get(position));
            // 緩沖
            mediaPlayer.prepare();

        } catch (Exception e) {
            Utils.showToast(MainActivity.this, "語音異常,加載失敗");
        }
    }

    /**
     * 展示聲音列表的adapter
     */
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return voiceList.size() == 0 ? 0 : voiceList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(MainActivity.this);
            tv.setText(voiceList.get(position));
            tv.setTextSize(20);
            return tv;
        }
    }


    /**
     * 開始錄制按鈕的onTouch事件
     *
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (v.getId() == R.id.voice) {

            //檢查權限
            if (!Utils.checkVoice(this)) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Utils.showToast(this, "錄音權限未打開,請打開錄音權限!");
                }
                return true;
            }

            //避免短時間裡頻繁操作
            if (!getTimeTF(SystemClock.elapsedRealtime()) && event.getAction() == MotionEvent.ACTION_DOWN) {
                Utils.showToast(this, "操作過於頻繁");
                return true;
            }

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                setTime(SystemClock.elapsedRealtime());
            }
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    int_x = event.getRawX();
                    int_y = event.getRawY();
                    VoicePopupWindow();
                    mychronometer.setBase(SystemClock.elapsedRealtime());
                    mychronometer.start();
                    MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStart();
                    flag = true;
                    mychronometer.setOnMyChronometerTickListener(new MyChronometer.OnMyChronometerTickListener() {
                        @Override
                        public void onMyChronometerTick(int time) {
                            if (time == maxRecordTime || time > maxRecordTime) {
                                mychronometer.setText("60");
                                setVoiceToUp();
                            }
                        }
                    });
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (flag) {
                        if (Math.abs(int_y) - Math.abs(event.getRawY()) > 100.0 && flag) {
                            voice_popupWindow.dismiss();
                            mychronometer.stop();
                            MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStop();
                            MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderDelete();
                            flag = false;
                        }
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                    if (flag) {
                        voice_popupWindow.dismiss();
                        mychronometer.stop();
                        MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStop();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (flag) {
                        setVoiceToUp();
                    }
                    break;
            }
            return true;
        }
        return false;
    }

    private long base_time = 0;

    private void setTime(long time) {
        base_time = time;
    }

    private boolean getTimeTF(long time) {
        int data = (int) (time - base_time) / oftenOperationTime;
        if (data > 1) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 聲音popupwindow
     */
    public void VoicePopupWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.voice_popupwindow, null);
        voice_popupWindow = new PopupWindow(this);
        voice_popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        voice_popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        voice_shengyin = (ImageView) view.findViewById(R.id.voice_shengyin);
        mychronometer = (MyChronometer) view.findViewById(R.id.mychronometer);
        voice_popupWindow.setContentView(view);
        voice_popupWindow.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        voice_popupWindow.setBackgroundDrawable(dw);
        voice_popupWindow.showAsDropDown(voice_popup);
    }


    private void setVoiceToUp() {
        flag = false;
        voice_popupWindow.dismiss();
        mychronometer.stop();
        MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStop();
        int time = Integer.parseInt(mychronometer.getText().toString());

        if (time != 0) {
            File file = new File(MediaRecorderUtils.getInstence(voice_shengyin).getPath());
            if (file.length() > 0) {
                voiceList = Utils.getVideoFiles(Utils.IMAGE_SDCARD_MADER);
                myAdapter.notifyDataSetChanged();

            } else {
                Utils.showToast(this, "錄音失敗,請檢查權限");
            }
        } else {
            Utils.showToast(this, "錄音時間太短");
        }
    }

}

6、activity_main布局




    

    


    

        


        

        

    

    

7、voice_popupwindow布局代碼:錄音的時候,會出現以下圖片中的popupwindow
這裡寫圖片描述




    

        

        

    

8、還有一個動畫布局,播放聲音的時候,有個動畫效果
這裡寫圖片描述




    
    
    

附錄:用到的圖片資源說明:如果手上沒有這樣的圖片,可以隨便用其他圖片代替,有效果,就算成功
這裡寫圖片描述


這裡寫圖片描述

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