title: Android MediaPlayer doesn't show the video, only plays the audio of video file
body: <p>I am trying to play a video in my Android application. But it only plays the audio of video and don't show anything on screen!</p>
  
  <p>Here is my java activity file:</p>
  
  <pre><code>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();
      }
  
  }
  </code></pre>
  
  <p>and there is my xml activity layout:</p>
  
  <pre><code>&lt;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" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hamed" /&gt;
  
      &lt;SurfaceView 
          android:id="@+id/surface" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:paddingTop="10dip" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I also viewed these questions but I didn't get my answer: </p>
  
  <p><a href="https://stackoverflow.com/questions/7509818/android-mediaplayer-sometimes-no-video-is-played-even-though-audio-plays">Android MediaPlayer - Sometimes No Video is Played Even Though Audio Plays</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9014720/android-mediaplayer-does-not-display-video-only-play-audio">Android MediaPlayer does not display video, only play audio</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9302611/mediaplayer-only-playing-audio-not-audiovideo">https://stackoverflow.com/questions/9302611/mediaplayer-only-playing-audio-not-audiovideo</a></p>
  
  <p>I guess that I should set something in xml activity layout but I didn't it.
  Would you please let me know your suggestions?</p>