Today I learned how to play audio with the MediaPlayer method in Android, the code below.
Start with a new Android project, place pre-recorded audio music.wma in the Res/raw directory, add a button to the layout file, and the audio will ring when the button is pressed.
1 Public classTestaudioextendsActionbaractivityImplementsOnclicklistener {2 3 PrivateMediaPlayer MP;4 @Override5 Public voidonCreate (Bundle savedinstancestate) {6 Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8Setvolumecontrolstream (Audiomanager.stream_music);//Indicates that the volume key is pressed to adjust the volume of music or other media, not the volume of the ringtone9 TenButton Btnaudio =(Button) Findviewbyid (r.id.audio); OneBtnaudio.setonclicklistener ( This); A } - - Public voidOnClick (View v) { the Switch(V.getid ()) { - CaseR.id.audio: -MP = Mediaplayer.create ( This, r.raw.music); -Mp.start ();//Press the play button and the music rings + Break; - } +}
Android Learning to play audio