2013-03-31 98 views
1

我想在Android應用程序中播放視頻。但它只播放視頻的音頻,不會在屏幕上顯示任何內容!Android MediaPlayer不顯示視頻,只播放視頻文件的音頻

這裏是我的Java活動文件:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     try { 
      videoPlayer(); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void videoPlayer() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{ 
     Uri myUri = Uri.fromFile(new File("/sdcard/sub_sda1/a.mpg")); 
     MediaPlayer mediaPlayer = new MediaPlayer(); 
     SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.surface); 
     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     mediaPlayer.setDataSource(getApplicationContext(), myUri); 
     mediaPlayer.prepare(); 
     int videoWidth = mediaPlayer.getVideoWidth(); 
     int videoHeight = mediaPlayer.getVideoHeight(); 
     //Get the width of the screen 
     int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); 
     //Get the SurfaceView layout parameters 
     android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams(); 
     //Set the width of the SurfaceView to the width of the screen 
     lp.width = screenWidth; 
     //Set the height of the SurfaceView to match the aspect ratio of the video 
     //be sure to cast these as floats otherwise the calculation will likely be 0 
     lp.height = (int) (((float)videoHeight/(float)videoWidth) * (float)screenWidth); 
     //Commit the layout parameters 
     mSurfaceView.setLayoutParams(lp); 
     mediaPlayer.start(); 
    } 

} 

,並有我的XML活動佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hamed" /> 

    <SurfaceView 
     android:id="@+id/surface" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="10dip" /> 

</RelativeLayout> 

我也查看了這些問題,但我沒有得到我的回答:

Android MediaPlayer - Sometimes No Video is Played Even Though Audio Plays

Android MediaPlayer does not display video, only play audio

https://stackoverflow.com/questions/9302611/mediaplayer-only-playing-audio-not-audiovideo

我想我應該設置在XML活動佈局的東西,但我沒有它。 您能否讓我知道您的建議?

+0

你試過用VideoView嗎? –

+0

還沒有!但現在我正在尋找它。如果你有一個使用'VideoView'的工作代碼,請讓我知道。 – boomz

回答

4

嘗試使用VideoView

videoView = (VideoView)findViewById(R.id.VideoView);   
videoView.setVideoPath("/sdcard/your_video.3gp"); 
videoView.start(); 

希望這將幫助你。

+0

好吧,我試過這個,它適用於我:-)謝謝。但我想知道「MediaPlayer」問題。 – boomz

+2

@boomz在基於MediaPlayer的實現中,您需要調用一個'setSurface'來提供'output canvas',''decoder'可以在其上填充數據。你也可以嘗試'mediaplayer.setDisplay(mSurfaceView.getHolder());' – Ganesh

相關問題