How to play videos on android

How to Play Video from URL in Android?

In this article, you will see how to play a video from URL on Android. For showing the video in our android application we will use the VideoView widget. The VideoView widget is capable of playing media files, and the formats supported by the VideoView are 3gp and MP4. By using VideoView you can play media files from the local storage and also from the internet. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Now let’s see the step-by-step implementation of the above application shown in the gif.

Step By Step Implementation

Step 1: Create a new project

So the first step is to create a new project, On the Welcome screen of Android Studio, click on Create New Project and If you have a project already opened, Go to File > New > New Project. Then select a Project Template window, select Empty Activity and click Next. Enter your App Name in the Name field and select Java from the Language drop-down menu.

Step 2: Add the Internet Permission

Источник

How to Play MP4 files on Android Phones

Although an Android phone is compatible with MP4 video files, it also requires the file to be encoded in the way that is recognized by your particular phone model. Some MP4 videos are not supported to play on your Android due to the failure of reorganization. That’s why you need to convert it to the compatible one firstly.

Android Video Converter can convert videos to any format with 30X faster conversion speed and no quality loss. Download the free trial version here:

Windows version and Mac version are provided. The way it works is almost the same. Here we take Window version for an example.

How to Convert MP4 Videos to Android Phones

Step 1 Run the Video Converter on your Mac
First of all, download and install Android Video Converter on your Mac and then launch it, you will see the interface as below.

Step 2 Import MP4 Video File
Click «Add File» button at the upper-left to import the source MP4 video from computer that you want to convert.
Note: You are also allowed to drag and drop the files you want directly into the interface.

Step 3 Select Target Video Format
After adding the video, you can click the the button on the right of the video information bar or the drop-down list in next to profile to select the format or device that you need to convert the video to. At the bottom of the interface, you can set the folder that you want to save the converted video.

Step 4 Convert MP4 in Super Fast Speed
Hit the «Convert» button, then, the program will begin converting the MP4 videos. For few seconds and as the process bar ends, You can open the file and check it.

Note: After that, the MP4 videos can be converted to the format that it can be played on Android phone.

Читайте также:  Восстановление файлов после удаления для андроид

Step 5 Transfer & Play Video on Android
Now you can transfer the video to Android and enjoy the playback.

It is totally within your control. Moreover, besides video conversion, you can try the function of burning homemade DVDs to any formats you want, as well as transferring them to optimized device formats, so you can watch them on the go. Come to try it now.

Ivy Bruce

Ivy Bruce is the Founder and Editorial Director at Recovery-Android Studio. You can also find her on Google+ and Twitter.

How to Transfer Videos from Computer to iPhone

User Guide of Phone to Phone Transfer Software

How to Recover Deleted File from Motorola Atrix

How to Transfer Music from Computer to Nexus

Источник

How to play a Video File in Android Studio using VideoView – Full Code Tutorial and Explanation

What will you learn?

  1. How to use a VideoView element to play videos on your Android app. If needed, you can read more about the VideoView widget in detail before proceeding.

Note: Find the full code of the project towards the end of the post. Read the entire post for further clarification and explanations.

Building the Interface of our Video Player Android app

Of all our apps that we have created up until now, you will see that this has the simplest of interfaces. We need two elements in our Activity.

Start with an Empty Activity

Next, drag and drop a button and then a VideoView. If you don’t want to keep adjusting, and make the app work for all devices and orientations, drop in a LinearLayout before you drop your elements. If you have forgotten why we are using the LinearLayout here, then give this article a quick read. It tells us about the different layouts in Android Studio and which ones to use when.

Components Used:

  1. Layouts > LinearLayout
  2. Widgets > Button
  3. Images > VideoView

Here’s a screenshot of the Interface we have built. You’ll find the full code in the XML file towards the end of the article.

Attaching a function to the button

Once you highlight the button, a list of its properties comes up on the right-hand side. Once here, find the label ‘onClick‘ and in the input value, write down playVideo. This is a custom function that we will create in the MainActivity.java file.

Adding our video file in the Android App

