2012-03-08 114 views
1

我想用mediaPlayer創建一個簡單的視頻播放器..所以我做了我在下面給出的代碼。但我在這裏遇到問題,因爲視頻不可見,但音頻正在播放。 在我的XML文件中,我使用了一個簡單的VideoView。android-從SD卡播放視頻

package com.Android.Simple; 

    import android.app.Activity; 
    import android.media.MediaPlayer; 
    import android.os.Bundle; 
    import android.view.SurfaceHolder; 
    import android.widget.VideoView; 

    public class SimpleVideoPlayerActivity extends Activity { 

     private VideoView _UIVideo; 
     SurfaceHolder _videoHolder; 

     public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      setContentView(R.layout.simple_video_view); 

      _UIVideo = (VideoView) findViewById(R.id.VVSimpleVideo); 
      MediaPlayer _mediaPlayer; 
      String _path = "/sdcard/abc.3gp"; 
      _videoHolder = _UIVideo.getHolder(); 

      try { 
       _mediaPlayer = new MediaPlayer(); 
       _mediaPlayer.setDataSource(_path); 
       _mediaPlayer.setDisplay(_videoHolder); 
       _mediaPlayer.prepare(); 
       _mediaPlayer.start(); 

      } catch (Exception e) { 
      } 
    } 

和清單文件:

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.Android.Simple" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="8" /> 

    <application 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/app_name" > 
      <activity 
       android:name=".SimpleVideoPlayerActivity" 
       android:label="@string/app_name" > 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 
     </application> 

    </manifest> 





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

     <VideoView 
      android:id="@+id/VVSimpleVideo" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

     </LinearLayout> 

回答

3
Try this Code 

     VideoView videoView = (VideoView)this.findViewById(R.id.videoView); 
     MediaController mc = new MediaController(this); 
     mc.setAnchorView(videoView); 
     mc.setMediaPlayer(videoView); 
     videoView.setMediaController(mc); 

     videoView.setVideoPath(path); 


     videoView.requestFocus(); 
     videoView.start(); 
+1

Sam,這是使用V ideoView ...我想使用MediaPlayer – Vivekanand 2012-03-08 08:53:04

2

u能顯示你的清單文件添加權限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
+0

節目清單文件 – 2012-03-08 08:18:23

+0

雅它就在那裏......我編輯的我不知道爲什麼代碼 – Vivekanand 2012-03-08 09:08:45

2

在Android開發者網站,你有非常簡單的演示:

Media Player

改變你的佈局:這樣

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

    <SurfaceView android:id="@+id/surface" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"> 
    </SurfaceView> 

</LinearLayout> 

,並開始活動:

Intent i = new Intent(getApplicationContext(), MediaPlayerDemo_Video.class); 
i.putExtra("path", path); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(i); 
+0

,由於某種原因,即使這個例子沒有顯示視頻...它只播放音頻 – Vivekanand 2012-03-08 09:08:04

+0

讓我看看你的佈局xml – 2012-03-08 09:10:35

+0

遐我做了....... – Vivekanand 2012-03-08 09:24:19