//视频播放器
VideoView vv=(VideoView) findViewById(R.id.videoView1); //设置播放路径 ///data/media/0/local/abc.MP4 ///data/iToolsVMShare/minion_09.mp4 vv.setVideoPath("data/media/0/local/abc.MP4"); MediaController control=new MediaController(this); control.setMediaPlayer(vv); vv.setMediaController(control); //开始播放 vv.start();
//布局
<VideoView android:id="@+id/videoView1" android:layout_width="match_parent" android:layout_height="200dp"
//权限
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16" />
//音频播放器
public void mplay(View v){ if(falg){ //1.创建对象 mp=new MediaPlayer(); try { //2.设置数据源--sd卡的路径 /sdcard/xxx.mp3 // mp.setDataSource("/data/iToolsVMShare/bajie.mp3");//在iTools模拟器下 mp.setDataSource("/sdcard/Music/Over_the_horizon.mp3");//-在原生模拟器 //3.准备-进入准备状态 mp.prepare(); //4.开始播放 mp.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ //继续播 mp.start(); } } public void mpause(View v){ if(mp!=null){ mp.pause(); falg=false; } } public void mstop(View v){ if(mp!=null){ mp.stop(); falg=true; } }
//布局
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="36dp" android:layout_marginTop="60dp" android:text="播放" android:onClick="mplay" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="43dp" android:text="暂停" android:onClick="mpause" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="38dp" android:text="停止" android:onClick="mstop" />
//权限
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" />