我正在尝试创建一个在后台播放视频的开始屏幕。我有一个名为" bday"的mp4文件。在原始文件夹中。运行项目时没有错误,但最后一个对话框给出了msg"无法播放此文件"
java
public class MainActivity extends Activity implements SurfaceHolder.Callback{
MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
getWindow().setFormat(PixelFormat.UNKNOWN);
//Displays a video file.
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
String uriPath = "android.resource://com.example.media/"+R.raw.bday;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// VideoView refference see main.xml
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
String uriPath = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.bday;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
String path1 = "";
if(vid_path!=""||vid_path!=null)
path1 = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.bday;
else{
Toast.makeText(this, "Video path not found", Toast.LENGTH_LONG).show();
finish();
}
final MediaController mc = new MediaController(this);
mc.setAnchorView(video);
mc.setMediaPlayer(video);
Uri uri = Uri.parse(path1);
video.setMediaController(mc);
System.out.println("!-- uri: "+uri.toString());
video.setVideoURI(uri);
video.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mProgressDialog.dismiss();
}
});
video.start();
video.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
}
public void playVideo(View v) {
}
public void showLoaderProgress(){
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading Video\nPlease wait...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
尝试上述代码。在showLoaderProgress()
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);