Get video width and height of MediaPlayer and update SurfaceView LayoutParams accordingly
Last article demonstrate how to Play Youtube 3gp video with MediaPlayer in SurfaceView. It can be noticed that the video is fill in parent view, because "fill_parent" are defined in layout.To adjust the video fit the Youtube video width and height, we can implement our custom OnVideoSizeChangedListener(), and call myMediaPlayer.getVideoWidth() and myMediaPlayer.getVideoHeight() to get the video width and height. After then, update SurfaceView LayoutParams with correct size accordingly.
Modify the Java code:
package com.example.androidyoutubeplayer;
import java.io.IOException;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements SurfaceHolder.Callback{
MediaPlayer myMediaPlayer;
SurfaceView mySurfaceView;
SurfaceHolder mySurfaceHolder;
Button btnPlay, btnStop;
TextView textVideoSize;
String path3gp = "rtsp://v8.cache6.c.youtube.com/CjYLENy73wIaLQkP0kiLmutogRMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKjR78WV1ZH5Tgw=/0/0/0/video.3gp";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFormat(PixelFormat.UNKNOWN);
mySurfaceView = (SurfaceView)findViewById(R.id.mediasurface);
mySurfaceHolder = mySurfaceView.getHolder();
mySurfaceHolder.addCallback(this);
myMediaPlayer = new MediaPlayer();
btnPlay = (Button)findViewById(R.id.play);
btnStop = (Button)findViewById(R.id.stop);
btnPlay.setOnClickListener(playOnClickListener);
btnStop.setOnClickListener(stopOnClickListener);
textVideoSize = (TextView)findViewById(R.id.size);
}
OnClickListener playOnClickListener
= new OnClickListener(){
@Override
public void onClick(View v) {
myMediaPlayer.reset();
myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
myMediaPlayer.setDisplay(mySurfaceHolder);
try {
myMediaPlayer.setOnVideoSizeChangedListener(videoSizeChangedListener);
myMediaPlayer.setDataSource(path3gp);
myMediaPlayer.prepare();
/*
* if you get Video Width and Height after prepare() here, it always return 0, 0!
*/
int videoWidth = myMediaPlayer.getVideoWidth();
int videoHeight = myMediaPlayer.getVideoHeight();
textVideoSize.setText(String.valueOf(videoWidth) + " x " + String.valueOf(videoHeight));
myMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
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();
}
}};
OnClickListener stopOnClickListener
= new OnClickListener(){
@Override
public void onClick(View v) {
if(myMediaPlayer.isPlaying()){
myMediaPlayer.stop();
}
}};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
/*
* Get Video Width and Height in OnVideoSizeChangedListener,
* and update SurfaceView LayoutParams accordingly.
*/
OnVideoSizeChangedListener videoSizeChangedListener
= new OnVideoSizeChangedListener(){
@Override
public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) {
int videoWidth = myMediaPlayer.getVideoWidth();
int videoHeight = myMediaPlayer.getVideoHeight();
textVideoSize.setText(String.valueOf(videoWidth) + " x " + String.valueOf(videoHeight));
mySurfaceView.setLayoutParams(
new LinearLayout.LayoutParams(videoWidth, videoHeight));
}};
}
Modify the layout file to add TextView to show the detected size.
<LinearLayout 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:orientation="vertical">
<Button
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="PLAY"/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="STOP"/>
<TextView
android:id="@+id/size"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<SurfaceView
android:id="@+id/mediasurface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Modify AndroidManifest.xml to add permission of "android.permission.INTERNET".
0 komentar:
Posting Komentar