Simple music playing and simple music playing

Source: Internet
Author: User

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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.