android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer)

如果你看過我的《android音樂播放簡單實現(xiàn)(MediaPlayer)》,那么本篇將會毫無壓力。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,溫泉企業(yè)網(wǎng)站建設,溫泉品牌網(wǎng)站建設,網(wǎng)站定制,溫泉網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,溫泉網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

首先是主界面的三個按鈕和一個播放控件

<?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="com.cofox.myplayvideo.MainActivity">

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <Button
   android:id="@+id/btnPlay"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Play"
   android:textAllCaps="false" />

  <Button
   android:id="@+id/btnPause"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Pause"
   android:textAllCaps="false" />

  <Button
   android:id="@+id/btnReplay"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Replay"
   android:textAllCaps="false" />
 </LinearLayout>
 <VideoView
  android:id="@+id/vdvwFilm"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

</LinearLayout>

在 MainActivity.java 中這里需要用到的是 VideoView 作為視頻播放時的顯示位置。

 private VideoView videoView;

在 onCreate 里,對界面的按鈕和顯示位置實例化,并檢查權限。

videoView = (VideoView)findViewById(R.id.vdvwFilm);
  Button btnPlay = (Button)findViewById(R.id.btnPlay);
  Button btnPause = (Button)findViewById(R.id.btnPause);
  Button btnReplay = (Button)findViewById(R.id.btnReplay);

  btnPlay.setOnClickListener(this);
  btnPause.setOnClickListener(this);
  btnReplay.setOnClickListener(this);

  if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
   ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  }else {
   initVideoPath();//初始化MediaPlayer
  }

用一個單獨的方法 initVideoPath() 來實現(xiàn)視頻播放初始化

 private void initVideoPath() {
  File file = new File(Environment.getExternalStorageDirectory(), "movie2.mp4");
  videoView.setVideoPath(file.getPath());//指定視頻文件路徑
  videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
   @Override
   public void onPrepared(MediaPlayer mp) {
    mp.setLooping(true);//讓電影循環(huán)播放
   }
  });
 }

onRequestPermissionsResult 中對權限的取得結果進行判斷,并針對性操作。如果獲得了權限,就執(zhí)行初始化;如果沒有獲得權限,就提示用戶。

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  switch (requestCode){
   case 1:
    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
     initVideoPath();
    }else{
     Toast.makeText(this, "拒絕權限,無法使用程序。", Toast.LENGTH_LONG).show();
     finish();
    }
    break;
   default:
    break;
  }
 }

在一個 onClick 方法中,統(tǒng)一處理 Play(播放)、Pause(暫停)、Replay(重新播放)的邏輯。

 @Override
 public void onClick(View v) {
  switch (v.getId()){
   case R.id.btnPlay:
    if(!videoView.isPlaying()){
     videoView.start();//播放
    }
    break;
   case R.id.btnPause:
    if(videoView.isPlaying()){
     videoView.pause();//暫停
    }
    break;
   case R.id.btnReplay:
    if(videoView.isPlaying()){
     videoView.resume();//重新播放
    }
    break;
  }
 }

執(zhí)行完畢,釋放所有資源。

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if(videoView != null){
   videoView.suspend();
  }
 }

完整代碼示例:

package com.cofox.myplayvideo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.os.EnvironmentCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 private VideoView videoView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  videoView = (VideoView)findViewById(R.id.vdvwFilm);
  Button btnPlay = (Button)findViewById(R.id.btnPlay);
  Button btnPause = (Button)findViewById(R.id.btnPause);
  Button btnReplay = (Button)findViewById(R.id.btnReplay);

  btnPlay.setOnClickListener(this);
  btnPause.setOnClickListener(this);
  btnReplay.setOnClickListener(this);

  if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
   ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  }else {
   initVideoPath();//初始化MediaPlayer
  }
 }

 private void initVideoPath() {
  File file = new File(Environment.getExternalStorageDirectory(), "movie2.mp4");
  videoView.setVideoPath(file.getPath());//指定視頻文件路徑
  videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
   @Override
   public void onPrepared(MediaPlayer mp) {
    mp.setLooping(true);//讓電影循環(huán)播放
   }
  });
 }

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  switch (requestCode){
   case 1:
    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
     initVideoPath();
    }else{
     Toast.makeText(this, "拒絕權限,無法使用程序。", Toast.LENGTH_LONG).show();
     finish();
    }
    break;
   default:
    break;
  }
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()){
   case R.id.btnPlay:
    if(!videoView.isPlaying()){
     videoView.start();//播放
    }
    break;
   case R.id.btnPause:
    if(videoView.isPlaying()){
     videoView.pause();//暫停
    }
    break;
   case R.id.btnReplay:
    if(videoView.isPlaying()){
     videoView.resume();//重新播放
    }
    break;
  }
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if(videoView != null){
   videoView.suspend();
  }
 }
}

在 AndroidManifest.xml 中配置相應的權限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.cofox.myplayvideo">
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...

然后我們就可以看到運行結果了。

android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer)

android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁標題:android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer)
網(wǎng)頁URL:http://bm7419.com/article16/ijhcdg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務網(wǎng)站制作、Google、網(wǎng)站設計、響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設