代码压缩包

VideoPlaying.zip

一、第一种方法

1.实现步骤

(1).在清单文件中添加权限

image.png

(2).在布局文件中写出样式

image.png

(3).在java文件中添加地址,实现视频播放

image.png

2.整体代码

  1. (1).AndroidManifest.xml清单文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.videoplaying">
  4. <uses-permission android:name="android.permission.INTERNET"/>
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/Theme.VideoPlaying">
  13. <activity android:name=".MainActivity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. </manifest>
  1. (2).activity_main.xml布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical">
  9. <VideoView
  10. android:id="@+id/vv_demo"
  11. android:layout_width="match_parent"
  12. android:layout_height="300dp"
  13. />
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="horizontal">
  18. <Button
  19. android:id="@+id/btn_begin"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="开始"
  23. android:layout_marginLeft="20dp"
  24. android:background="#8BC34A"
  25. />
  26. <Button
  27. android:id="@+id/btn_end"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="结束"
  31. android:layout_marginLeft="20dp"
  32. android:background="#2196F3"
  33. />
  34. </LinearLayout>
  35. </LinearLayout>
  1. (3).MainActivity文件代码
  1. package com.example.videoplaying;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.core.app.ActivityCompat;
  4. import android.Manifest;
  5. import android.app.Activity;
  6. import android.content.pm.PackageManager;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.MediaController;
  12. import android.widget.VideoView;
  13. public class MainActivity extends AppCompatActivity {
  14. private VideoView vv_demo;
  15. private Button btn_begin;
  16. private Button btn_end;
  17. private MediaController mediaController;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. initView(); }
  23. private void initView() {
  24. vv_demo=findViewById(R.id.vv_demo);
  25. btn_begin=findViewById(R.id.btn_begin);
  26. btn_end=findViewById(R.id.btn_end);
  27. btn_begin.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. init();
  31. }
  32. });
  33. btn_end.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. vv_demo.stopPlayback();
  37. }
  38. });
  39. }
  40. public void init(){
  41. vv_demo=findViewById(R.id.vv_demo);
  42. mediaController=new MediaController(this);
  43. //本地连接地址
  44. // String uri="android.resource://"+getPackageName()+"/"+R.raw.aas;
  45. //网络连接地址
  46. String uri="/sdcard/Pictures/a.mp4";
  47. vv_demo.setVideoURI(Uri.parse(uri));
  48. vv_demo.setMediaController(mediaController);
  49. mediaController.setMediaPlayer(vv_demo);
  50. vv_demo.requestFocus();
  51. vv_demo.start();
  52. }
  53. }

3.结果图

image.png

代码压缩包:

VideoPlay.zip

二、第二种方法

1.实现步骤

(1).在布局文件中添加布局页面

image.png

(2).创建Utils文件,在文件中放入视频播放的地址

image.png

(3).在java文件中,实现视频播放

image.png

2.整体代码

  1. (1).activity_main.xml布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <RelativeLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="200dp">
  11. <VideoView
  12. android:id="@+id/vv_demo"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. />
  16. </RelativeLayout>
  17. </RelativeLayout>
  1. (2).Utils文件代码
  1. package com.example.videoplay;
  2. public class Utils {
  3. public static final String videoUrl="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
  4. }
  1. (3).MainActivity文件代码
  1. package com.example.videoplay;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.media.MediaPlayer;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.widget.MediaController;
  7. import android.widget.Toast;
  8. import android.widget.VideoView;
  9. public class MainActivity extends AppCompatActivity {
  10. private VideoView vv_demo;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. //网络视频
  16. String videoUrl=Utils.videoUrl;
  17. Uri uri= Uri.parse(videoUrl);
  18. vv_demo=findViewById(R.id.vv_demo);
  19. //设置视频控制器
  20. vv_demo.setMediaController(new MediaController(this));
  21. //播放完成回调
  22. vv_demo.setOnCompletionListener(new MyPlayerOnCompletionListener());
  23. //设置视频路径
  24. vv_demo.setVideoURI(uri);
  25. //开始播放视频
  26. vv_demo.start();
  27. }
  28. class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
  29. @Override
  30. public void onCompletion(MediaPlayer mp) {
  31. Toast.makeText(MainActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
  32. }
  33. }
  34. }

3.结果图

image.png