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