This is just like how we did in our tutorial for playing an audio file using the MediaPlayer class. Firstly, on the left-hand side of our Android Studio, make sure we are on the PROJECT sidebar and under Android. From here, you have to go to app > res > raw. If, for whatever reason, you don’t have the raw directory, then right-click on the res folder and create a new directory with ‘raw’ as its name.

Once here, just right-click on the ‘raw‘ directory and click on ‘Reveal in Finder‘ if on MacOs and ‘Show in folder‘ if on Windows. I may be wrong about the Windows part, though, but you can figure it out.

This is the folder where you paste your video file.

Alternatively, if you copy your video file, you can right-click on the raw directory and click on paste. I find the drag and drop method to be more comfortable, though.

Just like when you are adding your audio file, remember to give a unique name with the extension and all of it in small cases.

Coding the functionality of the video player android app

Now here is the exciting part; this isn’t like how we coded our Audio Player file. It’s a bit different.

Step 1: Creating objects to our classes

We first create a new object of a class in the AppCompatActivity class.

Step 2: Connecting our object with our elements

Next, we have to connect this object with the VideoView we used in building the user interface.
We do this inside the onCreate function by the following code.

Step 3: Playing the video

We first create the function playVideo.

In that function, we are setting a new MediaController.

Next, we are getting the path of our video file. So go ahead and declare a new variable of data type String. To find the path of the video file, right-click on it and hit on Copy path address. And paste this address inside double-quotes. You can use this method, but we will use an alternative way.

An alternative way of doing this by the following code. It means the same thing, and either of the ways can be used.

Читайте также:  Внешний термодатчик для андроид

Now, all we need to do is to set a new Uri and parse the string that we defined above. We set the videoUri in this set, and the file is ready to play.

Next, we need to deploy the app to our target device and watch the magic happen. For any confusion, refer to the full code below.

Check out some more of our tutorials on Android.

The full code for our UI in the VideoPlayer App

The full code for the main functionality in the VideoPlayer App

Remember to change your package name at the beginning of the code!

About the author

Asem has a Bachelors Degree in Computer Science.

Back to course page

Share and Support

7 thoughts on “ How to play a Video File in Android Studio using VideoView – Full Code Tutorial and Explanation ”

I want to play video using multiple button click which will display different video but using same video view. How to code it?

hello, i would like to ask since i’m not sure if I understood correctly. If I were to put the android:resource file instead of the sourcepath can I play the video still on my phone?

also is it possible for the video to play right away once you open the application? I was thinking of making an app where you click a button that will redirect you to another activity that contains the video and the video will immediately play

As soon as the application starts running, the code snippet in onCreate gets executed. So you need to place the code from (playVideo) over there.

So you need to place the code in the onCreate function of your other activity.

Thanks a Lot. It was a very useful post.

I tried above codes. On Run it says “Can’t play this video”.
Cane you please solve it.

You need to troubleshoot this. Unfortunately, there’s no way for me to figure out why your video won’t run. Try these tips:

1. Check if you added the video to the correct directory.
2. Check that you named it appropriately as well. All small cases with the proper extension.
3. Check whether you mentioned the correct address of the video into the path.
4. Make sure you changed the package name when copy-pasting the full code into your Android Studio.

If nothing works, then make a copy of your code on github and forward the link here. I’ll be glad to take a look at it!

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Streaming Video in Android Apps

The Android platform provides libraries you can use to stream media files, such as remote videos, presenting them for playback in your apps. In this tutorial, we will stream a video file, displaying it using the VideoView component together with a MediaController object to let the user control playback.

We will also briefly run through the process of presenting the video using the MediaPlayer class. If you’ve completed the series on creating a music player for Android, you could use what you learn in this tutorial to further enhance it. You should be able to complete this tutorial if you have developed at least a few Android apps already.

Premium Option

If you want a ready-made solution, check out YoVideo, an Android app template for creating a beautiful mobile video player for Android smartphone.

Users can view videos, follow and share with their friends on Facebook. Using this application template will save you money and time in creating a video-sharing application.

YoVideo on Envato Market

Or you could hire an Android developer to create a custom solution for you. Otherwise, read on for the instructions on how to do it yourself.

1. Create a New App

Step 1

You can use the code in this tutorial to enhance an existing app you are working on or you can create a new app now in Eclipse or Android Studio. Create a new Android project, give it a name of your choice, configure the details, and give it an initial main Activity class and layout.

Step 2

Let’s first configure the project’s manifest for streaming media. Open the manifest file of your project and switch to XML editing in your IDE. For streaming media, you need internet access, so add the following permission inside the manifest element:

2. Add VideoView

Step 1

The Android platform provides the VideoView class in which you can play video files. Let’s add one to the main layout file:

Читайте также:  Гетконтакт не показывает кто звонит андроид что делать

Alter the parent layout to suit your own app if necessary. We give the VideoView instance an id attribute so that we can refer to it later. You may need to adjust the other layout properties for your own design.

Step 2

Now let’s retrieve a reference to the VideoView instance in code. Open your app’s main Activity class and add the following additional imports:

Your Activity class should already contain the onCreate method in which the content view is set:

After the setContentView line, let’s get a reference to the VideoView instance as follows, using the id we set in the XML layout:

3. Stream a Video File

Step 1

Now we can stream a video file to the app. Prepare the URI for the endpoint as follows:

You will of course need to use the remote address for the video file you want to stream. The example here is a public domain video file hosted on the Internet Archive. We parse the address string as a URI so that we can pass it to the VideoView object:

Now you can simply start playback:

The Android operating system supports a range of video and media formats, with each device often supporting additional formats on top of this.

As you can see in the Developer Guide, video file formats supported include 3GP, MP4, WEBM, and MKV, depending on the format used and on which platform level the user has installed.

Audio file formats you can expect built-in support for include MP3, MID, OGG, and WAV. You can stream media on Android over RTSP, HTTP, and HTTPS (from Android 3.1).

4. Add Playback Controls

Step 1

We’ve implemented video playback, but the user will expect and be accustomed to having control over it. Again, the Android platform provides resources for handling this using familiar interaction via the MediaController class.

In your Activity class’s onCreate method, before the line in which you call start on the VideoView , create an instance of the class:

Next, set it to use the VideoView instance as its anchor:

And finally, set it as the media controller for the VideoView object:

When you run the app now, the user should be able to control playback of the streaming video, including fast forward and rewind buttons, a play/pause button, and a seek bar control.

The seek bar control is accompanied by the length of the media file on the right and the current playback position on the left. As well as being able to tap along the seek bar to jump to a position in the file, the streaming status is indicated using the same type of display the user will be accustomed to from sites and apps like YouTube.

As you will see when you run the app, the default behavior is for the controls to disappear after a few moments, reappearing when the user touches the screen. You can configure the behavior of the MediaController object in various ways. See the series on creating a music player app for Android for an example of how to do this. You can also enhance media playback by implementing various listeners to configure your app’s behavior.

5. Using MediaPlayer

Step 1

Before we finish, let’s run through an alternative approach for streaming video using the MediaPlayer class, since we used it in the series on creating a music player. You can stream media, including video, to a MediaPlayer object using a surface view. For example, you could use the following layout:

We will refer to the SurfaceView in the implementation of the Activity class.

Step 2

In your Activity class, add the following interfaces:

Your IDE should prompt you to add these unimplemented methods:

We will add to the surfaceCreated and onPrepared methods.

Step 3

To implement playback, add the following instance variables to the class:

In the Activity ‘s onCreate method, you can then start to instantiate these variables using the SurfaceView object you added to the layout:

Step 4

In the surfaceCreated method, set up your media playback resources:

Finally, in the onPrepared method, start playback:

Your video should now play in the MediaPlayer instance when you run the app.

Conclusion

In this tutorial, we have outlined the basics of streaming video on Android using the VideoView and MediaPlayer classes. You could add lots of enhancements to the code we implemented here, for example, by building video or streaming media support into the music player app we created. You may also wish to check out associated resources for Android such as the YouTube Android Player API.

Источник

Оцените статью