//视频播放 //网络权限记得加 <uses-permission android:name="android.permission.INTERNET" /> public class MainActivity extends AppCompatActivity { private VideoView videoView ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // //本地的视频 需要在手机SD卡根目录添加一个 fl1234.mp4 视频 // String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/fl1234.mp4" ; //网络视频 String videoUrl2 = Utils.videoUrl ; Uri uri = Uri.parse( videoUrl2 ); videoView = (VideoView)this.findViewById(R.id.videoView ); //设置视频控制器 videoView.setMediaController(new MediaController(this)); //播放完成回调 videoView.setOnCompletionListener( new MyPlayerOnCompletionListener()); //设置视频路径 videoView.setVideoURI(uri); //开始播放视频 videoView.start(); } class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener { @Override public void onCompletion(MediaPlayer mp) { Toast.makeText( MainActivity.this, "播放完成了", Toast.LENGTH_SHORT).show(); } } } //<RelativeLayout // android:layout_width="match_parent" // android:layout_height="200dp"> //<VideoView // android:id="@+id/videoView" // android:layout_width="match_parent" // android:layout_height="match_parent" // /> //</RelativeLayout> //public class Utils { // public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ; //}
布局:
<RelativeLayout android:layout_width="match_parent" android:layout_height="200dp"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
Utils类:
public class Utils { public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ; }