开发者

Trying to play a mp4 video file in Android

开发者 https://www.devze.com 2023-03-25 22:49 出处:网络
I tried to play in the code with the string of the directory and tried with the root like removing the root or in the directory string to add F:/

I tried to play in the code with the string of the directory and tried with the root like removing the root or in the directory string to add F:/ But none of this worked. Im getting on my android phone error "Cannot play video" And under it "sorry, this video cannot be played."

I have some .3gp videos on my android so i converted one of them with a program on my pc to .mp4 but im still getting this error.

This is the code

package com.lightcone.playingvideo;

   import java.io.File;

   import android.app.Activity;
   import android.media.MediaPlayer;
   import android.media.MediaPlayer.OnCompletionListener;
   import android.media.MediaPlayer.OnPreparedListener;
   import android.os.Bundle;
   import android.os.Environment;
   import android.view.MotionEvent;
   import android.widget.VideoView;

   public class PlayingVideo extends Activity implements OnCompletionListener, OnPreparedListener {

      static private final String pathToFile = "DCIM/100MEDIA/VIDEO0030.mp4";  // 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.main);

         // Find the root of the external storage file system.  We assume the file system is
         // mounted and writable (see the project WriteSDCard for ways to check this).

         File root = Environment.getExternalStorageDirectory(); 

         // Assign a VideoView object to the video player and set its properties.  It
         // will be started by the onPrepared(MediaPlayer vp) callback below when the
         // file is ready to play.

         videoPlayer = (VideoView) findViewById(R.id.videoPlayer);   
         vi开发者_如何学GodeoPlayer.setOnPreparedListener(this);
         videoPlayer.setOnCompletionListener(this);
         videoPlayer.setKeepScreenOn(true);    
         videoPlayer.setVideoPath(root + "/" + pathToFile);
      }

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

         // Don't start until ready to play.  The arg of seekTo(arg) is the start point in
         // milliseconds from the beginning. In this example we start playing 1/5 of
         // the way through the video if the player can do forward seeks on the video.

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

Thanks for help.


MP4 is just a video container and can contain all kinds of formats.

You need to know which kind of video the hardware decoder of your phone supports (probably something like H264 Main Profile or Base Profile). Also be aware that some hardware decoders have limitations on bit rate and video resolution.

If you have a native Video Player on your device the best test for you files would be to try to play it with the native player. In that case, chances are very high that it plays with your approach as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号