Android編程實(shí)現(xiàn)播放視頻的方法示例
本文實(shí)例講述了Android編程實(shí)現(xiàn)播放視頻的方法。分享給大家供大家參考,具體如下:
播放視頻文件其實(shí)并不比播放音頻文件復(fù)雜,主要是使用 VideoView 類來實(shí)現(xiàn)的。這個(gè) 類將視頻的顯示和控制集于一身,使得我們僅僅借助它就可以完成一個(gè)簡易的視頻播放器。 VideoView 的用法和 MediaPlayer 也比較類似,主要有以下常用方法:
|
方法名 |
功能描述 |
|
setVideoPath() |
設(shè)置要播放的視頻文件的位置。 |
|
start() |
開始或繼續(xù)播放視頻。 |
|
pause() |
暫停播放視頻。 |
|
resume() |
將視頻重頭開始播放。 |
|
seekTo() |
從指定的位置開始播放視頻。 |
|
isPlaying() |
判斷當(dāng)前是否正在播放視頻。 |
|
getDuration() |
獲取載入的視頻文件的時(shí)長。 |
那么我們還是通過一個(gè)實(shí)際的例子來學(xué)習(xí)一下吧,新建 PlayVideoTest 項(xiàng)目,然后修改activity_main.xml 中的代碼,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/play" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Play" /> <Button android:id="@+id/pause" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Pause" /> <Button android:id="@+id/replay" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Replay" /> </LinearLayout> </LinearLayout>
在這個(gè)布局文件中,首先是放置了一個(gè) VideoView,稍后的視頻就將在這里顯示。然后在 VideoView 的下面又放置了三個(gè)按鈕,分別用于控制視頻的播放、暫停和重新播放。 接下來修改 MainActivity 中的代碼,如下所示:
public class MainActivity extends Activity implements OnClickListener {
private VideoView videoView;
private Button play; private Button pause; private Button replay;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
replay = (Button) findViewById(R.id.replay);
videoView = (VideoView) findViewById(R.id.video_view); play.setOnClickListener(this); pause.setOnClickListener(this); replay.setOnClickListener(this);
initVideoPath();
}
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(), "movie.3gp");
videoView.setVideoPath(file.getPath()); // 指定視頻文件的路徑
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!videoView.isPlaying()) {
videoView.start(); // 開始播放
}
break;
case R.id.pause:
if (videoView.isPlaying()) {
videoView.pause(); // 暫時(shí)播放
}
break;
case R.id.replay:
if (videoView.isPlaying()) {
videoView.resume(); // 重新播放
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null) {
videoView.suspend();
}
}
}
這部分代碼相信你理解起來會(huì)很輕松,因?yàn)樗颓懊娌シ乓纛l的代碼非常類似。首先在 onCreate()方法中仍然是去獲取一些控件的實(shí)例,然后調(diào)用了 initVideoPath()方法來設(shè)置視頻 文件的路徑,這里我們需要事先在 SD 卡的根目錄下放置一個(gè)名為 movie.3gp 的視頻文件。 下面看一下各個(gè)按鈕的點(diǎn)擊事件中的代碼。當(dāng)點(diǎn)擊 Play 按鈕時(shí)會(huì)進(jìn)行判斷,如果當(dāng)前 并沒有正在播放音頻,則調(diào)用 start()方法開始播放。當(dāng)點(diǎn)擊 Pause 按鈕時(shí)會(huì)判斷,如果當(dāng)前 視頻正在播放,則調(diào)用 pause()方法暫時(shí)播放。當(dāng)點(diǎn)擊 Replay 按鈕時(shí)會(huì)判斷,如果當(dāng)前視頻正在播放,則調(diào)用 resume()方法重頭播放視頻。最后在 onDestroy()方法中,我們還需要調(diào)用一下 suspend()方法,將 VideoView 所占用的 資源釋放掉。
現(xiàn)在將程序運(yùn)行到手機(jī)上,然后點(diǎn)擊一下 Play 按鈕,就可以看到視頻已經(jīng)開始播放了, 如下圖所示:

點(diǎn)擊 Pause 按鈕可以暫停視頻的播放,點(diǎn)擊 Replay 按鈕可以重頭播放視頻。 這樣的話,你就已經(jīng)將 VideoView 的基本用法掌握得差不多了。不過,為什么它的用法
和 MediaPlayer 這么相似呢?其實(shí) VideoView 只是幫我們做了一個(gè)很好的封裝而已,它的背 后仍然是使用 MediaPlayer 來對(duì)視頻文件進(jìn)行控制的。另外需要注意,VideoView 并不是一 個(gè)萬能的視頻播放工具類,它在視頻格式的支持以及播放效率方面都存在著較大的不足。所 以,如果想要僅僅使用 VideoView 就編寫出一個(gè)功能非常強(qiáng)大的視頻播放器是不太現(xiàn)實(shí)的。 但是如果只是用于播放一些游戲的片頭動(dòng)畫,或者某個(gè)應(yīng)用的視頻宣傳,使用 VideoView 還 是綽綽有余的。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android使用surfaceview+MediaPlayer播放視頻
- Android ViewPager中顯示圖片與播放視頻的填坑記錄
- Android中使用TextureView播放視頻
- Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
- Android仿搜狐視頻、微視等列表播放視頻功能
- Android多媒體教程之播放視頻的四種方法
- Android 播放視頻常見問題小結(jié)
- Android DragVideo實(shí)現(xiàn)播放視頻時(shí)任意拖拽的方法
- Android仿新浪微博/QQ空間滑動(dòng)自動(dòng)播放視頻功能
- android surfaceView實(shí)現(xiàn)播放視頻功能
相關(guān)文章
Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案
今天小編就為大家分享一篇關(guān)于Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
android中關(guān)于call撥號(hào)功能的實(shí)現(xiàn)方法
這篇文章主要介紹了android中關(guān)于call撥號(hào)功能實(shí)現(xiàn)的記錄,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
這篇文章主要為大家詳細(xì)介紹了Android音視頻之視頻采集,系統(tǒng)API預(yù)覽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android中Listview點(diǎn)贊功能的實(shí)現(xiàn)
最近一段時(shí)間在研究android方面的知識(shí),利用listview實(shí)現(xiàn)點(diǎn)贊功能,下面小編通過本文給大家介紹下基本思路,需要的朋友可以參考下2016-11-11
android屏幕圓角實(shí)現(xiàn)方法的示例代碼
本篇文章主要介紹了android屏幕圓角實(shí)現(xiàn)方法的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面,主要采用SharedPreferences來保存用戶數(shù)據(jù),感興趣的小伙伴們可以參考一下2016-05-05
Android連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作示例講解
這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作示例講解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Android編程使用android-support-design實(shí)現(xiàn)MD風(fēng)格對(duì)話框功能示例
這篇文章主要介紹了Android編程使用android-support-design實(shí)現(xiàn)MD風(fēng)格對(duì)話框功能,涉及Android對(duì)話框、視圖、布局相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
淺析Android Dialog中setContentView()方法
本篇文章主要介紹了Android Dialog中setContentView()方法的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05
Android開發(fā)壁紙的驗(yàn)證設(shè)置和確認(rèn)功能實(shí)現(xiàn)demo
android?wallpaper包括鎖屏壁紙和桌面壁紙,壁紙又區(qū)分靜態(tài)和動(dòng)態(tài)兩種。本文詳細(xì)介紹靜態(tài)壁紙?jiān)O(shè)置和確認(rèn),有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04

