1
我正在开发一个录音应用程序......问题是我不知道如何使它在后台工作。我的意思是,即使用户关闭应用程序记录器保持工作..如果它达到最大..它删除文件并再次记录..永远的媒体录制
用户只应该按一下按钮,应用程序应该永远工作直到用户卸载程序。
我的代码如下:
public class MainActivity extends Activity {
private MediaRecorder recorder ;
Button B ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recorder = new MediaRecorder();
B = (Button)findViewById(R.id.button1) ;
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Thread startThread = new Thread (new startRe (recorder));
startThread.start() ;
B.setEnabled(false);
}
}
);
}
}
唱片类:
public class startRe implements Runnable {
private MediaRecorder recorder;
startRe(MediaRecorder r) {
this.recorder = r ;
}
public void run() {
// TODO Auto-generated method stub
startRecording();
}
public void startRecording() {
//recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilePath());
try{
recorder.prepare();
recorder.start();
}catch (Exception e){
e.printStackTrace();
}
}
private String getFilePath() {
String filePath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filePath, "MediaRecorderSample");
if(!file.exists())
file.mkdirs();
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp4");
}
}
现在的问题是如何使它永远以及如何当它到达删除最大尺寸。 此外,使用此代码,我不知道它是否真的录音或因为我找不到输出文件。