集成短视频功能

serial
BlueMatthew 1 year ago
parent b7fbec6d12
commit 1bede89a8c

@ -186,7 +186,7 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa
mRegisterHeartbeatMid = env->GetMethodID(classService, "registerHeartbeatTimer", "(I)V"); mRegisterHeartbeatMid = env->GetMethodID(classService, "registerHeartbeatTimer", "(I)V");
mUpdateTimeMid = env->GetMethodID(classService, "updateTime", "(J)Z"); mUpdateTimeMid = env->GetMethodID(classService, "updateTime", "(J)Z");
mUpdateCaptureScheduleMid = env->GetMethodID(classService, "updateCaptureSchedule", "(J)Z"); mUpdateCaptureScheduleMid = env->GetMethodID(classService, "updateCaptureSchedule", "(J)Z");
mStartRecordingMid = env->GetMethodID(classService, "startRecording", "(IJIIII)V"); mStartRecordingMid = env->GetMethodID(classService, "startRecording", "(IJIIIII)V");
mRequestWakelockMid = env->GetMethodID(classService, "requestWakelock", "(Ljava/lang/String;J)V"); mRequestWakelockMid = env->GetMethodID(classService, "requestWakelock", "(Ljava/lang/String;J)V");
mReleaseWakelockMid = env->GetMethodID(classService, "releaseWakelock", "(Ljava/lang/String;)V"); mReleaseWakelockMid = env->GetMethodID(classService, "releaseWakelock", "(Ljava/lang/String;)V");
@ -848,7 +848,8 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<
ALOGE("Failed to get JNI Env"); ALOGE("Failed to get JNI Env");
return false; return false;
} }
env->CallVoidMethod(m_javaService, mStartRecordingMid, mPhotoInfo.cameraId, (unsigned long)mPhotoInfo.photoId, mPhotoInfo.duration, mPhotoInfo.width, mPhotoInfo.height, mPhotoInfo.duration); int orientation = mPhotoInfo.orientation == 0 ? -1 : (mPhotoInfo.orientation - 1) * 90;
env->CallVoidMethod(m_javaService, mStartRecordingMid, mPhotoInfo.cameraId, (unsigned long)mPhotoInfo.photoId, mPhotoInfo.duration, mPhotoInfo.width, mPhotoInfo.height, mPhotoInfo.duration, orientation);
if (didAttachThread) if (didAttachThread)
{ {

@ -476,7 +476,7 @@ public class MicroPhotoService extends Service {
registerPhotoTimer(getApplicationContext(), scheduleTime, scheduleTime, timeout, schedules); registerPhotoTimer(getApplicationContext(), scheduleTime, scheduleTime, timeout, schedules);
} }
public void startRecording(int cameraId, long videoId, int duration, int width, int height, int quality) { public void startRecording(int cameraId, long videoId, int duration, int width, int height, int quality, int orientation) {
Context context = getApplicationContext(); Context context = getApplicationContext();
Intent intent = new Intent(this, VideoActivity.class); Intent intent = new Intent(this, VideoActivity.class);
@ -486,6 +486,7 @@ public class MicroPhotoService extends Service {
intent.putExtra("width", width); intent.putExtra("width", width);
intent.putExtra("height", height); intent.putExtra("height", height);
intent.putExtra("quality", quality); intent.putExtra("quality", quality);
intent.putExtra("orientation", orientation);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); startActivity(intent);

@ -16,28 +16,12 @@ public class VideoActivity extends AppCompatActivity {
setContentView(R.layout.activity_video); setContentView(R.layout.activity_video);
if (null == savedInstanceState) { if (null == savedInstanceState) {
Bundle bundle = new Bundle();
Intent intent = getIntent(); Intent intent = getIntent();
int duration = intent.getIntExtra("duration", 0);
if (intent.hasExtra("cameraId")) {
bundle.putInt("cameraId", intent.getIntExtra("cameraId", 0));
}
if (intent.hasExtra("videoId")) {
bundle.putLong("videoId", intent.getLongExtra("videoId", 0));
}
bundle.putInt("duration", duration);
String act = intent.getStringExtra("action");
if (act != null) {
bundle.putString("action", act);
}
bundle.putInt("width", intent.getIntExtra("width", 0));
bundle.putInt("height", intent.getIntExtra("height", 0));
Fragment fragment = VideoFragment.newInstance(); Fragment fragment = VideoFragment.newInstance();
Bundle bundle = intent.getExtras();
if (bundle != null) {
fragment.setArguments(bundle); fragment.setArguments(bundle);
}
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment) .replace(R.id.container, fragment)
.commit(); .commit();

@ -161,6 +161,8 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med
*/ */
private Size mVideoSize; private Size mVideoSize;
private int mOrientation = -1;
/** /**
* MediaRecorder * MediaRecorder
*/ */
@ -280,12 +282,21 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med
* @param choices The list of available sizes * @param choices The list of available sizes
* @return The video size * @return The video size
*/ */
private static Size chooseVideoSize(Size[] choices) { private static Size chooseVideoSize(Size[] choices, Size expectedSize) {
if (expectedSize == null || expectedSize.getWidth() <= 0 || expectedSize.getHeight() <= 0) {
for (Size size : choices) { for (Size size : choices) {
if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) { if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
return size; return size;
} }
} }
} else {
for (Size size : choices) {
if (size.equals(expectedSize)) {
return size;
}
}
}
Log.e(TAG, "Couldn't find any suitable video size"); Log.e(TAG, "Couldn't find any suitable video size");
return choices[choices.length - 1]; return choices[choices.length - 1];
} }
@ -342,6 +353,12 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med
mCameraId = Integer.toString(argument.getInt("CameraId", 0)); mCameraId = Integer.toString(argument.getInt("CameraId", 0));
mVideoId = argument.getLong("videoId", 0); mVideoId = argument.getLong("videoId", 0);
mDuration = argument.getInt("duration", 0); mDuration = argument.getInt("duration", 0);
int width = argument.getInt("width", 0);
int height = argument.getInt("height", 0);
mOrientation = argument.getInt("orientation", -1);
if (width > 0 && height > 0) {
mVideoSize = new Size(width, height);
}
} }
Log.i(TAG, "Recv recording request CameraId=" + mCameraId + " videoId=" + Long.toString(mVideoId)); Log.i(TAG, "Recv recording request CameraId=" + mCameraId + " videoId=" + Long.toString(mVideoId));
@ -484,7 +501,7 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med
if (map == null) { if (map == null) {
throw new RuntimeException("Cannot get available preview/video sizes"); throw new RuntimeException("Cannot get available preview/video sizes");
} }
mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class)); mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class), mVideoSize);
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
width, height, mVideoSize); width, height, mVideoSize);
@ -627,19 +644,14 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med
if (null == activity) { if (null == activity) {
return; return;
} }
try { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
} catch (Exception ex) {
}
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) { if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
mNextVideoAbsolutePath = getVideoFilePath(getActivity()); mNextVideoAbsolutePath = getVideoFilePath(getActivity());
} }
mMediaRecorder.setOutputFile(mNextVideoAbsolutePath); mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
mMediaRecorder.setVideoEncodingBitRate(10000000); mMediaRecorder.setVideoEncodingBitRate(1024 * 1024 * 8);
mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
@ -647,10 +659,16 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med
if (mDuration > 0) { if (mDuration > 0) {
mMediaRecorder.setMaxDuration(mDuration * 1000); mMediaRecorder.setMaxDuration(mDuration * 1000);
} }
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientationAdjustment = 0;
if (mOrientation != -1) {
orientationAdjustment = mOrientation / 90;
}
int rotation = (activity.getWindowManager().getDefaultDisplay().getRotation() + orientationAdjustment) % 4;
int orientationHint = 0;
switch (mSensorOrientation) { switch (mSensorOrientation) {
case SENSOR_ORIENTATION_DEFAULT_DEGREES: case SENSOR_ORIENTATION_DEFAULT_DEGREES:
mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation)); orientationHint = DEFAULT_ORIENTATIONS.get(rotation);
mMediaRecorder.setOrientationHint(orientationHint);
break; break;
case SENSOR_ORIENTATION_INVERSE_DEGREES: case SENSOR_ORIENTATION_INVERSE_DEGREES:
mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation)); mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));

Loading…
Cancel
Save