我知道这个问题已经被问过很多次了。我已经尝试了前面提到的许多解决方案,例如提高API级别,更改android设备,更改视频,更改视频格式。每次相同的错误。我使用的是运行Oreo 8.1的Nexus 6p,最低API类似于Marshmellow。 xml代码:
<VideoView
android:background="@color/colorPrimaryDark"
android:layout_height="600dp"
android:layout_width="fill_parent"
android:id="@+id/VideoView" />
<Button
android:text="@string/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButtonClick"
android:background="@color/colorPrimary"
android:id="@+id/button"/>
java代码:
public class IntroVideoActivity extends AppCompatActivity {
VideoView VideoView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_video);
VideoView = (VideoView) findViewById(R.id.VideoView);
button = (Button) findViewById(R.id.button);
}
public void onButtonClick (View v) {
String videopath = "android.resource://com.example.nisat.favor"+R.raw.anime;
Uri uri = Uri.parse(videopath);
VideoView.setVideoURI(uri);
VideoView.start();
}
}
我也尝试过此代码:
VideoView videoView = (VideoView)findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath("vectorvideo");
videoView.start();
答案 0 :(得分:1)
视频必须采用受支持的格式(3gp,wmv,mp4),并以小写字母,数字,下划线和点号(在其文件名中依次命名为:video_file.mp4)。
代替
String videopath = "android.resource://com.example.nisat.favor"+R.raw.anime;
尝试
String videopath = "android.resource://" + getPackageName() + "/" + R.raw.anime;
答案 1 :(得分:0)
检查何时调用您的方法?
onButtonClicked(查看v)
尝试此代码,如果没有用,请先尝试不单击按钮,然后单击按钮时调用它。我在活动中使用了它,效果很好。
String basePath = "android.resource://" + getPackageName() +"/"+"/raw/";
Intent intent = getIntent();
String videoName = intent.getStringExtra(VIDEO_NAME);
final boolean videoRepeat = intent.getBooleanExtra(VIDEO_REPEAT, false);
Uri videoUri = Uri.parse(basePath+videoName);
VideoView videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoPlayer);
mc.setMediaPlayer(videoPlayer);
mc.setVisibility(View.GONE);
videoPlayer.setMediaController(mc);
videoPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if(!videoRepeat){
finish();
}
}
});
videoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if(videoRepeat) {
mp.setLooping(true);
}else{
mp.setLooping(false);
}
}
});
videoPlayer.setVideoURI(videoUri);
videoPlayer.start();
答案 2 :(得分:0)
use this code
mention permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
and this line of code inside the MainActivity.class
String videopath = "android.resource://com.example.cloudanalogy.play_video/"+R.raw.a;
Uri vidUri = Uri.parse(videopath);
videoView.setVideoURI(vidUri);
videoView.start();
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);