Android:视频未从原始文件夹播放

Android: Video not playing from raw folder

我想在 Videoview 中播放 .mp4 视频,但无法播放。生成错误 "Sorry! video cannot be played"

public class MainActivity extends Activity {

    VideoView videoview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoview=(VideoView)findViewById(R.id.video);
        videoview.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.snowfall));
        videoview.start();

    }

如有任何帮助,我们将不胜感激。谢谢!

当框架尚未准备好播放视频时会引发此错误。你的 videoview.start() 被叫得太早了。添加侦听器以在视频准备就绪时开始播放。 将 videoview.start() 替换为:

    videoview.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
           videoview.start();
        }
    });

此代码将有效...!!

  Uri uri= Uri.parse("android.resource://" + getPackageName() + "/"
    + R.raw.video);
    videoView.setVideoURI(uri);
     videoView.requestFocus();
  videoView.start();

  //loop the video

      videoview.setOnPreparedListener(new OnPreparedListener() {

     @Override
     public void onPrepared(MediaPlayer mp) {
             mp.setLooping(true);

     }
 });