多媒体——视频——利用视频视图VideoView播放视频

矫情吗;* 2024-04-03 12:50 214阅读 0赞

aad6f7d8c98ac1748c512f363760be88.png

08c1087d463b75aa50c5b404a1377ad3.png

97bb47c1e980c3e9cadc580bfe174d73.png

8425d8754c42967a01e1096166e79fc9.png

========================================================================================================================

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <Button
  6. android:id="@+id/btn_choose"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="打开相册播放视频"
  10. android:textColor="@color/black"
  11. android:textSize="17sp" />
  12. <VideoView
  13. android:id="@+id/vv_content"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content" />
  16. </LinearLayout>

155f03aea2a4727f1a0def657ddd0a24.png

主代码:

  1. package com.example.myapplication;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.MediaController;
  6. import android.widget.VideoView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MainActivity extends AppCompatActivity implements View.OnClickListener
  9. {
  10. private final static String TAG = "VideoPlayActivity";
  11. private VideoView vv_content; // 声明一个视频视图对象
  12. private int CHOOSE_CODE = 3; // 只在视频库挑选图片的请求码
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. // 从布局文件中获取名叫vv_content的视频视图
  19. vv_content = findViewById(R.id.vv_content);
  20. findViewById(R.id.btn_choose).setOnClickListener(this);
  21. }
  22. @Override
  23. public void onClick(View v)
  24. {
  25. if (v.getId() == R.id.btn_choose)
  26. {
  27. // 创建一个内容获取动作的意图(准备跳到系统视频库)
  28. // ACTION_GET_CONTENT只可选择近期的视频
  29. //Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  30. // ACTION_PICK可选择所有视频
  31. Intent intent = new Intent(Intent.ACTION_PICK);
  32. intent.setType("video/*"); // 类型为视频
  33. startActivityForResult(intent, CHOOSE_CODE); // 打开系统视频库
  34. }
  35. }
  36. @Override
  37. protected void onActivityResult(int requestCode, int resultCode, Intent intent)
  38. {
  39. super.onActivityResult(requestCode, resultCode, intent);
  40. if (resultCode == RESULT_OK && requestCode == CHOOSE_CODE)
  41. {
  42. if (intent.getData() != null) // 从视频库回来
  43. {
  44. vv_content.setVideoURI(intent.getData()); // 设置视频视图的视频路径
  45. MediaController mc = new MediaController(this); // 创建一个媒体控制条
  46. vv_content.setMediaController(mc); // 给视频视图设置相关联的媒体控制条
  47. mc.setMediaPlayer(vv_content); // 给媒体控制条设置相关联的视频视图
  48. vv_content.start(); // 视频视图开始播放
  49. }
  50. }
  51. }
  52. }

663bca4803a47fb40b62da6579e07cc8.png

57c756423b5d6e41b60e7334f97d3365.png

a9595f553c10769151e4a660f7e7e914.png

d0236f25883118c3a10e5aadd8646843.png

b294d30efaec5dc1d59ddcdc93129e28.png

d76a813260c895413945cb0c14fee0c8.png

66a7c55d6b75a3ee62d43453dedca46d.png

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读