2011-08-03 46 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主配置文件或基本配置文件)。另請注意,某些硬件解碼器對比特率和視頻分辨率有限制。

如果您的設備上有本機視頻播放器,則最適合您的文件測試方法是嘗試使用本機播放。在這種情況下,它的機會非常高,它與你的方法一起玩。