Simple music playing and simple music playing
(To summarize the playing audio of the first line of code)
This method can only play a specified audio file.
Check the running effect first.
The code is relatively simple, but before running the program, you need to put a single audio file named "“music.pdf" in the root directory.
Public class MainActivity extends ActionBarActivity implements OnClickListener {private Button play; private Button pause; private Button stop; private MediaPlayer mediaPlayer = new MediaPlayer (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); play = (Button) findViewById (R. id. play); pause = (Button) findViewById (R. Id. pause); stop = (Button) findViewById (R. id. stop); play. setOnClickListener (this); pause. setOnClickListener (this); stop. setOnClickListener (this); // initialize MediaPlay initMediaPlayer ();} private void initMediaPlayer () {try {// the root directory of the mobile phone SDK card, which is stored in the audio music.pdf storage path File = new file (Environment. getExternalStorageDirectory (), "musicloud"); // assign the file storage path to meidaPlayer mediaPlayer. setDataSource (file. getPath (); // prepare m EdiaPlayer. prepare ();} catch (Exception e) {e. printStackTrace () ;}@ Override public void onClick (View v) {switch (v. getId () {// start playing case R. id. play: if (! MediaPlayer. isPlaying () {mediaPlayer. start ();} break; // pause. When the play button is clicked, the stream is played again, starting from the last paused position. id. pause: if (mediaPlayer. isPlaying () {mediaPlayer. pause () ;}break; // stop playing. When you click play, the music is played again. id. stop: if (mediaPlayer. isPlaying () {mediaPlayer. stop () ;}break; default: break ;}@ Override protected void onDestroy () {super. onDestroy (); if (mediaPlayer! = Null) {// release MediaPlay-related resources. stop (); mediaPlayer. release ();}}}