Code Your Own Video App Player in Android Studio!

@jerrybanfield · 2019-09-11 17:25 · steempress

video-player-android-studio.jpg


Hello and welcome to this new Android development course. This is part one of how to make a video player application for Android.

We’re going to start by opening our Android Studio environment and clicking on 'Start a new Android Studio project'. We will select an empty activity, and then we will click on next.

Code Your Own Video App Player in Android Studio!

Here we change the name of the application to video player. We make sure the language that is selected is the Java programming language and the minimum API level is at least 16.

We will click on Finish and we will wait for the project to be loaded. This project, this application will basically do two simple things. The first one is getting all the mp4 files or other compatible video files, and then it will enable the user to play those files using the default video view widget which is the default way to play video in the Android operating system.

Code Your Own Video App Player in Android Studio!

Right now, as you can see the project has finished loading and the first thing we’re going to do is to change the root element of our XML file.

https://youtu.be/e84sk_SHdog

If you will enjoy reading and contributing to the discussion for this post, will you please join us on the YouTube video above and leave a comment there because I read and respond to most comments on YouTube?

If you find anything helpful in this video or funny, will you please leave a like because you will feel great helping other people find it?

For that we will go to the left under the res folder and under the layout folder. We will find the activity_main.xml file. We’ll right click on it and then click on delete.

If you have these options checked please uncheck them and press in OK.

Code Your Own Video App Player in Android Studio!

We have deleted that layout file as you can see we have an arrow right here which is normal, and now we will create it again but with a different root element.

For that, click right on the layout folder here on the left. Right click here, go to the new and layout resource file.

As the filename we will write the same it was previously which is activity_main just like this. We will change the root element to a linear layout as you can see right here.

Code Your Own Video App Player in Android Studio!

We will click on OK and as you can see here we have a new linear layout XML file. If we switch to text mode you can see that we have this linear layout as the root element of our layout.

Code Your Own Video App Player in Android Studio!

If we go back to our main_activity.java, we can see that these red warning disappears which is cool. For this tutorial, I will be running the application on an emulator. You can do it in an emulator too or you can do it on a real device.

Right now, I’m starting the emulator in order for us to make it faster in future app launches. The emulator is starting right now. I will leave it to load in the background.

Code Your Own Video App Player in Android Studio!

It’s loading right there and as you can see, I’ve sent the app to the device but now, we will leave the emulator aside for a moment, and we will focus on our application.

As I told you we have to first be able to get the files. For that, we will go into our manifest file right here and we will have the read and write files permission.

Code Your Own Video App Player in Android Studio!

We will go ahead and start by saying user’s permission and we will specify the read and write permission for our storage. The IDE doesn’t auto-generated these for me because it’s loading something else. Right now, it’s installing the APK but now that it has finished installing it, I can start getting the help of the IDE right here. You go say users permission and we are going to type WRITE_EXTERNAL_STORAGE as you can see, and after that we are going to go and say user’s permission READ_EXTERNAL_STORAGE like that.

Code Your Own Video App Player in Android Studio!

We have this right here and then the activity right here. Right here we will also set the screen orientation for the portrait because that’s the one we will be using.

Code Your Own Video App Player in Android Studio!

I will run the app on the emulator to see if it works or maybe it’s not working. I’m compiling it, I’m installing the APK as you can see and soon it will run here on the left.

As you can see it is a simple thing that has nothing on it. It only has the video player title and also has the status bar title. I will remove both of those bars. To remove those bars, we go under the res folders and under the value’s folders, we open this style.xml file. Right here we can modify our style.

Code Your Own Video App Player in Android Studio!

We will start by typing item windowNoTitle, and we will set these values to the truth as you can see, and we will open another item. Here we say windowFullscreen. We will also set these to true.

Code Your Own Video App Player in Android Studio!

With only those two lines we have removed the bars so I will run it to test if that works well. I am compiling an app. It’s now sending it to the emulator as you can see. It’s empty. Everything is just a white rectangle.

Code Your Own Video App Player in Android Studio!

The title bars - the previous two bars were removed. We went back. We can now close the style file. We can now close this manifest file and we will have to create a way in which we can ask the user for permissions. If we are targeting Marshmallow or higher which is pretty common these days, we have to make sure that we ask for permissions, so that’s a very, very important thing to keep in mind.

We will be asking for permissions and creating a dialogue that will ask hey, we will use this permission and the user will click and enable or allow or not allow the permission. This is the onCreate method.

Right now we are in the main_activity. java file. After that we will start actually coding. The first thing is a code that will ask for the permission so the permission request code for that will define it as private static int. Actually, private static final because this will be fine value, so private static final int, and we will call it in uppercase REQUEST_ PERMISSIONS, and we will set this body to any identifier.

Code Your Own Video App Player in Android Studio!

For example, I would set it to one, two, three, four, okay?

Then we will create an array of strings that will specify the actual permissions that we will ask the user to grant. We’ll go and say private static final but now string - this is string array - so we specify an array right here and we will call this PERMISSIONS as you can see. We open brackets and we end the line with a semicolon. Inside here we will go and type Manifest.permission.READ_EXTERNAL_STORAGE. As you can see will type a comma and the next line will type Manifest.permission.WRITE_EXTERNAL_STORAGE.

Code Your Own Video App Player in Android Studio!

Now, we have specified the request code and the permissions we want to request. We also have to specify the number of permissions, in this case, we’re asking for two, so we’re going to go ahead and type private static final int PERMISSIONS_COUNT. We will set these to two.

Code Your Own Video App Player in Android Studio!

Now, we will ask for permissions and we will preparing the method. The first method that we will use is to check if the user denied any permission. For that we will create the following method, private boolean which we’ll return true or false. Whether the permissions are denied or not. So we will call this arePermissionsDenied as you can see right here. Inside this, we will write a for loop so we go ahead and type for, we open parenthesis and type int i=0; and then we go and say i < PERMISSIONS_COUNT. So we will check this for every permission. Then i++ as you can see right here.

Code Your Own Video App Player in Android Studio!

Inside this for loop, we will check if the user has granted permission or not, so for that we go ahead and say if checkSelfPermission. We will pass the current permissions which is permission at index i. If checkSelfPermission is different than PackageManager.PERMISSIONS_GRANTED. So basically here, what we are saying is if the permission is not granted then do something, okay? Pretty easy, right?

Code Your Own Video App Player in Android Studio!

If the permission is not granted, we will return it true because we are returning the permissions are not granted. That’s the name of the method. arePermissionsDenied? So if they are not granted they are denied.

After the for loop we, of course, have to return false. This means there was no permission denied. Every permission was allowed.

Code Your Own Video App Player in Android Studio!

As you can see here we have a warning that says call requires API level 23, but we don’t really care about this warning because we will call it only when API level is 23 or later, so we can go ahead and say suppressLint new API annotation. Now, the warning is gone.

Code Your Own Video App Player in Android Studio!

This is the first method to check the permissions. We have to also check for another method which will be called when the user clicks denied or allow permission. We’re going to type override public void onRequestPermissionsResult. As parameters, we’ll go with say final int requestCode. So this is the code that we passed.

Code Your Own Video App Player in Android Studio!

We will check if this gets passed the code that we passed previously. That’s the request code. Final int requestCode. Final string[] permissions and I’ll go to the next line and say also final int[] grantResult. These are kind of the parameters that will be passed to this method.

Code Your Own Video App Player in Android Studio!

We are overriding it so we will have to call the super method. For that we’re going to say super.onRequestPermissionResult, and we’ll pass the same parameters. So the first one will be requestCode. The second parameter we will pass will be permissions, and the third will be grantResult as you can see.

Code Your Own Video App Player in Android Studio!

Now, we want to go and check if the request code is actually the same that we sent and we will send these code right here which is one, two, three, four, which is called REQUEST_PERMISSIONS.

Code Your Own Video App Player in Android Studio!

We’re going to go and say if requestCode == REQUEST_PERMISSIONS. We also check if and grantResults.length is greater than zero which checks if the results are not empty. Inside here we will have to check if the permissions are denied or not, so we’re going to say if arePermissionsDenied, so we call the method that we created previously. Here what we will do if the permissions are denied, well, the app cannot work without these permissions, and if the user denies the permissions then the app is not useful. So the user should allow the permissions in order to use the app. Here as a quick fix we will open two parentheses. Inside the inner one we’re going to type ActivityManager. We go outside this and type objects.requireNonNulll and we’ll type these.getSystemService as you can see.

Code Your Own Video App Player in Android Studio!

What this is doing is basically telling the system to delete the application user data in order to enable us to keep asking for the permission every time the user launches the app. It’s just a quick fix. So we’re going to say ACTIVITY_SERVICE. We’ll pass the parameter right here and after these three parentheses we’re going to type .clearApplicationUserData. As you can see here it has a kind of warning again that requires API 19 but we will only call this if API is greater than 23 actually.

Code Your Own Video App Player in Android Studio!

We go and click suppressLint warning.

That happens if the user denies this and to close the app we will just type recreate. Right here what will happen if the user actually enables or allows access for those permissions, we will call the onResume method as you can see right here.

Code Your Own Video App Player in Android Studio!

Since we called the onResume method we have to override that and that method will actually be the main method of our activity. For that, we’re going to go after these and type @override then protected void onResume. We will open this and type super.onResume.

Here is when we do the check of the Android version. To check the current version the Android we will type the following. Build.VERSION.SDK_INT. So if Build.VERSION.SDK_INT is greater or equal than Build.VERSION_CODE.M which means Marshmallow. We check first if it’s Marshmallow or above and we also check something else, and arePermissionsDenied right here. We will request permissions. So we’ll type requestPermissions and we will pass permissions and the request permission code which is REQUEST_PERMISSIONS as you can see. And after this, we will return. As simple as that.

Code Your Own Video App Player in Android Studio!

We also have to check this every time the onResume is called because the user may disable the permissions for our application. This is basically how we do it. Now, we’re going to check if the app was initialized because the onResume method will be called many times and we just have to initialize the app ones. For that, we will create a flag

#androidstudiotutorial #codeandroidvideoplayer #codeinandroidstudio #javatutorial
Payout: 0.000 HBD
Votes: 889
More interactions (upvote, reblog, reply) coming soon.