android實(shí)現(xiàn)播放網(wǎng)絡(luò)視頻
本文實(shí)例為大家分享了android實(shí)現(xiàn)播放網(wǎng)絡(luò)視頻的具體代碼,供大家參考,具體內(nèi)容如下
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, "沒有足夠的權(quán)限", 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中兩個(gè)Activity之間數(shù)據(jù)傳遞及返回問題
本篇文章主要介紹了Android中兩個(gè)Activity之間數(shù)據(jù)傳遞及返回問題,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02Android ListView在Fragment中的使用示例詳解
這篇文章主要介紹了Android ListView在Fragment中的使用,因?yàn)楣ぷ饕恢痹谟胢vvm框架,因此這篇文章是基于mvvm框架寫的,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09安卓應(yīng)用開發(fā)通過java調(diào)用c++ jni的圖文使用方法
這篇文章主要介紹了2013-11-11Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞
這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08詳細(xì)分析android的MessageQueue.IdleHandler
這篇文章主要介紹了android的MessageQueue.IdleHandler用法,很有參考價(jià)值,歡迎大家在下方留言區(qū)討論。2017-11-11Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法
這篇文章主要介紹了Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法,通過ListView控件綁定setOnScrollListener方法簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的功能,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
今天介紹一下,我在項(xiàng)目開發(fā)過程中,實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實(shí)現(xiàn)方式是,通過隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實(shí)現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方,需要的朋友可以參考下2019-05-05Flutter交互并使用小工具管理其狀態(tài)widget的state詳解
這篇文章主要為大家介紹了Flutter交互并使用小工具管理其狀態(tài)widget的state詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12