728x90
반응형
분할압축
package com.jd.player;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Player extends Activity
{
private static final String TAG = "JD";
public static final String EXTRA_SOUCE_TYPE = "PLAYER_SOURCE_TYPE";
public static final int TYPE_URI = 0;
public static final int TYPE_FILE = 1;
public static final int TYPE_STREAM = 2;
public static final int TYPE_RES = 3;
public static final String EXTRA_URI = "PLAYER_URISTRING";
public static final String EXTRA_FILENAME = "PLAYER_FILENAME";
public static final String EXTRA_HOST = "PLAYER_HOST";
public static final String EXTRA_RES_ID = "PLAYER_RES_ID";
private int sourceType = 0;
private String host = "";// "android.resource://com.jd.player";
private String filename = "";
private String uriString = "";
private int resId = 0;
FrameLayout mFlPlayerlayout;
private ImageView mIvBack;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder mHolder;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;
// private PowerManager.WakeLock mWakeLock = null;
private int mScreenWidth;
private int mScreenHeight;
private int mVideoWidth;
private int mVideoHeight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Intent intent = getIntent();
sourceType = intent.getIntExtra(EXTRA_SOUCE_TYPE, TYPE_URI);
host = intent.getStringExtra(EXTRA_HOST);
filename = intent.getStringExtra(EXTRA_FILENAME);
uriString = intent.getStringExtra(EXTRA_URI);
resId = intent.getIntExtra(EXTRA_RES_ID, R.raw.robot_dance);
mFlPlayerlayout = (FrameLayout) findViewById(R.id.flPlayer);
mFlPlayerlayout.setOnClickListener(new OnClickListener() {
public void onClick(View view)
{
if (mIvBack.getVisibility() == View.VISIBLE)
{
mIvBack.setVisibility(View.GONE);
}
else
{
mIvBack.setVisibility(View.VISIBLE);
}
}
});
mIvBack = (ImageView) findViewById(R.id.ivBack);
mIvBack.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
finish();
}
});
// initHolder
mPreview = (SurfaceView) findViewById(R.id.surface);
mHolder = mPreview.getHolder();
mHolder.addCallback(surfaceCallback);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// initHolder
Display disp = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
mScreenWidth = disp.getWidth();
mScreenHeight = disp.getHeight();
// final PowerManager powermanager = (PowerManager) getSystemService(Context.POWER_SERVICE);
// mWakeLock = powermanager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "P_Video_sleep");
// mWakeLock.setReferenceCounted(false);
Log.d(TAG, "onCrete done");
}
@Override
protected void onDestroy()
{
if (mMediaPlayer != null)
{
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
super.onDestroy();
}
private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Log.d(TAG, "****** surface changed ******");
Log.d(TAG, "w:" + width + ", h:" + height);
Log.d(TAG, "sw:" + mPreview.getWidth() + ", sh:" + mPreview.getHeight());
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.d(TAG, "****** surface Created ******");
initPlayer();
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.d(TAG, "****** surface Destroyed ******");
stopPlayer();
}
};
public void initPlayer()
{
try
{
Log.d(TAG, "init Player~ ");
// mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
switch (sourceType)
{
case TYPE_URI:
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(uriString));
break;
case TYPE_FILE:
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(filename);
break;
case TYPE_STREAM:
mMediaPlayer = new MediaPlayer();
break;
case TYPE_RES:
mMediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = getResources().openRawResourceFd(resId);
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength() );
break;
default:
break;
}
mMediaPlayer.setDisplay(mHolder);
mMediaPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnErrorListener(mOnErrorListener);
mMediaPlayer.prepare();
}
catch (IllegalStateException ex)
{
Log.d(TAG, "videoPlay IllegalStateException error");
return;
}
catch (IOException ex)
{
Log.d(TAG, "videoPlay IOException error");
return;
}
catch (IllegalArgumentException ex)
{
Log.d(TAG, "videoPlay IllegalArgumentException error");
return;
}
}
private OnBufferingUpdateListener mOnBufferingUpdateListener = new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.d(TAG, "onBufferingUpdate ~ " + percent);
}
};
private OnCompletionListener mOnCompletionListener = new OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
Log.d(TAG, "onCompletion ~ ");
finish();
}
};
private OnPreparedListener mOnPreparedListener = new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaplayer)
{
Log.d(TAG, "onPrepared ~ ");
startPlayer();
}
};
private OnVideoSizeChangedListener mOnVideoSizeChangedListener = new OnVideoSizeChangedListener() {
public void onVideoSizeChanged(MediaPlayer mediaplayer, int width, int height)
{
Log.d(TAG, "onVideoSizeChanged ~ " + width + ", " + height);
// setHolderFixedSize(width, height);
}
};
private OnErrorListener mOnErrorListener = new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra)
{
Log.d(TAG, "onError onError");
switch (what)
{
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Log.e(TAG, ">>>>>>>>MEDIA_ERROR_SERVER_DIED<<<<<<<<<<<<");
mMediaPlayer.release();
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
return true;
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Log.e(TAG, ">>>>>>>>MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK<<<<<<<<<<<<");
break;
default:
Log.e(TAG, ">>>>>>>>PLAYBACK_UNKNOWNERROR<<<<<<<<<<<< \n Sorry, this video cannot be played.");
break;
}
return false;
}
};
public void startPlayer()
{
Log.d(TAG, "start Player~ ");
// mHolder.setFixedSize(mVideoWidth, mVideoHeight);
setHolderFixedSize(mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight());
mMediaPlayer.start();
}
public void setHolderFixedSize(int width, int height)
{
float ratioX = (float) mScreenWidth / width;
float ratioY = (float) mScreenHeight / height;
if (ratioX > ratioY)
{
mVideoWidth = (int) (width * ratioY);
mVideoHeight = (int) (height * ratioY);
}
else
{
mVideoWidth = (int) (width * ratioX);
mVideoHeight = (int) (height * ratioX);
}
Log.d(TAG, "setHolderFixedSize : " + mVideoWidth + ", " + mVideoHeight + " // " + ratioX + ":" + ratioY);
// mVideoWidth = width;
// mVideoHeight = height;
// mHolder.setFixedSize(mVideoWidth, mVideoHeight);
mHolder.setFixedSize(width, height);
}
public void stopPlayer()
{
Log.d(TAG, "stop Player~ ");
if (mMediaPlayer != null)
{
mMediaPlayer.stop();
}
// if (mWakeLock.isHeld())
// {
// mWakeLock.release();
// }
}
}
728x90
반응형
'공부하는 하스씨 > 안드로이드' 카테고리의 다른 글
카메라 사용 개발 가이드 자료 수집. (0) | 2012.07.02 |
---|---|
안드로 버전별 히스토리 (0) | 2011.06.04 |
안드로이드에서 SQLite DB 사용하기 (0) | 2010.10.25 |
숨겨진 메소드 호출 트릭 (0) | 2010.04.28 |
다국어 사이트를 위한 문자셋과 코드페이지 (0) | 2010.02.24 |