티스토리 뷰

android

안드로이드 SurfaceView Video 예제

노명규 2020. 1. 31. 17:12

* xml

<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

 

* activity onCreate

mSurfaceView1 = (SurfaceView)findViewById(R.id.surfaceView1);

mSurfaceHolder1 = mSurfaceView1.getHolder();

mMediaPlayer1 = new MediaPlayer();

 

mSurfaceHolder1.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer1.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override
public void surfaceDestroyed(SurfaceHolder holder) { }
});

 

try {
mMediaPlayer1.setLooping(true);/**반복 여부 설정*/
mMediaPlayer1.setDataSource(context, getUri/**실행할 영상 uri*/);
mMediaPlayer1.prepareAsync();/**prepare사용해도 무방*/
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "onPrepared() 로딩 완료 리스너. ");
mp.start();
}
});

 

 

*onDestroy 에서 꼭 release해줘야 함(reset도 초기화는 초기화이지만 release 그 전 어딘가쯤까지 초기화하는듯)