Android - 为什么我的应用程序不断弹出“视频无法播放屏幕”

发布于 2025-01-02 18:31:48 字数 2179 浏览 1 评论 0原文

代码有问题吗?有什么原因不能播放吗?

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.widget.VideoView;

public class Video extends Activity implements OnCompletionListener, OnPreparedListener {

        static private final String pathToFile ="com.chich.res.drawable-  hdpi.troopers.3gp";  // Video source file
       private VideoView videoPlayer;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.video);


      videoPlayer = (VideoView) findViewById(R.id.videoPlayer);   
      videoPlayer.setOnPreparedListener(this);
      videoPlayer.setOnCompletionListener(this);
      videoPlayer.setKeepScreenOn(true);    
      videoPlayer.setVideoPath(pathToFile);
       }


       /** This callback will be invoked when the file is ready to play */
       @Override
       public void onPrepared(MediaPlayer vp) {


          if(videoPlayer.canSeekForward()) videoPlayer.seekTo(videoPlayer.getDuration()/5);
          videoPlayer.start();
        }

        /** This callback will be invoked when the file is finished playing */
        @Override
        public void onCompletion(MediaPlayer  mp) {
          // Statements to be executed when the video finishes.
          this.finish();    
        }

        /**  Use screen touches to toggle the video between playing and paused. */
        @Override
        public boolean onTouchEvent (MotionEvent ev){   
           if(ev.getAction() == MotionEvent.ACTION_DOWN){
              if(videoPlayer.isPlaying()){
                       videoPlayer.pause();
              } else {
                       videoPlayer.start();
              }
              return true;      
           } else {
               return false;
           }
        }
     }

is there something wrong with the code? Some reason it won't be played?

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.widget.VideoView;

public class Video extends Activity implements OnCompletionListener, OnPreparedListener {

        static private final String pathToFile ="com.chich.res.drawable-  hdpi.troopers.3gp";  // Video source file
       private VideoView videoPlayer;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.video);


      videoPlayer = (VideoView) findViewById(R.id.videoPlayer);   
      videoPlayer.setOnPreparedListener(this);
      videoPlayer.setOnCompletionListener(this);
      videoPlayer.setKeepScreenOn(true);    
      videoPlayer.setVideoPath(pathToFile);
       }


       /** This callback will be invoked when the file is ready to play */
       @Override
       public void onPrepared(MediaPlayer vp) {


          if(videoPlayer.canSeekForward()) videoPlayer.seekTo(videoPlayer.getDuration()/5);
          videoPlayer.start();
        }

        /** This callback will be invoked when the file is finished playing */
        @Override
        public void onCompletion(MediaPlayer  mp) {
          // Statements to be executed when the video finishes.
          this.finish();    
        }

        /**  Use screen touches to toggle the video between playing and paused. */
        @Override
        public boolean onTouchEvent (MotionEvent ev){   
           if(ev.getAction() == MotionEvent.ACTION_DOWN){
              if(videoPlayer.isPlaying()){
                       videoPlayer.pause();
              } else {
                       videoPlayer.start();
              }
              return true;      
           } else {
               return false;
           }
        }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2025-01-09 18:31:48

如果您的视频是应用程序资源的一部分,我认为 setVideoPath 不起作用。使用 URI:使用 URI 引用 Android 资源。有一个使用 VideoView 的示例。

I don't think setVideoPath will work if your video is part of your apps resources. Use a URI: Referring to Android resources using URIs. There's an example of using the VideoView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文