Android如何制作一个简易的视频播放器
生活随笔
收集整理的這篇文章主要介紹了
Android如何制作一个简易的视频播放器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Android如何制作一個簡易的視頻播放器
——安德風(fēng)QQ1652102745
一、效果演示:
二、布局設(shè)計activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:orientation="vertical" 8 android:layout_height="match_parent" 9 tools:context="com.example.video.MainActivity"> 10 11 <VideoView 12 android:id="@+id/videoView" 13 android:layout_width="match_parent" 14 android:layout_height="300dp" /> 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:orientation="horizontal"> 19 <Button 20 android:id="@+id/btn_start" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="開始" 24 android:layout_marginLeft="20dp"/> 25 26 <Button 27 android:id="@+id/btn_end" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="結(jié)束" /> 31 </LinearLayout> 32 </LinearLayout>
三、功能實現(xiàn)MainActivity.java
1 package com.example.video;
2
3
4 import android.net.Uri;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.widget.Button;
8 import android.widget.MediaController;
9 import android.widget.VideoView;
10
11 import androidx.appcompat.app.AppCompatActivity;
12
13 public class MainActivity extends AppCompatActivity {
14 private VideoView videoView;
15 private Button btn_start,btn_end;
16 private MediaController mediaController;
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_main);
22 initView();
23 }
24
25 private void initView() {
26 videoView= (VideoView) findViewById(R.id.videoView);
27 btn_start= (Button) findViewById(R.id.btn_start);
28 btn_end= (Button) findViewById(R.id.btn_end);
29
30
31 btn_start.setOnClickListener(new View.OnClickListener() {
32 @Override
33 public void onClick(View v) {
34 init();//實現(xiàn)開始播放功能函數(shù)
35 }
36 });
37 btn_end.setOnClickListener(new View.OnClickListener() {
38 @Override
39 public void onClick(View v) {
40 videoView.stopPlayback();//結(jié)束播放
41 }
42 });
43 }
44
45 private void init() {
46 videoView = (VideoView) findViewById(R.id.videoView); //綁定視頻視圖控件ID
47 mediaController = new MediaController(this);//創(chuàng)建媒體控制器
48 String uri = "android.resource://" + getPackageName() + "/" + R.raw.a;//導(dǎo)入視頻路徑
49 videoView.setVideoURI(Uri.parse(uri));//設(shè)置視頻文件的統(tǒng)一資源標(biāo)志符目的為了導(dǎo)入視頻路徑以及解析視頻
50 videoView.setMediaController(mediaController);//設(shè)置視頻控制器
51 mediaController.setMediaPlayer(videoView);//通過媒體控制器來控制視頻播放器
52 videoView.requestFocus();//請求獲得視頻視圖焦點
53 videoView.start();//開始播放
54 }
55 }
四、視頻存放路徑:R/raw
總結(jié)
以上是生活随笔為你收集整理的Android如何制作一个简易的视频播放器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springMVC分析-2
- 下一篇: 小程序 反编译 pc微信