【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android筆記視屏播放、VideoView、surfaceView簡易視頻播放_(tái)第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android筆記視屏播放、VideoView、surfaceView簡易視頻播放_(tái)第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android筆記視屏播放、VideoView、surfaceView簡易視頻播放_(tái)第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android筆記視屏播放、VideoView、surfaceView簡易視頻播放_(tái)第4頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android筆記視屏播放、VideoView、surfaceView簡易視頻播放_(tái)第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論