Android uses the built-in audio player

Source: Internet
Author: User

To play an audio file, you can use the built-in Music function to play the audio file) "function of the application-that is, use the built-in or installed music player to play the specified audio file.


This example is relatively simple. The source code is provided below:

Layout file activity_main:

     
      
  
 

Code File MainActivity:

Package com. mutimediademo3audio; import java. io. file; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private Button button; @ Overrideprotected void o NCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); button. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button. content. intent. the data of ACTION_VIEW intent is set to the URI of an audio file, * and its MIME type is specified, so that Android can select an appropriate application for playing. */Intent intent = new Intent (android. content. intent. ACTION_VIEW); File sdcard = Environment. getExternalStorageDirectory (); File audioFile = new File (sdcard. getPath () + "/good.mp3"); // put an mp3 file named good in the SD card. Intent. setDataAndType (Uri. fromFile (audioFile), "audio/mp3"); startActivity (intent); break; default: break ;}}}

Source code:

Click to download source code


Related Article

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.