在清单文件(AndroidManifest.xml)要加入读取内存卡的权限
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.g150825_android11_zuoye"><!--添加使用内存卡的权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/APPTheme"> <activity android:name=".VodeoPlayActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>布局文件 action_local_video.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_local_video" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.g150825_android11_zuoye.LocalVideoActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="300dp"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout></RelativeLayout>LocalVideoActivity
public class LocalVideoActivity extends AppCompatActivity { PRivate VideoView videoView ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_local_video); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //全屏 //本地的视频 需要在手机SD卡根目录添加一个 fl1234.mp4 视频// String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath(); String videoUrl1 = Environment.getExternalStorageDirectory().getAbsolutePath()+"/storage/emulated/0/f11234.mp4" ; Log.i("test","ddddddddddddd"+Environment.getExternalStorageDirectory().getPath()); Log.i("test",""+Environment.getExternalStorageDirectory().getPath()); //网络视频 // String videoUrl2 = Utils.videoUrl ; Uri uri = Uri.parse( videoUrl1 ); 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( LocalVideoActivity.this, "播放完成了", Toast.LENGTH_SHORT).show(); } } public class Utils { public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ; }}新闻热点
疑难解答