android VideoView的使用常式

來源:互聯網
上載者:User

一、 xxxvideo.java

 

public class RemoteVideo extends Activity {

private String path = "";

private String textUrl = "";

private VideoView mVideoView;

private static int i = 0;

private int width;

private int heigh;

private Dialog dialog;

 

private Handler mHandler = new Handler();

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

 

requestWindowFeature(Window.FEATURE_NO_TITLE);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

 

Bundle bundle = this.getIntent().getExtras();

 

DisplayMetrics dm = new DisplayMetrics(); 

       getWindowManager().getDefaultDisplay().getMetrics(dm);

width=dm.widthPixels;

heigh=dm.heightPixels;

textUrl = bundle.getString("url");

if(width/heigh>0)

{

setContentView(R.layout.videoview);

 

path = bundle.getString("widthurl");

Log.i("mp4", "heng"+path);

}

if(width/heigh==0)

{

setContentView(R.layout.view);

 

path = bundle.getString("heighturl");

Log.i("mp4", "shu"+path);

}

 

dialog=ProgressDialog.show(this, "視頻載入中...", "請您稍候");

mVideoView = (VideoView) findViewById(R.id.surface_view);

mVideoView.setVideoPath(path);

MediaController controller = new MediaController(this);

mVideoView.setMediaController(controller);

mVideoView.requestFocus();

mVideoView.setOnPreparedListener(new OnPreparedListener() {

 

//@Override

public void onPrepared(MediaPlayer mp) {

mVideoView.setBackgroundColor(Color.argb(0, 0, 255, 0));

dialog.dismiss();

}

});

 

mVideoView.setOnCompletionListener(new OnCompletionListener() {

//@Override

public void onCompletion(MediaPlayer mp) {

Toast.makeText(RemoteVideo.this, "video play finished!", Toast.LENGTH_LONG)

.show();

}

});

controller.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

return true;

}

});

 

}

 

 

@Override

protected void onResume() {

super.onResume();

mVideoView.seekTo(i);

mVideoView.start();

}

@Override

protected void onStop() {

super.onStop();

mVideoView.pause();

i = mVideoView.getCurrentPosition();

}

@Override 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 

        finish();

            return true; 

        } 

        return false; 

    }

}

 

說明:

1. VideoView可接收本地和遠端視頻源並解碼;

2. MediaController為videoview的控制條類;

3. bundle擷取intent傳過來的視頻路徑;

 

二、videoview.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent"

android:gravity="left" android:orientation="horizontal">

<VideoView android:id="@+id/surface_view"

android:layout_width="176px" android:layout_height="132px"

android:layout_gravity="left" />

<WebView android:id="@+id/web_openonline"

android:layout_width="wrap_content" 

android:layout_height="wrap_content"/>

</LinearLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.