版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android筆記視屏播放、VideoView、surfaceView,簡易視頻播放
一、VideoView方法1.activity_video.xml<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoActivity"
>
<VideoView
android:id="@+id/video_videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>2.代碼package
com.example.vediotest;
import
android.media.MediaPlayer;
import
.Uri;
import
android.os.Bundle;
import
android.app.Activity;
import
android.content.pm.ActivityInfo;
import
android.view.Menu;
import
android.view.Window;
import
android.view.WindowManager;
import
android.widget.MediaController;
import
android.widget.VideoView;
public
class
VideoActivity
extends
Activity
{
private
VideoView
videoView;
private
Uri
mUri;
private
int
mPositionWhenPaused;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//
設(shè)置成全屏模式
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//
強(qiáng)制為橫屏
setContentView(R.layout.activity_video);
String
url
=
"/Upload/Video/File/20140411/201404110228168972.mp4";
//
String
url
=
//
"/player.php/sid/XNDYwOTEzNzQ4/v.swf";
mUri
=
Uri.parse(url);
videoView
=
(VideoView)
findViewById(R.id.video_videoView);
MediaController
mediaController
=
new
MediaController(this);
videoView.setMediaController(mediaController);
//
videoView.setVideoPath("/sdcard/xyx.3gp");
//
videoView.setVideoURI(mUri);
//
videoView.requestFocus();
//
videoView.start();
}
public
void
onStart()
{
//
Play
Video
videoView.setVideoURI(mUri);
videoView.start();
super.onStart();
}
public
void
onPause()
{
//
Stop
video
when
the
activity
is
pause.
mPositionWhenPaused
=
videoView.getCurrentPosition();
videoView.stopPlayback();
super.onPause();
}
public
void
onResume()
{
//
Resume
video
player
if
(mPositionWhenPaused
>=
0)
{
videoView.seekTo(mPositionWhenPaused);
mPositionWhenPaused
=
-1;
}
super.onResume();
}
public
boolean
onError(MediaPlayer
player,
int
arg1,
int
arg2)
{
return
false;
}
public
void
onCompletion(MediaPlayer
mp)
{
this.finish();
}
}二、surfaceView方法(一)1.activity_video_surface.xml<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="標(biāo)題"
/>
<SurfaceView
android:id="@+id/surfaceVideo_surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
</SurfaceView>
</LinearLayout>2.代碼package
com.example.vediotest;
import
android.media.AudioManager;
import
android.media.MediaPlayer;
import
android.media.MediaPlayer.OnBufferingUpdateListener;
import
android.media.MediaPlayer.OnCompletionListener;
import
android.media.MediaPlayer.OnPreparedListener;
import
.Uri;
import
android.os.Bundle;
import
android.app.Activity;
import
android.util.Log;
import
android.view.Menu;
import
android.view.SurfaceHolder;
import
android.view.SurfaceHolder.Callback;
import
android.view.SurfaceView;
public
class
SurfaceVideoActivity
extends
Activity
implements
Callback,
OnBufferingUpdateListener,
OnCompletionListener,
OnPreparedListener
{
private
int
width
=
0;
private
int
height
=
0;
private
MediaPlayer
mMediaPlayer
=
null;
private
SurfaceView
mSurfaceView
=
null;
private
SurfaceHolder
holder
=
null;
private
String
path
=
"";
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_surface);
mSurfaceView
=
(SurfaceView)
this.findViewById(R.id.surfaceVideo_surfaceView);
holder
=
mSurfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//
設(shè)置風(fēng)格
}
public
void
playVedio()
{
try
{
path
=
android.os.Environment.getExternalStorageDirectory()
+
"/moto_0012.3gp";
mMediaPlayer
=
new
MediaPlayer();
//
mMediaPlayer.setDataSource(path);
String
url
=
"/Upload/Video/File/20140411/201404110228168972.mp4";
//
String
url
=
"/player.php/sid/XNDYwOTEzNzQ4/v.swf";
mMediaPlayer.setDataSource(this,
Uri.parse(url));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();//
準(zhǔn)備
Log.e("TAG-Duration",
mMediaPlayer.getDuration()
+
"");
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
}
catch
(Exception
ex)
{
}
}
public
void
onBufferingUpdate(MediaPlayer
mp,
int
percent)
{
//
TODO
Auto-generated
method
stub
Log.i("TAG-onBufferingUpdate",
percent
+
"|"
+
mMediaPlayer.getCurrentPosition());
}
public
void
onCompletion(MediaPlayer
mp)
{
//
TODO
Auto-generated
method
stub
Log.i("TAG-onCompletion",
"Completion");
}
public
void
onPrepared(MediaPlayer
mp)
{
//
TODO
Auto-generated
method
stub
width
=
mMediaPlayer.getVideoWidth();
height
=
mMediaPlayer.getVideoHeight();
if
(width
!=
0
&&
height
!=
0)
{
holder.setFixedSize(width,
height);//
設(shè)置視頻高寬
mMediaPlayer.start();
Log.i("TAG-Duration2",
mMediaPlayer.getDuration()
+
"");
}
}
public
void
surfaceChanged(SurfaceHolder
holder,
int
format,
int
width,
int
height)
{
//
TODO
Auto-generated
method
stub
}
public
void
surfaceCreated(SurfaceHolder
holder)
{
//
TODO
Auto-generated
method
stub
playVedio();
}
public
void
surfaceDestroyed(SurfaceHolder
holder)
{
//
TODO
Auto-generated
method
stub
Log.i("TAG-surfaceDestroyed",
"surfaceDestroyed");
}
@Override
protected
void
onPause()
{
super.onPause();
if
(mMediaPlayer
!=
null)
{
if
(mMediaPlayer.isPlaying())
{
mMediaPlayer.stop();
}
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer
=
null;
}
}
}(二)activity_video_surface2.xml<RelativeLayout
xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="@+id/surface2_surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
</SurfaceView>
</RelativeLayout>2.代碼package
com.example.vediotest;
import
java.io.IOException;
import
android.media.AudioManager;
import
android.media.MediaPlayer;
import
android.media.MediaPlayer.OnBufferingUpdateListener;
import
android.media.MediaPlayer.OnCompletionListener;
import
.Uri;
import
android.os.Bundle;
import
android.app.Activity;
import
android.content.pm.ActivityInfo;
import
android.util.Log;
import
android.view.Menu;
import
android.view.SurfaceHolder;
import
android.view.SurfaceView;
import
android.view.Window;
public
class
SurfaceVideo2Activity
extends
Activity
implements
OnBufferingUpdateListener,
OnCompletionListener,
MediaPlayer.OnPreparedListener,
SurfaceHolder.Callback
{
private
MediaPlayer
mediaPlayer;
private
SurfaceView
surfaceView;
private
SurfaceHolder
surfaceHolder;
private
int
videoWidth;
private
int
videoHeight;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_video_surface2);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//
強(qiáng)制為橫屏
this.surfaceView
=
(SurfaceView)
this.findViewById(R.id.surface2_surfaceView);
this.surfaceHolder
=
this.surfaceView.getHolder();
this.surfaceHolder.addCallback(this);
this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Log.v("cat",
">>>create
ok.");
}
private
void
playVideo()
throws
IllegalArgumentException,
IllegalStateException,
IOException
{
String
url
=
"/Upload/Video/File/20140411/201404110228168972.mp4";
//
String
url
=
"/player.php/sid/XNDYwOTEzNzQ4/v.swf";
this.mediaPlayer
=
new
MediaPlayer();
//
this.mediaPlayer.setDataSource("/sdcard/daoxiang.3gp");
this.mediaPlayer.setDataSource(this,
Uri.parse(url));
this.mediaPlayer.setDisplay(this.surfaceHolder);
this.mediaPlayer.prepare();
this.mediaPlayer.setOnBufferingUpdateListener(this);
this.mediaPlayer.setOnPreparedListener(this);
this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.i("mplayer",
">>>play
video");
}
@Override
public
void
surfaceChanged(SurfaceHolder
arg0,
int
arg1,
int
arg2,
int
arg3)
{
Log.i("cat",
">>>surface
changed");
}
@Override
public
void
surfaceCreated(SurfaceHolder
holder)
{
try
{
this.playVideo();
}
catch
(Exception
e)
{
Log.i("cat"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年制衣面料供應(yīng)居間合同
- 2025版小企業(yè)合同管理規(guī)范與合同管理信息化解決方案3篇
- 2025年超額展覽會(huì)保險(xiǎn)條款
- 二零二五版新型環(huán)保建材采購合同樣本2篇
- 2025版企事業(yè)單位食堂員工招聘與服務(wù)協(xié)議3篇
- 2024-2025年中國寬帶行業(yè)市場評估分析及投資發(fā)展盈利預(yù)測報(bào)告
- 2025版小額貸款合同簽訂中的合同簽訂中的合同簽訂前的準(zhǔn)備與協(xié)商3篇
- 二零二五年度門面房裝修工程設(shè)計(jì)與施工質(zhì)量監(jiān)理合同
- 2025版建筑行業(yè)設(shè)備托管正規(guī)范本3篇
- 二零二五年度游艇俱樂部船舶租賃售后服務(wù)合同
- 2024年高考語文備考之??甲骷易髌罚ㄏ拢褐袊F(xiàn)當(dāng)代、外國
- 《裝配式蒸壓加氣混凝土外墻板保溫系統(tǒng)構(gòu)造》中
- T-CSTM 01124-2024 油氣管道工程用工廠預(yù)制袖管三通
- 2019版新人教版高中英語必修+選擇性必修共7冊詞匯表匯總(帶音標(biāo))
- 新譯林版高中英語必修二全冊短語匯總
- 基于自適應(yīng)神經(jīng)網(wǎng)絡(luò)模糊推理系統(tǒng)的游客規(guī)模預(yù)測研究
- 河道保潔服務(wù)投標(biāo)方案(完整技術(shù)標(biāo))
- 品管圈(QCC)案例-縮短接臺(tái)手術(shù)送手術(shù)時(shí)間
- 精神科病程記錄
- 閱讀理解特訓(xùn)卷-英語四年級上冊譯林版三起含答案
- 清華大學(xué)考博英語歷年真題詳解
評論
0/150
提交評論