Skip to content

Commit

Permalink
Added example for #169
Browse files Browse the repository at this point in the history
  • Loading branch information
HBiSoft committed Aug 13, 2024
1 parent ad960c6 commit c23baee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Binary file modified app/release/HBRecorderDemo.apk
Binary file not shown.
31 changes: 29 additions & 2 deletions app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ private void startRecordingScreen() {
startbtn.setText(R.string.stop_recording);
}

String output_format;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
// Example of how to set custom settings
private void customSettings() {
Expand Down Expand Up @@ -493,7 +494,7 @@ private void customSettings() {
}

//Output Format
String output_format = prefs.getString("key_output_format", null);
output_format = prefs.getString("key_output_format", null);
if (output_format != null) {
switch (output_format) {
case "0":
Expand Down Expand Up @@ -646,7 +647,7 @@ private void setOutputPath() {
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "HBRecorder");
contentValues.put(MediaStore.Video.Media.TITLE, filename);
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, getMimeTypeForOutputFormat(output_format));
mUri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
//FILE NAME SHOULD BE THE SAME
hbRecorder.setFileName(filename);
Expand All @@ -657,6 +658,32 @@ private void setOutputPath() {
}
}

// Passing the MIME_TYPE to ContentValues() depending on what output format was selected
// This is just to demonstrate for the demo app - more can be added
private String getMimeTypeForOutputFormat(String outputFormat) {
String mimetype = "video/mp4";
switch (outputFormat) {
// We do not know what the devices DEFAULT (0) is
// For the sake of this demo app we will set it to mp4
case "0":
mimetype = "video/mp4";
break;
case "1":
mimetype = "video/mp4";
break;
case "2":
mimetype = "video/3gpp";
break;
case "3":
mimetype = "video/webm";
break;
default:
mimetype = "video/mp4";
break;
}
return mimetype;
}

//Generate a timestamp to be used as a file name
private String generateFileName() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class ScreenRecordService extends Service {
private boolean isAudioEnabled;
private String path;

private String outputFormat;

private MediaProjection mMediaProjection;
private MediaRecorder mMediaRecorder;
private VirtualDisplay mVirtualDisplay;
Expand Down Expand Up @@ -168,7 +170,7 @@ else if (intent.getAction().equals("resume")) {
filePath = name;
audioBitrate = intent.getIntExtra("audioBitrate", 128000);
audioSamplingRate = intent.getIntExtra("audioSamplingRate", 44100);
String outputFormat = intent.getStringExtra("outputFormat");
outputFormat = intent.getStringExtra("outputFormat");
if (outputFormat != null) {
setOutputFormatAsInt(outputFormat);
}
Expand Down Expand Up @@ -402,6 +404,27 @@ private void setOutputFormatAsInt(String outputFormat) {
}
}

private String getExtension(String outputFormat) {
switch (outputFormat) {
case "THREE_GPP":
return ".3gp";
case "AMR_NB":
return ".amr";
case "AMR_WB":
return ".amr";
case "AAC_ADTS":
return ".aac";
case "MPEG_2_TS":
return ".ts";
case "WEBM":
return ".webm";
case "OGG":
return ".ogg";
default:
return ".mp4"; // Default to .mp4 for unknown formats
}
}

//Set video encoder as int based on what developer has provided
//It is important to provide one of the following and nothing else.
private void setvideoEncoderAsInt(String encoder) {
Expand Down Expand Up @@ -549,6 +572,13 @@ private void initRecorder() throws Exception {
}
}
}else{
if (outputFormat!=null){
filePath = path + "/" + name + getExtension(outputFormat);
fileName = name + getExtension(outputFormat);
}else {
filePath = path + "/" + name + ".mp4";
fileName = name + ".mp4";
}
mMediaRecorder.setOutputFile(filePath);
}
mMediaRecorder.setVideoSize(mScreenWidth, mScreenHeight);
Expand Down

0 comments on commit c23baee

Please sign in to comment.