From e9c410d871c044ebf603c29d17ccde56ece3d5f1 Mon Sep 17 00:00:00 2001 From: kaysiz Date: Tue, 19 Feb 2019 15:02:16 +0200 Subject: [PATCH] working on video player --- .idea/misc.xml | 2 +- app/build.gradle | 7 ++ app/src/main/AndroidManifest.xml | 2 + .../com/example/cricket/MainActivity.java | 59 ++++++++++++---- .../java/com/example/cricket/Samples.java | 8 +++ .../java/com/example/cricket/VideoPlayer.java | 67 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 20 ++++++ .../main/res/layout/activity_video_player.xml | 38 +++++++++++ app/src/main/res/values/strings.xml | 3 + 9 files changed, 193 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/example/cricket/Samples.java create mode 100644 app/src/main/java/com/example/cricket/VideoPlayer.java create mode 100644 app/src/main/res/layout/activity_video_player.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 7bfef59..37a7509 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 49deae5..62e4fea 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,4 +25,11 @@ 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 'com.android.support:design:28.0.0' + implementation 'com.android.support:cardview-v7:28.0.0' + implementation 'com.android.support:support-v4:28.0.0' + implementation 'com.android.support:recyclerview-v7:28.0.0' + implementation 'com.google.android.exoplayer:exoplayer-core:2.8.0' + implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.0' + } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8b38324..a10d03e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + @@ -14,6 +15,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> + diff --git a/app/src/main/java/com/example/cricket/MainActivity.java b/app/src/main/java/com/example/cricket/MainActivity.java index 046032c..01c3828 100644 --- a/app/src/main/java/com/example/cricket/MainActivity.java +++ b/app/src/main/java/com/example/cricket/MainActivity.java @@ -2,6 +2,7 @@ import android.Manifest; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Camera; import android.graphics.SurfaceTexture; @@ -19,6 +20,7 @@ import android.os.Environment; import android.os.Handler; import android.os.HandlerThread; +import android.os.SystemClock; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; @@ -30,6 +32,8 @@ import android.view.Surface; import android.view.TextureView; import android.view.View; +import android.widget.Button; +import android.widget.Chronometer; import android.widget.ImageButton; import android.widget.Toast; @@ -88,6 +92,9 @@ public void onOpened(CameraDevice camera) { } startRecord(); mMediaRecorder.start(); + mChronometer.setBase(SystemClock.elapsedRealtime()); + mChronometer.setVisibility(View.VISIBLE); + mChronometer.start(); } else { startPreview(); } @@ -111,7 +118,9 @@ public void onError(CameraDevice camera, int error) { private Size mPreviewSize; private Size mVideoSize; private MediaRecorder mMediaRecorder; + private Chronometer mChronometer; private int mTotalRotation; + private Button mButton; private CaptureRequest.Builder mCaptureRequestBuilder; private ImageButton mRecordImageButton; private boolean mIsRecording = false; @@ -140,31 +149,43 @@ public int compare(Size lhs, Size rhs) { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - UsbManager m = (UsbManager)getApplicationContext().getSystemService(USB_SERVICE); - HashMap devices = m.getDeviceList(); - Collection ite = devices.values(); - UsbDevice[] usbs = ite.toArray(new UsbDevice[]{}); - for (UsbDevice usb : usbs){ - Toast.makeText(getApplicationContext(), usb.getDeviceName(), Toast.LENGTH_SHORT).show(); - } +// UsbManager m = (UsbManager)getApplicationContext().getSystemService(USB_SERVICE); +// HashMap devices = m.getDeviceList(); +// Collection ite = devices.values(); +// UsbDevice[] usbs = ite.toArray(new UsbDevice[]{}); +// for (UsbDevice usb : usbs){ +// Toast.makeText(getApplicationContext(), usb.getDeviceName(), Toast.LENGTH_SHORT).show(); +// } + mButton = findViewById(R.id.button); + mButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openVideoPlayer(); + } + }); + createVideoFolder(); mMediaRecorder = new MediaRecorder(); - mTextureView = (TextureView) findViewById(R.id.textureView); - mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton); + mChronometer = findViewById(R.id.chronometer); + mTextureView = findViewById(R.id.textureView); + mRecordImageButton = findViewById(R.id.videoOnlineImageButton); mRecordImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mIsRecording) { + mChronometer.stop(); + mChronometer.setVisibility(View.INVISIBLE); mIsRecording = false; + mButton.setEnabled(true); mRecordImageButton.setImageResource(R.mipmap.btn_video_online); mMediaRecorder.stop(); mMediaRecorder.reset(); startPreview(); } else { checkWriteStoragePermission(); - Toast.makeText(getApplicationContext(), "Yeah so what?", Toast.LENGTH_SHORT).show(); + mButton.setEnabled(false); } } }); @@ -191,6 +212,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "Application will not run without camera services", Toast.LENGTH_SHORT).show(); } + if (grantResults[1] != PackageManager.PERMISSION_GRANTED) { + Toast.makeText(getApplicationContext(), "Application will not have audio services", Toast.LENGTH_SHORT).show(); + } } if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { @@ -270,7 +294,7 @@ private void connectCamera() { if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) { Toast.makeText(this, "Video app requires access to camera", Toast.LENGTH_SHORT).show(); } - requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_RESULT); + requestPermissions(new String[] {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, REQUEST_CAMERA_PERMISSION_RESULT); } } else { cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler); @@ -401,7 +425,7 @@ private static Size chooseOptimalSize(Size[] choices, int width, int height) { private void createVideoFolder() { File movieFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); - mVideoFolder = new File(movieFile, "codehesionCrikect"); + mVideoFolder = new File(movieFile, "codehesionCricket"); if (!mVideoFolder.exists()) { mVideoFolder.mkdirs(); } @@ -428,6 +452,9 @@ private void checkWriteStoragePermission() { } startRecord(); mMediaRecorder.start(); + mChronometer.setBase(SystemClock.elapsedRealtime()); + mChronometer.setVisibility(View.VISIBLE); + mChronometer.start(); } else { if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(this, "app needs to be able to save videos", Toast.LENGTH_SHORT).show(); @@ -444,6 +471,9 @@ private void checkWriteStoragePermission() { } startRecord(); mMediaRecorder.start(); + mChronometer.setBase(SystemClock.elapsedRealtime()); + mChronometer.setVisibility(View.VISIBLE); + mChronometer.start(); } } private void setupMediaRecorder() throws IOException { @@ -459,4 +489,9 @@ private void setupMediaRecorder() throws IOException { mMediaRecorder.setOrientationHint(mTotalRotation); mMediaRecorder.prepare(); } + + private void openVideoPlayer() { + Intent intent = new Intent(this, VideoPlayer.class); + startActivity(intent); + } } diff --git a/app/src/main/java/com/example/cricket/Samples.java b/app/src/main/java/com/example/cricket/Samples.java new file mode 100644 index 0000000..95e318d --- /dev/null +++ b/app/src/main/java/com/example/cricket/Samples.java @@ -0,0 +1,8 @@ +package com.example.cricket; + +import android.net.Uri; + +public final class Samples { + public static final Uri MP4_URI = Uri.parse("https://storage.googleapis.com/exoplayer-test-media-0/firebase-animation.mp4"); + public static final Uri AD_TAG_URI = Uri.parse("https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpostlongpod&cmsid=496&vid=short_tencue&correlator="); +} diff --git a/app/src/main/java/com/example/cricket/VideoPlayer.java b/app/src/main/java/com/example/cricket/VideoPlayer.java new file mode 100644 index 0000000..a62cc98 --- /dev/null +++ b/app/src/main/java/com/example/cricket/VideoPlayer.java @@ -0,0 +1,67 @@ +package com.example.cricket; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; + +import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.source.ExtractorMediaSource; +import com.google.android.exoplayer2.source.SampleStream; +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; +import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.util.Util; + +public class VideoPlayer extends AppCompatActivity { + private Button mButton; + private PlayerView playerView; + private SimpleExoPlayer player; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_video_player); + playerView = findViewById(R.id.player_view); + mButton = findViewById(R.id.button2); + mButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + backToRecord(); + } + }); + } + + + + @Override + protected void onStart() { + super.onStart(); + + player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector()); + playerView.setPlayer(player); + + DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name))); + + ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory) + .createMediaSource(Samples.MP4_URI); + + player.prepare(mediaSource); + player.setPlayWhenReady(true); + } + + @Override + protected void onStop() { + super.onStop(); + playerView.setPlayer(null); + player.release(); + player = null; + } + + private void backToRecord() { + Intent intent = new Intent(this, MainActivity.class); + startActivity(intent); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 10ba945..48c659b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -15,6 +15,17 @@ android:layout_marginStart="0dp" android:layout_marginTop="0dp" /> + + +