我尝试为android编写一个简单的屏幕录像机。录音有效,但我无法阻止它。由于“--time-limit”选项,它会在30秒后自动停止,但如果按下停止按钮则不会。 这是我的代码:
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Button buttonStart;
private Button buttonStop;
private Process process;
private DataOutputStream stream;
private int pid = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
record("/storage/sdcard1/screencast-" + getFilename() + ".mp4");
}
});
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("Stopping ...");
try {
stop();
showToast("Stopped.");
} catch (Exception e) {
showToast("Failed to stop recording.");
}
}
});
}
private void record(final String path) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try{
process = Runtime.getRuntime().exec("su");
stream = new DataOutputStream(process.getOutputStream());
stream.writeBytes("echo $$\n");
stream.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
pid = Integer.parseInt(bufferedReader.readLine());
bufferedReader.close();
// just using "--time-limit 30" because i don´t want to restart to stop recording
stream.writeBytes("screenrecord --time-limit 30 " + path + "\n");
stream.flush();
stream.writeBytes("exit\n");
stream.close();
process.waitFor();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}.execute();
}
private void stop() throws IOException {
if(pid != -1) {
// both following lines don't work
android.os.Process.killProcess(pid);
android.os.Process.sendSignal(pid, android.os.Process.SIGNAL_QUIT);
if(process != null) {
process.destroy();
}
}
}
private String getFilename() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Calendar now = Calendar.getInstance();
return formatter.format(now.getTime());
}
private void showToast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="me.bleuzen.screenrecorder.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/buttonStart"
android:layout_above="@+id/buttonStop"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/buttonStop"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>