Skip to content

Commit

Permalink
working on video player
Browse files Browse the repository at this point in the history
  • Loading branch information
kaysiz committed Feb 19, 2019
1 parent d9472cd commit e9c410d
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cricket">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand All @@ -14,6 +15,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".VideoPlayer"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
59 changes: 47 additions & 12 deletions app/src/main/java/com/example/cricket/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -88,6 +92,9 @@ public void onOpened(CameraDevice camera) {
}
startRecord();
mMediaRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.setVisibility(View.VISIBLE);
mChronometer.start();
} else {
startPreview();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, UsbDevice> devices = m.getDeviceList();
Collection<UsbDevice> 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<String, UsbDevice> devices = m.getDeviceList();
// Collection<UsbDevice> 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);
}
}
});
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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);
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/example/cricket/Samples.java
Original file line number Diff line number Diff line change
@@ -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=");
}
67 changes: 67 additions & 0 deletions app/src/main/java/com/example/cricket/VideoPlayer.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
android:layout_marginStart="0dp"
android:layout_marginTop="0dp" />

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/videoOnlineImageButton"
android:layout_marginStart="2dp"
android:layout_marginBottom="32dp"
android:textColor="#ff0000"
android:textSize="25sp"
android:visibility="invisible" />

<ImageButton
android:id="@+id/videoOnlineImageButton"
android:layout_width="wrap_content"
Expand All @@ -25,4 +36,13 @@
android:contentDescription="@string/video_button"
app:srcCompat="@mipmap/btn_video_online" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="73dp"
android:layout_marginBottom="373dp"
android:text="Video Player" />
</RelativeLayout>
38 changes: 38 additions & 0 deletions app/src/main/res/layout/activity_video_player.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoPlayer">

<!--<com.google.android.exoplayer2.ui.PlayerView-->
<!--android:id="@+id/player_view"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--app:use_controller="true"-->
<!--app:layout_constraintBottom_toBottomOf="parent"-->
<!--app:layout_constraintEnd_toEndOf="parent"-->
<!--app:layout_constraintStart_toStartOf="parent"-->
<!--app:layout_constraintTop_toTopOf="parent" />-->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerviewVideo"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="47dp"
android:layout_marginBottom="43dp"
android:text="@string/record" />

</RelativeLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<resources>
<string name="app_name">Cricket</string>
<string name="video_button">Video Record Button</string>
<string name="video_player">Video Player</string>
<string name="hello_there">Hello there</string>
<string name="record">Record</string>
</resources>

0 comments on commit e9c410d

Please sign in to comment.