简体   繁体   中英

Android Application not playing MP4 from SD Card

I am trying to get an mp4 playing from the SD Card. This is my second day at it. Yesterday it wouldn't work at all and today is plays the first second of the video before the application crashes with the error: android.view.WindowManager$BadToxenException: Unable to add window -- token null is not valid; is your activity running?

I have a playvideo.xml file in the layout folder as such:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   >
<VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>
</LinearLayout>

and in the java code - only a few short lines:

path = Environment.getExternalStorageDirectory() + "/videos/video.mp4";
                    Log.i("path", path);

                     setContentView(R.layout.playvideo);
                       VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
                       myVideoView.setVideoPath(path);
                       myVideoView.setMediaController(new MediaController(getBaseContext()));
                       myVideoView.requestFocus();
                       myVideoView.start();

Like I said, the video plays for a second and then the application crashes.

Any ideas?

Thanks.

Instead of

myVideoView.setMediaController(new MediaController(getBaseContext()));

use

myVideoView.setMediaController(new MediaController(this));

This should work.

try this code

VideoView videoView = (VideoView)findViewById(R.id.myvideoview);

             videoView.setOnCompletionListener(this);//checks when end

             File path = new File(mPath + "/" + mDownloadedFile);

             videoView.setVideoPath(path.getAbsolutePath());
             MediaController mediaController = new MediaController(this);
             mediaController.setMediaPlayer(videoView);
             mediaController.setAnchorView(videoView);

             videoView.setMediaController(mediaController);
             videoView.requestFocus();
             videoView.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM