Android实现语⾳播放与录⾳功能
本⽂实例为⼤家分享了Android实现语⾳播放与录⾳的具体代码,供⼤家参考,具体内容如下
项⽬⽤到的技术点和亮点
语⾳录⾳(单个和列表)
语⾳播放(单个和列表)
语⾳录⾳封装
语⾳播放器封装
语⾳列表顺序播放
语⾳列表单个播放复⽤问题处理
因为安装原⽣录⾳不能录mp3格式⽂件⽽mp3格式是安卓和ios公⽤的,所以我们需要的是能直接录取mp3⽂件或者录完的格式转成mp3格式
下⾯添加这个库能直接录mp3⽂件,我觉得是最⽅便的
compile ‘p3recorder:library:1.0.3'
1. 语⾳录⾳封装
代码简单⾃⼰看吧
package com.video.zlc.audioplayer;
p3recorder.MP3Recorder;
import com.video.zlc.audioplayer.utils.LogUtil;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* @author zlc
*/
public class AudioManage {
private MP3Recorder mRecorder;
private String mDir;    // ⽂件夹的名称
private String mCurrentFilePath;
private static AudioManage mInstance;
private boolean isPrepared; // 标识MediaRecorder准备完毕
private AudioManage(String dir) {
mDir = dir;
LogUtil.e("AudioManage=",mDir);
}
/
**
* 回调“准备完毕”
* @author zlc
*/
public interface AudioStateListenter {
void wellPrepared(); // prepared完毕
}
public AudioStateListenter mListenter;
public void setOnAudioStateListenter(AudioStateListenter audioStateListenter) {
mListenter = audioStateListenter;
}
/
**
* 使⽤单例实现 AudioManage
* @param dir
* @return
*/
public static AudioManage getInstance(String dir) {
if (mInstance == null) {
synchronized (AudioManage.class) { // 同步
}
return mInstance;
}
/
**
* 准备录⾳
*/
public void prepareAudio() {
try {
isPrepared = false;
File dir = new File(mDir);
if (!ists()) {
dir.mkdirs();
}
String fileName = GenerateFileName(); // ⽂件名字
File file = new File(dir, fileName); // 路径+⽂件名字
//MediaRecorder可以实现录⾳和录像。需要严格遵守API说明中的函数调⽤先后顺序.
mRecorder = new MP3Recorder(file);
mCurrentFilePath = AbsolutePath();
//  mMediaRecorder = new MediaRecorder();
//  mCurrentFilePath = AbsolutePath();
//  mMediaRecorder.AbsolutePath()); // 设置输出⽂件
//  mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置MediaRecorder的⾳频源为麦克风//  mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); // 设置⾳频的格式
//  mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 设置⾳频的编码为AMR_NB //  mMediaRecorder.prepare();
/
/  mMediaRecorder.start();
mRecorder.start(); //开始录⾳
isPrepared = true; // 准备结束
if (mListenter != null) {
mListenter.wellPrepared();
}
} catch (Exception e) {
e.printStackTrace();
LogUtil.e("prepareAudio",e.getMessage());
}
}
/
**
* 随机⽣成⽂件名称
* @return
*/
private String GenerateFileName() {
// TODO Auto-generated method stub
return UUID.randomUUID().toString() + ".mp3"; // ⾳频⽂件格式
}
/**
* 获得⾳量等级——通过mMediaRecorder获得振幅,然后换算成声⾳Level
* maxLevel最⼤为7;
* @return
*/
public int getVoiceLevel(int maxLevel) {
if (isPrepared) {
try {
return maxLevel * MaxVolume() / 32768 + 1;
} catch (Exception e) {
e.printStackTrace();
}
}
return 1;
}
/**
* 释放资源
*/
public void release() {
if(mRecorder != null) {
mRecorder.stop();
/**
* 停⽌录⾳
*/
public void stop(){
if(mRecorder!=null && mRecorder.isRecording()){
mRecorder.stop();
}
}
/**
* 取消(释放资源+删除⽂件)
*/
public void delete() {
release();
if (mCurrentFilePath != null) {
File file = new File(mCurrentFilePath);
file.delete(); //删除录⾳⽂件
mCurrentFilePath = null;
}
}
public String getCurrentFilePath() {
return mCurrentFilePath;
}
public int getMaxVolume(){
MaxVolume();
}
public int getVolume(){
Volume();
}
}
2. 语⾳播放器封装
package com.video.zlc.audioplayer.utils;
t.Context;
dia.AudioManager;
dia.MediaPlayer;
import android.Uri;
/**
*
* @author zlc
*
*/
public class MediaManager {
private static MediaPlayer mMediaPlayer; //播放录⾳⽂件
private static boolean isPause = false;
static {
if(mMediaPlayer==null){
mMediaPlayer=new MediaPlayer();
mMediaPlayer.setOnErrorListener( new MediaPlayer.OnErrorListener() {    @Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
}
}
/**
* 播放⾳频
* @param filePath
* @param onCompletionListenter
public static void playSound(Context context,String filePath, MediaPlayer.OnCompletionListener onCompletionListenter){  if(mMediaPlayer==null){
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnErrorListener( new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
}else{
}
try {
//详见“MediaPlayer”调⽤过程图
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnCompletionListener(onCompletionListenter);
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
LogUtil.e("语⾳error==",e.getMessage());
}
}
/**
* 暂停
*/
public synchronized static void pause(){
if(mMediaPlayer!=null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
isPause=true;
}
}
//停⽌
public synchronized static void stop(){
if(mMediaPlayer!=null && mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
isPause=false;
}
}
/**
* resume继续
*/
public synchronized static void resume(){
if(mMediaPlayer!=null && isPause){
mMediaPlayer.start();
isPause=false;
}
}
public static boolean isPause(){
return isPause;
}
public static void setPause(boolean isPause) {
MediaManager.isPause = isPause;
}
/**
* release释放资源
*/
public static void release(){
if(mMediaPlayer!=null){
isPause = false;
mMediaPlayer.stop();
mMediaPlayer = null;
}
public synchronized static void reset(){
if(mMediaPlayer!=null) {
isPause = false;
}
}
/**
* 判断是否在播放视频
* @return
*/
public synchronized static boolean isPlaying(){
return mMediaPlayer != null && mMediaPlayer.isPlaying();video安卓下载
}
}
3. 语⾳列表顺序播放
private int lastPos = -1;
//播放语⾳
private void playVoice(final int position, String from) {
LogUtil.e("playVoice position",position+"");
if(position >= records.size()) {
LogUtil.e("playVoice","全部播放完了");
stopAnimation();
return;
}
String voicePath = (position).getPath();
LogUtil.e("playVoice",voicePath);
if(TextUtils.isEmpty(voicePath) || !ains(".mp3")){
Toast.makeText(this,"语⾳⽂件不合法",Toast.LENGTH_LONG).show();
return;
}
if(lastPos != position && "itemClick".equals(from)){
stopAnimation();
}
lastPos = position;
//获取listview某⼀个条⽬的图⽚控件
int pos = position - id_FirstVisiblePosition();
View view = id_ChildAt(pos);
id_iv_voice = (ImageView) view.findViewById(R.id.id_iv_voice);
LogUtil.e("playVoice position",pos+"");
if(MediaManager.isPlaying()){
MediaManager.pause();
stopAnimation();
}else if(MediaManager.isPause()){
startAnimation();
}else{
startAnimation();
MediaManager.playSound(this,voicePath, new MediaPlayer.OnCompletionListener() {    @Override
public void onCompletion(MediaPlayer mediaPlayer) {
//播放完停⽌动画重置MediaManager
stopAnimation();
playVoice(position + 1, "loop");
}
});
}
}
4. 语⾳列表单个播放复⽤问题处理

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。