试图在Android中播放mp4视频文件

时间:2011-08-03 01:26:08

标签: eclipse

我尝试使用目录的字符串在代码中播放并尝试使用root删除root或在目录字符串中添加F:/ 但这一切都没有奏效。我得到我的Android手机错误“无法播放视频” 在它下面“对不起,这个视频无法播放。”

我的android上有一些.3gp视频,所以我将其中一个用我的电脑上的程序转换为.mp4但我仍然收到此错误。

这是代码

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);   
         videoPlayer.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;
         }
      }
   }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

MP4只是一个视频容器,可以包含各种格式。

您需要知道手机的硬件解码器支持哪种视频(可能是H264 Main Profile或Base Profile)。另请注意,某些硬件解码器在比特率和视频分辨率方面存在限制。

如果您的设备上有原生视频播放器,那么您对文件的最佳测试就是尝试与原生播放器一起播放。在这种情况下,它也很有可能与你的方法一起玩。