栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 移动开发 > Android

Android录音播放管理工具

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android录音播放管理工具

1、语音播放直接用系统工具就好了,这个就不多说了,根据传入的路径(网络路径或本地路径均可)播放音频文件



public class PlayManager {
 private Context mcontext;
 public PlayManager(Context context){
  this.mcontext = context;
 }

 public void play(String song){
  MediaPlayer mp = new MediaPlayer();
  try {
//   存储在SD卡或其他文件路径下的媒体文件
//   例如:mp.setDataSource("/sdcard/test.mp3");
//   网络上的媒体文件
//   例如:mp.setDataSource("http://www...../music/test.mp3");
   mp.setDataSource(song);
   mp.prepare();
   mp.start();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

2.录制amr格式音频文件(微信语音便用的这种格式,至于音频文件格式之间的比较请自行百度)



public class RecordManager {
 //录制成amr格式............................................................
 private Context mcontext;
 MediaRecorder mediaRecorder ;

 public RecordManager(Context context){
  this.mcontext = context;
  //TODO 初始化安装路径,录音流程
 }

 
 public void start_amr(){
  mediaRecorder = new MediaRecorder();
  
  mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  
  mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
  
  mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  
  mediaRecorder.setOutputFile(String.valueOf(PathManger.getVoicePath()));

  
  try {
   mediaRecorder.prepare();
   mediaRecorder.start();
  }
  catch (IllegalStateException e) {
   e.printStackTrace();
  }
  catch (IOException e) {
   e.printStackTrace();
  }
 }

 
 public void stop_amr(){
  mediaRecorder.stop();
  mediaRecorder.release();
  mediaRecorder = null;
 }

 
 public void reset_amr(){
  mediaRecorder.reset();
 }

}

3、配置转换工具包(这个比较简单,配置以下文件即可)

添加flame.jar,并在armeabi和armeabi-v7a文件夹添加libmp3lame.so
资源文件:http://xiazai.jb51.net/201701/yuanma/androidlibmp3lame(jb51.net).rar

4、录制MP3格式音频文件(个人觉得这种格式能比较好的统一Android端和iOS端的音频文件,虽然方法相对比较繁杂一些)


public class RecordManager {
 //录制成MP3格式..............................................
 
 private Activity activity;

 
 public static final int RAW = 0x00000001;
 public static final int MP3 = 0x00000002;

 
 private String rawPath = null;
 private String mp3Path = null;

 
 private static final int SAMPLE_RATE = 11025;

 
 private short[] mBuffer;
 private AudioRecord mRecorder;

 
 private boolean isRecording = false;
 
 private boolean convertOk = false;

 public RecordManager(Activity activity, String rawPath, String mp3Path) {
  this.activity = activity;
  this.rawPath = rawPath;
  this.mp3Path = mp3Path;
 }

 
 public boolean start_mp3() {
  // 如果正在录音,则返回
  if (isRecording) {
   return isRecording;
  }
  // 初始化
  if (mRecorder == null) {
   initRecorder();
  }

  getFilePath();
  mRecorder.startRecording();
  startBufferedWrite(new File(rawPath));

  isRecording = true;
  return isRecording;
 }

 
 public boolean stop_mp3() {
  if (!isRecording) {
   return isRecording;
  }

  // 停止
  mRecorder.stop();
  isRecording = false;
//TODO
  // 开始转换
  FLameUtils lameUtils = new FLameUtils(1, SAMPLE_RATE, 96);
  convertOk = lameUtils.raw2mp3(rawPath, mp3Path);

  return isRecording ^ convertOk;// convertOk==true,return true
 }

 
 public String getFilePath(int fileAlias) {
  if (fileAlias == RAW) {
   return rawPath;
  } else if (fileAlias == MP3) {
   return mp3Path;
  } else
   return null;
 }

 
 public void cleanFile(int cleanFlag) {
  File f = null;
  try {
   switch (cleanFlag) {
    case MP3:
     f = new File(mp3Path);
     if (f.exists())
      f.delete();
     break;
    case RAW:
     f = new File(rawPath);
     if (f.exists())
      f.delete();
     break;
    case RAW | MP3:
     f = new File(rawPath);
     if (f.exists())
      f.delete();
     f = new File(mp3Path);
     if (f.exists())
      f.delete();
     break;
   }
   f = null;
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 
 public void close() {
  if (mRecorder != null)
   mRecorder.release();
  activity = null;
 }

 
 private void initRecorder() {
  int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
  mBuffer = new short[bufferSize];
  mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE,
    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
    bufferSize);
 }

 
 private void getFilePath() {
  try {
   String folder = "audio_recorder_2_mp3";
   String fileName = String.valueOf(System.currentTimeMillis());
   if (rawPath == null) {
    File raw = new File(activity.getDir(folder,
      activity.MODE_PRIVATE), fileName + ".raw");
    raw.createNewFile();
    rawPath = raw.getAbsolutePath();
    raw = null;
   }
   if (mp3Path == null) {
    File mp3 = new File(activity.getDir(folder,
      activity.MODE_PRIVATE), fileName + ".mp3");
    mp3.createNewFile();
    mp3Path = mp3.getAbsolutePath();
    mp3 = null;
   }

   Log.d("rawPath", rawPath);
   Log.d("mp3Path", mp3Path);

   runCommand("chmod 777 " + rawPath);
   runCommand("chmod 777 " + mp3Path);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 
 private boolean runCommand(String command) {
  boolean ret = false;
  Process process = null;
  try {
   process = Runtime.getRuntime().exec(command);
   process.waitFor();
   ret = true;
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    process.destroy();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return ret;
 }

 
 private void startBufferedWrite(final File file) {
  new Thread(new Runnable() {
   @Override
   public void run() {
    DataOutputStream output = null;
    try {
     output = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(file)));
     while (isRecording) {
      int readSize = mRecorder.read(mBuffer, 0,
 mBuffer.length);
      for (int i = 0; i < readSize; i++) {
output.writeShort(mBuffer[i]);
      }
     }
    } catch (IOException e) {
     e.printStackTrace();
    } finally {
     if (output != null) {
      try {
output.flush();
      } catch (IOException e) {
e.printStackTrace();

      } finally {
try {
 output.close();
} catch (IOException e) {
 e.printStackTrace();
}
      }
     }
    }
   }
  }).start();
 }
}


5、最后在自己想调用的地方调用就好了,PathManger这个是我自己的路径管理工具,这里不贴了,反正自己直接放一个路径字符串进去就好了


 private void initVoice() {
 //录音
 RecordManager = new RecordManager(
  CallHelpActivity.this,
  String.valueOf(PathManger.getVoicePathToRaw()),
  String.valueOf(PathManger.getVoicePathToMp3()));
 callHelp_Voice_longclick.setonTouchListener(new View.onTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
  switch(event.getAction()){
   case MotionEvent.ACTION_DOWN:
   RecordManager.start_mp3();
   break;
   case MotionEvent.ACTION_MOVE:

   break;
   case MotionEvent.ACTION_UP:
   RecordManager.stop_mp3();
   break;
  }
  return false;
  }
 });
 //语音播放
 final PlayManager PlayManager = new PlayManager(this);
 callHelp_Voice_click.setonClickListener(new View.onClickListener() {
  @Override
  public void onClick(View v) {
  PlayManager.play(String.valueOf(PathManger.getVoicePathToMp3()));
  }
 });
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/160023.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号