如何同时录制音频和播放声音。哪个进程最好在后台实现,以及使用Thread或AsyncTask的内容是什么?我曾尝试在新线程中播放声音并在主线程上录制,但我有问题,在某些设备上我得到主线程过载的错误。
使用本机rocordning是否更好,因为我还需要录制缓冲区?
有没有人有如何使用原生录音的例子?
答案 0 :(得分:0)
您可以使用以下自定义类: -
package com.app.controller;
import android.content.Context; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.provider.SyncStateContract.Constants; import android.widget.Toast;
public class MediaController实现了OnPreparedListener {
public MediaController() {
// TODO Auto-generated constructor stub
}
public MediaPlayer mp;
public void getMediaPlayObject() {
try {
System.out.println("00000000000000");
mp = new MediaPlayer();
System.out.println("2222222222");
} catch (Exception e) {
// TODO: handle exception
System.out.println("exception in audia player====" + e.toString());
}
}
public void onPrepared(MediaPlayer player) {
mp.start();
}
boolean WORKING = true;
public void mediaPlayStart(final Context m_Context) {
try {
mp = new MediaPlayer();
mp.setDataSource(m_Context, Uri.parse(Contants.audioURL_OR_PATH));
mp.prepare();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setLooping(true);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(m_Context, "Service unavailable this time. Please try again!", Toast.LENGTH_LONG).show();
System.out.println("#####THE EXCEPTION IN THE MEDIA PLAYER PLAY==="+e.getMessage());
}
}
public void mediaPlayStop() {
try {
if (mp.isPlaying()) {
mp.stop();
}
} catch (Exception e) {
// TODO: handle exception
}
}
private static String getSoundPath(int countPositiong) {
// TODO Auto-generated method stub
String aa = "";
try {
if (countPositiong < 10) {
aa = "sounds/00" + countPositiong + ".mp4";
} else if (countPositiong < 100) {
aa = "sounds/0" + countPositiong + ".mp4";
} else {
aa = "sounds/" + countPositiong + ".mp4";
}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("name is : " + aa);
return aa;
}
}