我想在启动 Android 应用程序时播放视频文件。我试过了,但它不能正常工作。我收到一条错误消息,提示无法播放视频。我已将我的视频(.mp4 扩展名)保存在原始文件夹中。我设置了如下路径:
path = "android.resource://com.easy.video/" + R.raw.myvideo;
这是我的java文件。
package com.easy.video;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
import android.widget.VideoView;
@SuppressWarnings("unused")
public class MainActivity extends Activity {
private VideoView videoView;
String extStorageDirectory;
protected static final int PLAY = 0x101;
protected static final int STOP = 0x102;
protected static final int PAUSE = 0x103;
int State;
private String current;
private String path;
private VideoView mVideoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
path = "android.resource://com.easy.video/" + R.raw.myvideo;
mVideoView = (VideoView) findViewById(R.id.video);
if (path == null || path.length() == 0) {
Toast.makeText(MainActivity.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(path);
mVideoView.start();
mVideoView.requestFocus();
}
mVideoView.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
Intent in = new Intent(MainActivity.this, NextActivity.class);
startActivity(in);
finish();
}
});
}
}
其实我不确定它是否正确。如果有人能如此友好地解释如何做到这一点,我将不胜感激?