简体   繁体   English

Android应用程序无法从SD卡播放MP4

[英]Android Application not playing MP4 from SD Card

I am trying to get an mp4 playing from the SD Card. 我正在尝试从SD卡播放mp4。 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; 昨天它根本不起作用,今天是在应用程序崩溃并播放以下错误之前播放视频的第一秒:android.view.WindowManager $ BadToxenException:无法添加窗口-令牌null无效; is your activity running? 您的活动正在进行吗?

I have a playvideo.xml file in the layout folder as such: 我在布局文件夹中有一个playvideo.xml文件,例如:

<?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: 在Java代码中-只有几行:

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 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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