I was learning how to make android apps with Android Studio with the help of some video courses.
As a part of the curriculum there was a challenge to make an app which could play a video file. I was able to complete the challenge. I made an app that could play the video of the song - Believer by Imagine Dragons.
Here I'll show how i made that.
- Create a folder named "raw" in the resources folder and copy the video file to that folder.
2.Then add a VideoView in the layout file.
3.Then in the java file of the activity, we should write just 6 lines of code to make the video play, along with a media controller which gives you control to play, pause, forward, and seek the video.
The explanation -
We declare a VideoView object and assign the file we copied to it.
Then create a MediaController object and link it with the VideoView object we just made.
Link the VideoView object to the MediaController object (Yes, it has to be done in both the ways)
Call the start() function of the VideoView object.
I have given the code here as well.
MainActivity.java :-
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.believer);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-5dp" />
</android.support.constraint.ConstraintLayout>
Now run the app and you'll get a neat little video player in your phone.
After mastering all the basic concepts I am looking forward to make a multimedia player app for myself!
Image Source - Self