2011-08-03 52 views
0

我试图在代码中播放目录的字符串,并尝试使用root来删除根目录或在目录字符串中添加F:/ 但是没有一个能够工作。林进入我的Android手机错误“无法播放视频” 而在它下面“对不起,这个视频不能播放。”试图在Android中播放mp4视频文件

我在我的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; 
     } 
     } 
    } 

感谢您的帮助。

回答

0

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

您需要知道手机的硬件解码器支持哪种类型的视频(可能类似H264主配置文件或基本配置文件)。另请注意,某些硬件解码器对比特率和视频分辨率有限制。

如果您的设备上有本机视频播放器,则最适合您的文件测试方法是尝试使用本机播放。在这种情况下,它的机会非常高,它与你的方法一起玩。