標籤:
之前,寫過一篇關於安卓錄製音訊部落格,裡面有些問題沒有解決,這裡,做一個最佳化
之前的那個部落格,
http://blog.csdn.net/u014620028/article/details/51283261
最佳化的地方:有多個音頻檔案,點擊A,播放,A在播放過程中,如果點擊A,停止播放;如果點擊B,A停止,播放B。類似於QQ的語音播放
現在的功能最佳化,是在之前的基礎上進行的,所以這裡唯寫最佳化的地方
1、工程目錄結構
新加了這個播放工具類
源碼:
package com.chen.voicedemo;import android.graphics.drawable.AnimationDrawable;import android.media.MediaPlayer;import android.widget.ImageView;import java.io.File;import java.io.FileInputStream;/** * 語音播放類 */public class MediaPlayerUtils { private static MediaPlayerUtils mediaPlayerUtils; private MediaPlayer mediaPlayer; private boolean flag=true; private static String media_path; private static ImageView media_imageView; public static MediaPlayerUtils getInstense(){ if (mediaPlayerUtils==null){ mediaPlayerUtils=new MediaPlayerUtils(); } return mediaPlayerUtils; } public void setPlayorStop(String path, ImageView imageView){ if (setTFVoice(path)) { media_path=path; media_imageView=imageView; AnimationDrawable animation = null; try { animation = (AnimationDrawable) media_imageView.getDrawable(); if (animation != null) { if (animation.isRunning()) { flag = false; return; } else { flag = true; } } } catch (Exception e) { e.printStackTrace(); } try { if (flag) { flag = false; mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { if (mp!=null) { mp.start(); media_imageView.setImageResource(R.drawable.voice_anim); AnimationDrawable animationDrawable= (AnimationDrawable) media_imageView.getDrawable(); animationDrawable.start(); } } }); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { flag = true; if (mp.isPlaying()) { mp.release();// 釋放資源 } try { AnimationDrawable animation = (AnimationDrawable) media_imageView.getDrawable(); if (animation != null && animation.isRunning()) { animation.stop(); } media_imageView.setImageResource(R.drawable.lcs_voice_receive); } catch (Exception e) { e.toString(); } } }); if (path.indexOf("http") == -1 && path.indexOf("file") == -1) { File file = new File(path); FileInputStream fis = new FileInputStream(file); mediaPlayer.setDataSource(fis.getFD());// mediaPlayer.setDataSource(FileHelper.getRealFilePath(bean.getVoice_Url())); } else { mediaPlayer.setDataSource(path); } mediaPlayer.prepare();// 緩衝 } } catch (Exception e) { flag = true; e.printStackTrace(); } } } public boolean setVoice_Stop(){ if (mediaPlayer == null){ return true; }else if (mediaPlayer != null&&mediaPlayer.isPlaying()){ flag=true; mediaPlayer.stop(); try { AnimationDrawable animation = (AnimationDrawable) media_imageView.getDrawable(); if (animation != null && animation.isRunning()) { animation.stop(); } media_imageView.setImageResource(R.drawable.lcs_voice_receive); } catch (Exception e) { e.toString(); } return false; }else if (mediaPlayer != null&&!mediaPlayer.isPlaying()){ return true; } mediaPlayer=null; return false; } private boolean setTFVoice(String path){ boolean mm=setVoice_Stop(); if (mm){ return true; }else { if (media_path.equals(path)) { return false; }else { return true; } } }}
之前的播放語音方法,是在Listview的onItemClick方法中,點擊那個,做對應的 操作,缺少一些判斷條件。這裡,封裝好 MediaPlayerUtils 只需要2個參數,即可,在onItemClick方法中
MediaPlayerUtils.getInstense().setPlayorStop(voiceList.get(position), voice_anim);
就可以了。
參數說明:第一個參數,是語音的路徑,可以是網路路徑,也可以使本地路徑。第二個參數,是ImageView。就是用於展示聲波的那個圖片。
還有一點需要說明的是,在初始化展示聲波圖片的時候,要用setImageResource,不要在xml檔案裡寫background,不然,有時候會導致動畫不能動。最開始初始化的時候,用靜態圖片,需要動的時候,寫xml動畫檔案。詳細地方,見源碼
Android錄製聲音,並播放,功能最佳化