Search code examples
androidvideomedia-playerplayback

Android - Why does my application keep bringing up the "Video can't be played screen"


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;
           }
        }
     }

Solution

  • 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.