How to play video from url?

In this tutorial, we’ll learn about Android VideoView. We will create an apk phầm mềm in Android Studio và play video from URL, we will also do some customizations khổng lồ it’s control panel.

Bạn đang xem: How to play video from url?


Android VideoView

Android VideoView class is used khổng lồ display Video files in them. Following are the acceptable formats:


3gpMP4 – Only H.263, H.264, H.264 codecs work.

VideoViews can play videos either from resource files, local data or url specified. Following are some of the methods used on VideoView:

setVideoURI() – This is used to mix the url path. It needs to be parsed as a URI.setVideoPath() – Used for local paths.start()stopPlayback()seekTo(int milliSec)pause()resume()isPlaying()canPause()canSeekForward()canSeekBackward()setOnCompletedListener()addSubtitleSource()setMediaController() : Used lớn add MediaControls on the Video. Pause/Play, SeekgetDuration()setOnPreparedListener() : Gets invoked once the Clip starts.

Note: VideoView does not retain its full state when going into the background.

In the following section, we’ll be creating an application that runs Videos from urls one after the other.We’ll see how the MediaController works with the VideoView.

Android VideoView Example

*


Do not forget to lớn add the Internet Permission in your AndroidManifest.xml file.

Xem thêm: Cốt Bánh Bông Lan Sữa Tươi Đơn Giản Mà Ngon, Cách Làm Bánh Bông Lan Sữa Tươi Đơn Giản Mà Ngon

Android VideoView Example Code

activity_main.xml

MainActivity.java

The code for the MainActivity.java looks lượt thích this:


package com.thesuggestr.com.videoview;import android.media.MediaPlayer;import app android.net.Uri;import android.support.v7.ứng dụng.AppCompatActivity;import app android.os.Bundle;import android.util.Log;import app android.widget.MediaController;import android.widget.Toast;import app android.widget.VideoView;import java.util.ArrayList;import java.util.Arrays;public class MainActivity extends AppCompatActivity VideoView videoView; ArrayList arrayList = new ArrayList(Arrays.asList("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")); int index = 0;
Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = findViewById(R.id.videoView); final MediaController mediacontroller = new MediaController(this); mediacontroller.setAnchorView(videoView); videoView.setMediaController(mediacontroller); videoView.setVideoURI(Uri.parse(arrayList.get(index))); videoView.requestFocus(); videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
Override public void onCompletion(MediaPlayer mp) Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show(); if (index++ == arrayList.size()) index = 0; mp.release(); Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show(); else videoView.setVideoURI(Uri.parse(arrayList.get(index))); videoView.start(); ); videoView.setOnErrorListener(new MediaPlayer.OnErrorListener()
Override public boolean onError(MediaPlayer mp, int what, int extra) Log.d("API123", "What " + what + " extra " + extra); return false; ); We’ve sầu added two urls in an ArrayList. We mix the anchorView() on the VideoView to keep the MediaControl inside the VideoView.

The output looks something lượt thích this:

*

Why is the MediaControl outside the VideoView?

Well, the MediaControl doesn’t know the dimensions of the VideoView until the đoạn phim is started.

For having truyền thông media control inside the đoạn phim, we need lớn mix the onPreparedListener on our VideoView và then set the anchor inside it. This way the MediaController is set inside the VideoView.

Our updated MainActivity.java class looks like this now:

package com.thesuggestr.com.videoview;import apk.truyền thông media.MediaPlayer;import app android.net.Uri;import android.tư vấn.v7.phầm mềm.AppCompatActivity;import app android.os.Bundle;import game android.util.Log;import apk.widget.MediaController;import android.widget.Toast;import app android.widget.VideoView;import java.util.ArrayList;import java.util.Arrays;public class MainActivity extends AppCompatActivity VideoView videoView; ArrayList arrayList = new ArrayList(Arrays.asList("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")); int index = 0;
Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = findViewById(R.id.videoView); final MediaController mediacontroller = new MediaController(this); mediacontroller.setAnchorView(videoView); videoView.setMediaController(mediacontroller); videoView.setVideoURI(Uri.parse(arrayList.get(index))); videoView.requestFocus(); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
Override public void onPrepared(MediaPlayer mp) mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener()
Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) videoView.setMediaController(mediacontroller); mediacontroller.setAnchorView(videoView); ); ); videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
Override public void onCompletion(MediaPlayer mp) Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show(); if (index++ == arrayList.size()) index = 0; mp.release(); Toast.makeText(getApplicationContext(), "Videos completed", Toast.LENGTH_SHORT).show(); else videoView.setVideoURI(Uri.parse(arrayList.get(index))); videoView.start(); ); videoView.setOnErrorListener(new MediaPlayer.OnErrorListener()
Override public boolean onError(MediaPlayer mp, int what, int extra) Log.d("API123", "What " + what + " extra " + extra); return false; ); mp.release() is mix to release the truyền thông player resources. It should be done lớn prevent memory leaks. Any videoView calls after this line would lead lớn a CRASH.

Xem thêm: Giới Thiệu Về Công Ty Cổ Phần Thuốc Thú Y Marphavet Tuyển Dụng

Now the output of the above sầu application in action is given below:

*

This brings an over to app android đoạn phim view tutorial. You can download the final Android VideoView project from the link below.


Chuyên mục: Công nghệ