华域联盟 Andriod android实现播放网络视频

android实现播放网络视频

本文实例为大家分享了android实现播放网络视频的具体代码,供大家参考,具体内容如下

PlayVideoActivity.java

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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实现播放网络视频

转载请保留出处和原文链接:https://www.cnhackhy.com/109262.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Android 第三方库lottie、mmkv的使用详解

Android使用Walle实现多渠道打包功能的实现示例

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们