2011-05-08 71 views
2

我在與Eclipse(我有一個.NET背景)Android的Java編程新手,達不到在模擬器的視頻播放(也不在Android的HTC Desire)的Android - 播放視頻不起作用

我的設置:

  • Eclipse IDE的Java開發人員,版本:太陽神服務版本2
  • Android SDK和ADT Eclipse插件

我使用的視頻是3GP視頻我RECO用我的HTC願望。

我已經試過2種方法與

  1. VideoView
  2. SurfaceView用MediaPlayer的 我拿出這本書從雷託邁耶「專業的Android 2應用開發」的。

我靶向的Android 2.2(無論是在模擬器,以及對我的HTC Desire)

詳細

1 VideoView方法

getDuration總是返回-1即使在調用videoView.start()之後,isPlaying也是false。 下面是我使用的代碼:

public class MyActivity extends Activity 
{ 
    private static final String FileName = "VIDEO0015.3gp"; 
    private static final String MyTag = "MyActivity"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    {    
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     VideoView videoView = (VideoView) findViewById(R.id.videoView1); 
     videoView.setKeepScreenOn(true); 

     File clip=new File(Environment.getExternalStorageDirectory(), FileName); 
     if (clip.exists()) 
     { 
      videoView.setVideoPath(clip.getAbsolutePath()); 
      int duration = videoView.getDuration(); 

      if (videoView.canSeekForward()) 
      { 
       videoView.seekTo(duration/2); 
      } 

      videoView.start(); 
     } 
    } 
} 

main.xml中的文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<VideoView android:id="@+id/videoView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</VideoView> 
</LinearLayout> 

2. SurfaceView與MediaPlayer的 通過這種方法,我得到正確的時間(大約15秒)並且打擊是真實的。 無論是什麼都看不到,視頻播放從未完成。

我會添加代碼,如果有人想檢查它。


感謝您的幫助,我越來越絕望。

弗蘭克

+0

似乎很奇怪,你可以發佈您的main.xml(不要忘了將它格式化爲代碼 - 只是將其標記,然後按Ctrl + K),並有你嘗試播放另一個視頻(沒有記錄在手機上)? – MByD 2011-05-08 11:04:50

+0

@MByD:感謝您的幫助。我已經添加了main.xml文件的內容。我在佈局設置上玩了一下,但沒有變化。我真的是一個新手,所以也許我做了一些根本性錯誤的事情。關於其他視頻,我嘗試了另一種視頻,但失敗時顯示不同的錯誤。因爲第二次嘗試(使用surfaceView和mediaPlayer)返回正確的持續時間,並說isPlaying = true我認爲視頻正常。我會檢查另一個視頻。是否有任何視頻可以保證正常工作? – FrankE 2011-05-09 17:05:32

+0

我試過了你的代碼,得到了相同的結果(有效的視頻),我不知道爲什麼,也許我會有時間來檢查它。 – MByD 2011-05-09 17:25:25

回答

0

我之前遇到過類似的問題。您是否嘗試「暗示」3gp視頻?你可以看看here。希望它會有所幫助。

0

嗨,我寫了一些測試視頻查看計劃之前。 我想也許是你的視頻文件路徑錯了? 這裏是我的測試代碼,

public class VideoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.video_view_layout); 

     VideoView videoView = (VideoView) findViewById(R.id.VideoView); 
     MediaController mediaController = new MediaController(this); 
     mediaController.setAnchorView(videoView); 

     Uri video = Uri.parse("/sdcard/test_video.mp4"); 
     videoView.setMediaController(mediaController); 
     videoView.setVideoURI(video); 
     videoView.start(); 
     } 
} 

和XML是,

<?xml version="1.0" encoding="utf-8"?> 
<VideoView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/VideoView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center"> 
</VideoView> 

AndroidManifest。xml,

 <activity android:name=".VideoActivity" 
        android:label="@string/app_name" 
        android:screenOrientation="landscape" 
        android:theme="@android:style/Theme.NoTitleBar" 
        android:noHistory="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

希望能幫助你。

+0

在這種情況下,視頻文件被放入/ sdcard根級別。 – Rebecca 2011-05-09 07:43:38

+0

感謝您的幫助。我試過你的代碼,但是我找到了_No啓動器活動!該推出只會同步設備上的應用程序包!_我是一個新手,所以我不知道問題的原因。我認爲我的視頻文件的路徑是可以的,因爲我在代碼中使用'File clip = new File(Environment.getExternalStorageDirectory(),FileName)來檢查它。 if(clip.exists())' – FrankE 2011-05-09 17:22:27

0

如果您還沒有找到解決方案,如果視頻未處於播放狀態,VideoView.getDuration()將返回-1。視頻在準備好之前未處於播放狀態。所以在設置URI後直接調用VideoView.getDuration()並不能保證視頻已經準備好。

我發現這個通過查看VideoView來源:

@Override 
public int getDuration() { 
    if (isInPlaybackState()) { 
     return mMediaPlayer.getDuration(); 
    } 

    return -1; 
} 

的解決方案是一個OnPreparedListener設置爲您VideoView,並獲得/使用時間,當影片播放準備。然後您可以使用VideoView.getDuration()MediaPlayer.getDuration(),它們幾乎相同。

解決方案:

public class MyActivity extends Activity { 
private static final String FileName = "VIDEO0015.3gp"; 
private static final String MyTag = "MyActivity"; 

/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    VideoView videoView = (VideoView) findViewById(R.id.videoView1); 
    videoView.setKeepScreenOn(true); 

    File clip = new File(Environment.getExternalStorageDirectory(), FileName); 
    if (clip.exists()) { 
     videoView.setVideoPath(clip.getAbsolutePath()); 
     videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       int duration = videoView.getDuration(); 
       if (videoView.canSeekForward()) { 
        videoView.seekTo(duration/2); 
        videoView.start(); 
       } 
      } 
     }); 
    } 
} 

}