Use VideoView to play, pause, and fast forward videos.
1 <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 tools: context = "com. example. videoview. mainActivity "> 6 7 <VideoView 8 android: id =" @ + id/videoView "9 android: layout_width =" match_parent "10 android: layout_height = "416dp"/> 11 12 <LinearLayout13 android: id = "@ + id/linearLayout" 14 android: layout_width = "match_parent" 15 android: layout_height = "wrap_content" 16 android: layout_gravity = "bottom" 17 android: orientation = "horizontal"> 18 19 <Button20 android: id = "@ + id/start" 21 android: layout_width = "wrap_content" 22 android: layout_height = "wrap_content" 23 android: text = ""/> 24 25 <Button26 android: id = "@ + id/pause" 27 android: layout_width = "wrap_content" 28 android: layout_height = "wrap_content" 29 android: text = "pause"/> 30 31 <Button32 android: id = "@ + id/seekto" 33 android: layout_width = "wrap_content" 34 android: layout_height = "wrap_content" 35 android: text = "3 seconds"/> 36 </LinearLayout> 37 38 </FrameLayout>Activity_main.xml
Test master code:
1 package com.example.videotest; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.os.Environment; 8 import android.provider.MediaStore.Video; 9 import android.view.Menu;10 import android.view.MenuItem;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 import android.widget.VideoView;15 16 public class MainActivity extends Activity implements OnClickListener {17 18 private VideoView videoView;19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.activity_main);24 25 videoView = (VideoView) findViewById(R.id.videoView);26 27 File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);28 29 File f = new File(path, "/Camera/test.mp4");30 31 videoView.setVideoPath(f.getAbsolutePath());32 33 Button start = (Button) findViewById(R.id.start);34 start.setOnClickListener(this);35 Button pause = (Button) findViewById(R.id.pause);36 pause.setOnClickListener(this);37 Button seekto = (Button) findViewById(R.id.seekto);38 seekto.setOnClickListener(this);39 40 }41 42 @Override43 public void onClick(View v) {44 switch (v.getId()) {45 case R.id.start:46 videoView.start();47 break;48 case R.id.pause:49 videoView.pause();50 break;51 case R.id.seekto:52 videoView.seekTo(3*1000);53 break;54 }55 }56 57 }