RelativeLayout and MediaPlayer

41 views
Skip to first unread message

Irena Brudner

unread,
Jul 26, 2011, 11:26:24 AM7/26/11
to Android Media Developers
Hello,
I'm trying to play a live video, I'm displaying it with RelativeLayout
and MediaPlayer.

I've two problems, that I can't resolve.

First of all, upon running the application on Motorola Xoom, it works
great, however when I'm trying to run it on Android 2.X ( I tried it
on galaxy s, nexus s and droid devices), the video always stretches on
the screen. I'm trying to change the size of the holder, but
setFixSize has no influence on it.

In addition, while playing the video, after clicking on it, I'm trying
to change the size of the video to fit full screen. I tried destroying
the surface and the mediaplayer and creating a new ones and instead
the expected new stream, I've the old one stuck.

I'm attaching the code that I use, can you, please, advise me on these
matters?

Thanks,
Irena


public class RVVideoView extends RelativeLayout implements
RTSPServerEventHandler,

AndroidPlayerCoreEventHandler,

OnCompletionListener,

OnPreparedListener,

OnVideoSizeChangedListener
{
private ProgressBar m_progressBar = null;
private MediaPlayer m_mediaPlayer = null;
private SurfaceView m_surfaceView = null;
private SurfaceHolder m_holder = null;
private String m_url;
private boolean m_prepered = false;
private int m_id = 0;
private int m_videoWidth = 0;
private int m_videoHeight = 0;
private float m_ratio = 0;
private int m_normalScreenWidth = 0;
private int m_normalScreenHeight = 0;
private int m_fullScreenWidth = 0;
private int m_fullScreenHeight = 0;
private Activity m_parentActivity = null;
private boolean m_fullScreen = false;

/**
* RVVideoView constructor
*/
public RVVideoView(Activity parentActivity)
{
super((Context)parentActivity);

m_parentActivity = parentActivity;

createMainLayout();
createSurfaceLayout();
createProgressBar();
createP2PButton();

AndroidPlayerCore.sharedAndroidPlayerCore().setEventHandler(this);
}

private void createMainLayout()
{
LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
layout.addRule(LinearLayout.VERTICAL);
this.setGravity(Gravity.CLIP_HORIZONTAL|Gravity.CLIP_VERTICAL);
this.setLayoutParams(layout);
}

private void createSurfaceLayout()
{
LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.addRule(RelativeLayout.CENTER_IN_PARENT);
m_surfaceView = new SurfaceView(getContext());
m_surfaceView.setLayoutParams(layout);
m_surfaceView.setId(m_id);
this.addView(m_surfaceView, m_id, layout);
m_id++;

m_holder = m_surfaceView.getHolder();
m_holder.addCallback(this);
m_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

m_surfaceView.setOnClickListener(m_surfaceViewListener);
}

private void createProgressBar()
{
m_progressBar = new ProgressBar(getContext());
LayoutParams layout = new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layout.addRule(RelativeLayout.CENTER_IN_PARENT);
m_progressBar.setLayoutParams(layout);
m_progressBar.setVisibility(View.GONE);
m_progressBar.setId(m_id);
this.addView(m_progressBar, m_id, layout);
m_id++;
m_progressBar.setOnClickListener(m_progressBarListener);
}

private void setVideoSize()
{
int width =
m_parentActivity.getWindowManager().getDefaultDisplay().getWidth();
int height =
m_parentActivity.getWindowManager().getDefaultDisplay().getHeight();

if (width < height)
m_videoWidth = width;
else
m_videoWidth = height;
float fw = (float)m_videoWidth;
m_videoHeight = (int)(m_ratio*fw);

m_normalScreenWidth = m_videoWidth;
m_normalScreenHeight = m_videoHeight;


int newWidth = width;
int newHeight = (int)((((float)3)/((float)4))*(float)width);
if (newHeight > height) {
newHeight = height;
newWidth = (int)(m_ratio*((float)height));
}

m_fullScreenWidth = newWidth;
m_fullScreenHeight = newHeight;

}


public void setVideoURI(Uri uri)
{

if(uri.getScheme().equals("rayv"))
{
m_channel = uri.getSchemeSpecificPart();
}
}

//OnCompletionListener
@Override
public void onCompletion(MediaPlayer mp)
{
if (m_prepered == true) {
startMediaPlayer();
m_prepered = false;
}
}

//OnPreparedListener
@Override
public void onPrepared(MediaPlayer mp)
{
startMediaPlayer();
}

//RTSPServerEventHandler - received the url
@Override
public void mediaSegmentsReady(final String url)
{
m_url = url;
prepareMediaPlayer();
m_prepered = true;
}


//OnVideoSizeChangedListener
@Override
public void onVideoSizeChanged(MediaPlayer mediaPlayer, int width,
int height)
{
if (width != 0) {
m_ratio = ((float)width)/((float)height);
setVideoSize();
}
}

//Media Player methods
private void prepareMediaPlayer()
{
if (m_mediaPlayer != null)
return;

m_mediaPlayer = new MediaPlayer();
m_mediaPlayer.setScreenOnWhilePlaying(true);
m_mediaPlayer.setOnCompletionListener((OnCompletionListener) this);
m_mediaPlayer.setOnPreparedListener((OnPreparedListener) this);

m_mediaPlayer.setOnVideoSizeChangedListener((OnVideoSizeChangedListener)
this);
m_mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
m_mediaPlayer.setOnInfoListener (this);
m_mediaPlayer.setDisplay(m_holder);

Runnable r = new Runnable()
{
public void run()
{
try {
m_mediaPlayer.setDataSource(m_url);
m_mediaPlayer.prepare();
}
catch (Exception ex) {
android.util.Log.d("mediaSegmentsReady","exception: "+
ex.getMessage());
}
}
};
new Thread(r).start();
}

private void startMediaPlayer()
{
if (m_mediaPlayer == null)
return;
m_holder.setFixedSize(m_videoWidth, m_videoHeight);
m_mediaPlayer.start();
m_prepered = false;
if (m_controller == null)
return;
m_controller.setEnabled(true);
}

private void stopMediaPlayer()
{
if (m_mediaPlayer == null || (m_mediaPlayer != null && !
m_mediaPlayer.isPlaying()))
return;
m_mediaPlayer.stop();
m_prepered = false;
}

private void releaseMediaPlayer()
{
if (m_mediaPlayer == null)
return;
m_mediaPlayer.release();
m_mediaPlayer = null;
}

private void resumeMediaPalyer()
{
setPauseState(false);
startMediaPlayer();
}

private void pauseMediaPlayer()
{
setPauseState(true);
stopMediaPlayer();
releaseMediaPlayer();
m_mediaPlayer = null;
}


private OnClickListener m_surfaceViewListener = new
OnClickListener()
{
@Override
public void onClick(View v)
{
if (m_fullScreen == true)
setNormalScreen();
else
setFullScreen();
}
};

private void setFullScreen()
{
restartSurface();
m_fullScreen = true;
m_videoWidth = m_fullScreenWidth;
m_videoHeight = m_fullScreenHeight;
}

private void setNormalScreen()
{
restartSurface();
m_fullScreen = false;
m_videoWidth = m_normalScreenWidth;
m_videoHeight = m_normalScreenHeight;
}


private void restartSurface()
{
if (m_mediaPlayer == null)
return;
if (m_mediaPlayer.isPlaying() == false)
return;


releaseMediaPlayer();
m_surfaceView = null;
createSurfaceLayout();
prepareMediaPlayer();
startMediaPlayer();
}
}


Reply all
Reply to author
Forward
0 new messages