我制作了一个屏幕录制应用程序,但很简单。
我想在切换按钮上添加5秒的延迟,以便在触摸屏幕时看到我的手势,并在完成录制后通过共享按钮分享我的视频,或者直接在Facebook或Youtube上流式传输。
有人知道该怎么做吗?
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1000;
private static final int REQUEST_PERMISSION = 1001;
private static final SparseIntArray ORIENTATION = new SparseIntArray();
private MediaProjectionManager mediaProjectionManager;
private MediaProjection mediaProjection;
private VirtualDisplay virtualDisplay;
private MediaProjectionCallback mediaProjectionCallback;
private MediaRecorder mediaRecorder;
private int mScreenDensity;
private int DISPLAY_WIDTH = 720;
private int DISPLAY_HEIGHT = 1280;
static {
ORIENTATION.append(Surface.ROTATION_0,90);
ORIENTATION.append(Surface.ROTATION_90,0);
ORIENTATION.append(Surface.ROTATION_180,270);
ORIENTATION.append(Surface.ROTATION_270,180);
}
private RelativeLayout rootLayout;
private ToggleButton toggleButton;
private Button play;
private VideoView videoView;
private String videoUri = " ";
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
DISPLAY_HEIGHT = metrics.heightPixels;
DISPLAY_WIDTH = metrics.widthPixels;
mediaRecorder = new MediaRecorder();
mediaProjectionManager = (MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
videoView = (VideoView) findViewById(R.id.videoView);
toggleButton = (ToggleButton) findViewById(R.id.toogleButton);
rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
||
ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.RECORD_AUDIO))
{
toggleButton.setChecked(false);
Snackbar.make(rootLayout,"Permision",Snackbar.LENGTH_INDEFINITE)
.setAction("Enable", new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
},REQUEST_PERMISSION);
}
}).show();
}
else
{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
},REQUEST_PERMISSION);
}
}
else
{
toogleScreenShare(v);
}
}
});
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void toogleScreenShare(View v) {
if(((ToggleButton) v).isChecked())
{
initRecorder();
recordScreen();
}
else
{
mediaRecorder.stop();
mediaRecorder.reset();
stopRecordScreen();
videoView.setVisibility(View.VISIBLE);
videoView.setVideoURI(Uri.parse(videoUri));
videoView.start();
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void recordScreen() {
if(mediaProjection == null)
{
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(),REQUEST_CODE);
return;
}
virtualDisplay = createVirtualDisplay();
mediaRecorder.start();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private VirtualDisplay createVirtualDisplay() {
return mediaProjection.createVirtualDisplay("MainActivity",DISPLAY_WIDTH,DISPLAY_HEIGHT,mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mediaRecorder.getSurface(),null,null);
}
private void initRecorder() {
try {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
videoUri = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ new StringBuilder("/EDMTRecord_").append(new SimpleDateFormat("dd-MM-yyyy-hh_mm_ss")
.format(new Date())).append(".mp4").toString();
mediaRecorder.setOutputFile(videoUri);
mediaRecorder.setVideoSize(DISPLAY_WIDTH,DISPLAY_HEIGHT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncodingBitRate(512*1000);
mediaRecorder.setVideoFrameRate(30);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int orientation = ORIENTATION.get(rotation+90);
mediaRecorder.setOrientationHint(orientation);
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode != REQUEST_CODE)
{
Toast.makeText(this,"Unk error",Toast.LENGTH_SHORT).show();
return;
}
if(resultCode != RESULT_OK)
{
Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT).show();
toggleButton.setChecked(false);
return;
}
mediaProjectionCallback = new MediaProjectionCallback();
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode,data);
mediaProjection.registerCallback(mediaProjectionCallback,null);
virtualDisplay = createVirtualDisplay();
mediaRecorder.start();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private class MediaProjectionCallback extends MediaProjection.Callback {
@Override
public void onStop() {
if(toggleButton.isChecked())
{
toggleButton.setChecked(false);
mediaRecorder.stop();
mediaRecorder.reset();
}
mediaProjection = null;
stopRecordScreen();
super.onStop();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void stopRecordScreen() {
if(virtualDisplay == null)
return;
virtualDisplay.release();
destroyMediaProjection();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void destroyMediaProjection() {
if(mediaProjection != null)
{
mediaProjection.unregisterCallback(mediaProjectionCallback);
mediaProjection.stop();
mediaProjection = null;
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case REQUEST_PERMISSION:
{
if((grantResults.length>0) && (grantResults[0] + grantResults[1] == PackageManager.PERMISSION_GRANTED))
{
toogleScreenShare(toggleButton);
toggleButton.setEnabled(false);
toggleButton.findViewById(R.id.toogleButton).setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toggleButton.findViewById(R.id.toogleButton).setEnabled(true);
}
},10000);
}
else
{
toggleButton.setChecked(false);
Snackbar.make(rootLayout,"Permision",Snackbar.LENGTH_INDEFINITE)
.setAction("Enable", new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
},REQUEST_PERMISSION);
}
}).show();
}
return;
}
}
}
}
答案 0 :(得分:0)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//code in this segment run after 5s or 5000 milliseconds
}
}, 5000);
它是一个简单的处理程序,可用于延迟。可能会对您有所帮助。
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app to share"));
此处的内容Uri表示录制的视频的Uri。