如果你看過(guò)我的《android音樂(lè)播放簡(jiǎn)單實(shí)現(xiàn)(MediaPlayer)》,那么本篇將會(huì)毫無(wú)壓力。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),溫泉企業(yè)網(wǎng)站建設(shè),溫泉品牌網(wǎng)站建設(shè),網(wǎng)站定制,溫泉網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,溫泉網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
首先是主界面的三個(gè)按鈕和一個(gè)播放控件
<?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 作為視頻播放時(shí)的顯示位置。
private VideoView videoView;
在 onCreate 里,對(duì)界面的按鈕和顯示位置實(shí)例化,并檢查權(quán)限。
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 }
用一個(gè)單獨(dú)的方法 initVideoPath() 來(lái)實(shí)現(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 中對(duì)權(quán)限的取得結(jié)果進(jìn)行判斷,并針對(duì)性操作。如果獲得了權(quán)限,就執(zhí)行初始化;如果沒(méi)有獲得權(quá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, "拒絕權(quán)限,無(wú)法使用程序。", Toast.LENGTH_LONG).show(); finish(); } break; default: break; } }
在一個(gè) 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, "拒絕權(quán)限,無(wú)法使用程序。", 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 中配置相應(yīng)的權(quán)限。
<?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"/> ...
然后我們就可以看到運(yùn)行結(jié)果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁(yè)標(biāo)題:android視頻播放簡(jiǎn)單實(shí)現(xiàn)示例(VideoView&MediaPlayer)
網(wǎng)頁(yè)URL:http://aaarwkj.com/article16/ijhcdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站制作、、Google、網(wǎng)站設(shè)計(jì)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)