package com.example.playvideotest; import java.io.File; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.VideoView; import android.app.Activity; 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(); } break; case R.id.replay: if (videoView.isPlaying()) { videoView.resume(); } break; } } @Override protected void onDestroy() { super.onDestroy(); if (videoView != null) { videoView.suspend(); } } }
<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>
时间: 2024-10-12 04:07:33