Here you will learn how to play video from url in android using videoview.
在这里,您将学习如何使用videoview从android中的url播放视频。
We can using VideoView widget to play offline video as well as video from url.
我们可以使用VideoView小部件来播放脱机视频以及来自url的视频。
In below example I have displayed a progress dialog until the video is buffering.
在下面的示例中,我显示了一个进度对话框,直到视频正在缓冲。
Also Read: Picasso Android Tutorial – Load Image from URL
另请阅读: Picasso Android教程–从URL加载图像
Android使用VideoView示例从URL播放视频 (Android Play Video From URL Using VideoView Example)
Create an android studio project with package name com.videoview
使用包名称com.videoview创建一个android studio项目
Add internet access permission in AndroidManifest.xml file.
在AndroidManifest.xml文件中添加Internet访问权限。
|
1
|
<uses-permission android:name="android.permission.INTERNET" />
|
|
1
|
< uses - permission android : name = "android.permission.INTERNET" / >
|
Now add following code in respective files.
现在,在相应文件中添加以下代码。
activity_main.xml
activity_main.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.videoview.MainActivity">
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/video" />
</RelativeLayout>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns : android = "http://schemas.android.com/apk/res/android"
xmlns : tools = "http://schemas.android.com/tools"
android : layout_width = "match_parent"
android : layout_height = "match_parent"
android : paddingBottom = "@dimen/activity_vertical_margin"
android : paddingLeft = "@dimen/activity_horizontal_margin"
android : paddingRight = "@dimen/activity_horizontal_margin"
android : paddingTop = "@dimen/activity_vertical_margin"
tools : context = "com.videoview.MainActivity" >
< VideoView
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : id = "@+id/video" / >
< / RelativeLayout >
|
MainActivity.java
MainActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.videoview;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView video;
String video_url = "http://file2.video9.in/english/movie/2014/x-men-_days_of_future_past/X-Men-%20Days%20of%20Future%20Past%20Trailer%20-%20[Webmusic.IN].3gp";
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView)findViewById(R.id.video);
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Buffering video please wait...");
pd.show();
Uri uri = Uri.parse(video_url);
video.setVideoURI(uri);
video.start();
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//close the progress dialog when buffering is done
pd.dismiss();
}
});
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com . videoview ;
import android . app . ProgressDialog ;
import android . media . MediaPlayer ;
import android . net . Uri ;
import android . support . v7 . app . AppCompatActivity ;
import android . os . Bundle ;
import android . widget . VideoView ;
public class MainActivity extends AppCompatActivity {
VideoView video ;
String video_url = "http://file2.video9.in/english/movie/2014/x-men-_days_of_future_past/X-Men-%20Days%20of%20Future%20Past%20Trailer%20-%20[Webmusic.IN].3gp" ;
ProgressDialog pd ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
video = ( VideoView ) findViewById ( R . id . video ) ;
pd = new ProgressDialog ( MainActivity . this ) ;
pd . setMessage ( "Buffering video please wait..." ) ;
pd . show ( ) ;
Uri uri = Uri . parse ( video_url ) ;
video . setVideoURI ( uri ) ;
video . start ( ) ;
video . setOnPreparedListener ( new MediaPlayer . OnPreparedListener ( ) {
@Override
public void onPrepared ( MediaPlayer mp ) {
//close the progress dialog when buffering is done
pd . dismiss ( ) ;
}
} ) ;
}
}
|
Save and run the project.
保存并运行项目。
Screenshot
屏幕截图
翻译自: https://www.thecrazyprogrammer.com/2016/11/android-play-video-url-using-videoview.html