zoukankan      html  css  js  c++  java
  • 4.14Android学习

    一、今日学习内容

    android实现播放网络视频

     

    PlayVideoActivity.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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    package cn.edu.zufe.app002;
     
    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    import android.widget.VideoView;
     
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;
    import androidx.core.content.ContextCompat;
     
    public class PlayVideoActivity extends AppCompatActivity implements View.OnClickListener{
     
        private VideoView vvVideo;
        private Button btnPlay;
        private Button btnPause;
        private Button btnReplay;
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_play_video);
     
            vvVideo = (VideoView) findViewById(R.id.vv_video);
            btnPlay = (Button) findViewById(R.id.btn_play);
            btnPause = (Button) findViewById(R.id.btn_pause);
            btnReplay = (Button) findViewById(R.id.btn_replay);
     
            btnPlay.setOnClickListener(this);
            btnPause.setOnClickListener(this);
            btnReplay.setOnClickListener(this);
     
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            } else {
                initVideoView();
            }
        }
     
        private void initVideoView() {
            vvVideo.setVideoPath("http://jackie.vaiwan.com/cn.edu.zufe.app002/zhecai.mp4");
        }
     
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_play:
                    if(!vvVideo.isPlaying()) {
                        vvVideo.start();
                    }
                    break;
                case R.id.btn_pause:
                    if(vvVideo.isPlaying()) {
                        vvVideo.pause();
                    }
                    break;
                case R.id.btn_replay:
                    if(vvVideo.isPlaying()) {
                        vvVideo.resume();
                    }
                    break;
                default:
                    break;
            }
        }
     
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            switch (requestCode) {
                case 1:
                    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        initVideoView();
                    } else {
                        Toast.makeText(this, "没有足够的权限", Toast.LENGTH_SHORT).show();
                        finish();
                    }
            }
        }
    }

    activity_play_video.xml

    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
    39
    40
    41
    42
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
        tools:context=".PlayVideoActivity">
     
        <VideoView
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:id="@+id/vv_video" />
         
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
     
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="播放"
                android:id="@+id/btn_play" />
     
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="暂停"
                android:id="@+id/btn_pause" />
     
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="重播"
                android:id="@+id/btn_replay" />
     
        </LinearLayout>
     
    </LinearLayout>

    二、遇到的问题

     暂无

    三、明日计划

    继续安卓学习

  • 相关阅读:
    为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
    为Android系统的Application Frameworks层增加硬件访问服务
    为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
    为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
    为Android系统内置C可执行程序测试Linux内核驱动程序
    Android内核驱动程序的编写和编译过程
    添加一个Application Framework Service
    getopt()函数
    Google推Android新开发语言Sky:流畅度 秒iOS
    开发人员要避免的七个心态问题
  • 原文地址:https://www.cnblogs.com/zyljal/p/14909910.html
Copyright © 2011-2022 走看看