Using Mediaplayer within a Fragment

后端 未结 1 1956
灰色年华
灰色年华 2021-01-05 23:45

I\'m still trying to understand fragments and how they work. I have a fragment that inflates a layout. I am trying to play a sound when a button is pushed but I am coming ac

1条回答
  •  孤城傲影
    2021-01-06 00:02

    Use this code in onCreateView method of your fragment class and it will be working fine.

    mp = MediaPlayer.create(getActivity, R.raw.songname);
    

    Now you can call it's start method on your button click listener. Make sure to make the MediaPlayer mp; global for fragment class so you can use it afterwards.

    0 讨论(0)
提交回复
热议问题