标签:ams protect internet 模拟器 emulated 方法 java bundle string
这是一个播放本地视频的播放器,videoUrl1是手机里放置视频的路径
效果图:
MainActivity.java
package com.example.aimee.videotest; import android.Manifest; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.WindowManager; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; public class MainActivity extends AppCompatActivity { private final int REQUESTCODE=101; private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){ int checkSelfPermission=checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if(checkSelfPermission== PackageManager.PERMISSION_DENIED){ requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUESTCODE); } } play_mp4(); } private void play_mp4(){ String videoUrl1 = "/storage/emulated/0/save/a1.mp4"; Uri uri = Uri.parse( videoUrl1 ); videoView = (VideoView)this.findViewById(R.id.videoView ); videoView.setMediaController(new MediaController(this)); videoView.setVideoURI(uri); videoView.start(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); if(requestCode==REQUESTCODE){ if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]== PackageManager.PERMISSION_GRANTED){ Toast.makeText(this,"ok", Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"无权限",Toast.LENGTH_LONG).show(); } } } }
layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/videoView" android:layout_width="wrap_content" android:layout_height="match_parent" /> </LinearLayout>
androidmanifest.xml中要添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" />
注意:使用模拟器播放时,要确保路径下有那个视频。从电脑传视频到模拟器的方法在上一篇中有。不过,每次传完当时可以用,不过重启电脑或重启模拟器时,那个视频文件会丢失,所以下次想用时得重新上传。我是用的ADV tools里面创建的模拟器,不知道其他模拟器会不会同样如此。
标签:ams protect internet 模拟器 emulated 方法 java bundle string
原文地址:https://www.cnblogs.com/smart-zihan/p/9914848.html