Foggy day

Android - MediaPlayer with surfaceView 본문

Android

Android - MediaPlayer with surfaceView

jinhan38 2021. 2. 17. 00:05

This article is an simple example of mediaplayer using surfaceview.

 

The reason that i make this sample, it is to play video without audiofocus. If you use VideoView, it catch audiofocus. But MediaPlayer doesn't catch audiofocus. 

 

 

 

Video

 

 

 

 

 

 

 

1. add video file

Add video file in res -> raw. In my case, video file is mp4 type. 

 

 

 

2. draw xml.

All you need is a SurfaceView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/llContainer"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

 

 

 

3. wirte code

Somethings you need are MediaPlayer, SurfaceView, and SurfaceHolder.

Nothing complicated, just follow the code below.

 

public class VideoAudioFocusControlJava extends AppCompatActivity implements SurfaceHolder.Callback {

    private static final String TAG = "VideoAudioFocusControl";



    MediaPlayer player;
    SurfaceView surfaceView;
    SurfaceHolder holder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_audio_focus_control_activity);


        try {

            player = new MediaPlayer();
            player = MediaPlayer.create(this, getResources().getIdentifier("main1", "raw", getPackageName()));
            surfaceView = findViewById(R.id.surfaceView);
            holder = surfaceView.getHolder();
            holder.addCallback(this);

        } catch (Exception e) {
            Log.d(TAG, "onCreate: error : " + e);
        }

    }


    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        Toast.makeText(getApplicationContext(), "surfaceCreated", Toast.LENGTH_LONG).show();
        player.setDisplay(holder);
        try {
            player.prepare();
        } catch (Exception e) {
//            Log.e(TAG,"ERROR",e);
        }
        player.start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
        Toast.makeText(getApplicationContext(), "surfaceDestroyed", Toast.LENGTH_LONG).show();
    }



}

 

 

 

 

 

4. kotlin code

class VideoAudioFocusControlKotlin : AppCompatActivity(), SurfaceHolder.Callback {


    companion object{
        private const val TAG = "VideoAudioFocusControlK"
    }

    lateinit var player: MediaPlayer;
    lateinit var surfaceView: SurfaceView;
    lateinit var holder: SurfaceHolder;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.video_audio_focus_control_activity)

        try {
            player = MediaPlayer()
            player = MediaPlayer.create(this, resources.getIdentifier("main1", "raw", packageName))
            surfaceView = findViewById(R.id.surfaceView)
            holder = surfaceView.holder
            holder.addCallback(this)

        } catch (e: Exception) {
            Log.d(TAG, "onCreate: error : $e")
        }

    }

    override fun surfaceCreated(p0: SurfaceHolder) {
        player.setDisplay(holder)
        player.prepare()
        player.start()
    }

    override fun surfaceChanged(p0: SurfaceHolder, p1: Int, p2: Int, p3: Int) {
        TODO("Not yet implemented")
    }

    override fun surfaceDestroyed(p0: SurfaceHolder) {
        player.stop()
        TODO("Not yet implemented")
    }
}

 

 

 


    /**
     * surfaceview에 동영상 비율 맞춰서 넣기
     */
    private void setSurfaceViewProportion() {

        // Get the dimensions of the video
        int videoWidth = player.getVideoWidth();
        int videoHeight = player.getVideoHeight();
        float videoProportion = (float) videoWidth / (float) videoHeight;

        // Get the width of the screen
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        float screenProportion = (float) screenWidth / (float) screenHeight;

        ViewGroup.LayoutParams params = b.sfClassCategory.getLayoutParams();

        if (videoProportion > screenProportion) {
            params.width = screenWidth;
            params.height = (int) ((float) screenWidth / videoProportion);

        } else {
            params.width = (int) (videoProportion * (float) screenHeight);
            params.height = screenHeight;
        }

        b.sfClassCategory.setLayoutParams(params);
    }