Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloHuDi committed May 15, 2018
1 parent eeff687 commit 10164df
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 64 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</application>

</manifest>
25 changes: 24 additions & 1 deletion app/src/main/java/com/hd/screen/capture/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.hd.screencapture.ScreenCapture;
import com.hd.screencapture.ScreenCaptureConfig;

public class MainActivity extends AppCompatActivity {

Expand All @@ -14,7 +17,27 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenCapture = ScreenCapture.with(this);
init();
}

private void init() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int dpi = metrics.densityDpi;
final int width = metrics.widthPixels;
final int height = metrics.heightPixels;
final int bitrate = 6000000;
Log.d("tag", "current device +" + dpi + "==" + width + "==" + height);
ScreenCaptureConfig captureConfig = new ScreenCaptureConfig.Builder()//
.setAudio(false)//
.setDpi(dpi)//
.setWidth(width)//
.setHeight(height)//
.setBitrate(bitrate)//
.setFrameRate(60)//
.setIFrameInterval(10)//
.create();//
screenCapture = ScreenCapture.with(this).setConfig(captureConfig);
}

public void startCapture(View view) {
Expand Down
2 changes: 1 addition & 1 deletion screencapture/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:common-java8:1.1.1"
implementation "android.arch.lifecycle:extensions:1.1.1"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hd.screencapture;

/**
* Created by hd on 2018/5/15 .
*/
public abstract class CaptureObserver {

private ScreenCapture screenCapture;

volatile boolean alive;

CaptureObserver(ScreenCapture screenCapture) {
this.screenCapture = screenCapture;
alive=true;
}

public boolean isAlive(){
return alive;
}

public void stopCapture(){
screenCapture.stopCapture();
alive=false;
screenCapture=null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ public class ScreenCapture {
private final String TAG = "Screen-Capture";

public static ScreenCapture with(@NonNull AppCompatActivity activity) {
if(activity.isFinishing() || activity.isDestroyed()){
if (activity.isFinishing() || activity.isDestroyed()) {
throw new RuntimeException("current activity is not running state !");
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
throw new RuntimeException("the sdk version less than 21 equipment does not provide this function");
}
return new ScreenCapture(activity);
}

private ScreenCaptureFragment screenCaptureFragment;

private ScreenCapture(@NonNull AppCompatActivity activity) {
//add lifecycle observer
activity.getLifecycle().addObserver(new ScreenCaptureObserver(this));
//init the main capture work fragment
ScreenCaptureObserver observer = new ScreenCaptureObserver(this);
activity.getLifecycle().addObserver(observer);
//init the main capture work fragment7
screenCaptureFragment = getScreenCaptureFragment(activity);
screenCaptureFragment.addObserver(observer);
//init default config
setConfig(ScreenCaptureConfig.initDefaultConfig());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ public class ScreenCaptureConfig {
/**
* video file
*/
private File file;
private File file = new File(Environment.getExternalStorageDirectory(), "screen_capture_" + System.currentTimeMillis() + ".mp4");

/**
* video width and height
*/
private int width = 1280, height = 1920;
private int width = 1080, height = 1920;

/**
* device dpi
*/
private int dpi=480;
private int dpi = 480;

/**
* video bitrate
*/
private int bitrate = 60000;
private int bitrate = 25000;

/**
* video frame rate
Expand All @@ -44,19 +44,8 @@ public class ScreenCaptureConfig {
*/
private int iFrameInterval = 10;


public static ScreenCaptureConfig initDefaultConfig() {
return new ScreenCaptureConfig(true);
}

public ScreenCaptureConfig() {
this(false);
}

private ScreenCaptureConfig(boolean dfConfig) {
if (dfConfig) {
setFile(new File(Environment.getExternalStorageDirectory(), "screen_capture_" + System.currentTimeMillis() + ".mp4"));
}
return new ScreenCaptureConfig();
}

public boolean hasAudio() {
Expand Down Expand Up @@ -115,11 +104,64 @@ public void setFrameRate(int frameRate) {
this.frameRate = frameRate;
}

public int getiFrameInterval() {
public int getIFrameInterval() {
return iFrameInterval;
}

public void setiFrameInterval(int iFrameInterval) {
public void setIFrameInterval(int iFrameInterval) {
this.iFrameInterval = iFrameInterval;
}

public static class Builder {

private ScreenCaptureConfig captureConfig;

public Builder() {
this.captureConfig = new ScreenCaptureConfig();
}

public Builder setAudio(boolean audio) {
captureConfig.setAudio(audio);
return this;
}

public Builder setFile(File file) {
captureConfig.setFile(file);
return this;
}

public Builder setWidth(int width) {
captureConfig.setWidth(width);
return this;
}

public Builder setHeight(int height) {
captureConfig.setHeight(height);
return this;
}

public Builder setDpi(int dpi) {
captureConfig.setDpi(dpi);
return this;
}

public Builder setBitrate(int bitrate) {
captureConfig.setBitrate(bitrate);
return this;
}

public Builder setFrameRate(int frameRate) {
captureConfig.setFrameRate(frameRate);
return this;
}

public Builder setIFrameInterval(int iFrameInterval) {
captureConfig.setIFrameInterval(iFrameInterval);
return this;
}

public ScreenCaptureConfig create() {
return captureConfig;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ScreenCaptureFragment extends Fragment {

private Context appContext;

private CaptureObserver observer;

private String[] permissions1 = {Manifest.permission.WRITE_EXTERNAL_STORAGE};

private String[] permissions2 = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO};
Expand All @@ -46,21 +48,29 @@ public class ScreenCaptureFragment extends Fragment {

private ScreenCaptureConfig config;

public ScreenCaptureFragment() {
public void addObserver(CaptureObserver observer) {
this.observer = observer;
}

public void setConfig(@NonNull ScreenCaptureConfig config) {
this.config = config;
}

public void startCapture() {
if (hasPermissions()) {
Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, REQUEST_MEDIA_PROJECTION);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
if (observer.isAlive()) {
if (hasPermissions()) {
Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, REQUEST_MEDIA_PROJECTION);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
} else {
Log.e(TAG, "fuck,how can this happened !");
}
}
} else {
Log.e(TAG, "current activity is not alive state !!!");
stopCapture();
}
}

Expand All @@ -87,7 +97,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
if (granted == PackageManager.PERMISSION_GRANTED) {
startCapture();
} else {
Log.d(TAG, "No Permission!");
Log.e(TAG, "No Permission!");
}
}
}
Expand All @@ -110,8 +120,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}

private void startRecorder() {
screenCaptureRecorder = new ScreenCaptureRecorder(mediaProjection, config);
screenCaptureRecorder.startCapture();
if (observer.isAlive()) {
screenCaptureRecorder = new ScreenCaptureRecorder(mediaProjection, config);
screenCaptureRecorder.addObserver(observer);
screenCaptureRecorder.startCapture();
getActivity().moveTaskToBack(true);
} else {
Log.e(TAG, "start recorder failed ,current activity is not alive state !!!");
stopCapture();
}
}

private void cancelRecorder() {
Expand Down Expand Up @@ -146,5 +163,4 @@ private boolean hasPermissions() {
PackageManager.PERMISSION_GRANTED) | pm.checkPermission(WRITE_EXTERNAL_STORAGE, packageName);
return granted == PackageManager.PERMISSION_GRANTED;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,23 @@
/**
* Created by hd on 2018/5/14 .
*/
public class ScreenCaptureObserver implements LifecycleObserver {
public class ScreenCaptureObserver extends CaptureObserver implements LifecycleObserver {

private final String TAG = ScreenCaptureObserver.class.getSimpleName();

private ScreenCapture screenCapture;

public ScreenCaptureObserver(ScreenCapture screenCapture) {
this.screenCapture = screenCapture;
super(screenCapture);
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void onResume() {
Log.d(TAG, "onResume");
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void onPause() {
Log.d(TAG, "onPause");
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onStop() {
Log.d(TAG, "onStop");
alive=true;
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void onDestroy() {
Log.d(TAG, "onDestroy");
alive=false;
}

}
Loading

0 comments on commit 10164df

Please sign in to comment.