Today, any smart phone can play audio. Of course, android-based devices are no exception. They allow you to create music players, audio books, podcasts, or any other applications that play around audio. In this article, we will discuss Android's functions in terms of format and decoder support, as well as several different playing programs.
Audio Playback
Android supports multiple audio file formats and codecs for playback (recording is also supported)
AAC
MP3
AMR
Ogg
PCM
For details about the format, refer to the relevant documents.
Intention to use the built-in player
Use android to push and play the specified file through intent. content. intent. the data of ACTION_VIEW intent is set to the URI of an audio file and Its MIME type is specified. In this way, Android can select the application to play the video.
Intent intent = new Intent (android. content. Intent. ACTION_VIEW );
Intent. setDataAndType (audioFileUri, "audio/mp3 ");
StartActivity (intent );
The following is a complete example,
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;
/**
* This example shows how to use Music of Android to play a program.
* Like Camera, you can use Intent to start it.
* We need to specify an ACTION_VIEW Action.
* At the same time, a Uri is used to specify the path of the file to be played.
* Specify the MIME type and the file type to be played.
* Each file type corresponds to a MIME, which is generally similar to audio/mp3 format.
* The first part is a large type, followed by a more specific type
*
* Similarly, for Audio-type multimedia, the system is stored in MediaStore. Audio.
* Media, Album, Genre, and other information bodies
*
* This document lists all Album information for users to choose from.
* When you select an Album instance, the system will open all Audio files under this ALbum instance.
* @ Author Administrator
*
*/
Before playing the audio, the activity monitors whether to press a button. Because the activity implements OnClickListener, it can respond to this event.
Public class IntentAudioPlayer extends Activity implements OnClickListener {
Button playButton;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
PlayButton = (Button) this. findViewById (R. id. Button01 );
PlayButton. setOnClickListener (this );
}
Public void onClick (View v ){
Intent intent = new Intent (android. content. Intent. ACTION_VIEW );
File sdcard = Environment. getExternalStorageDirectory ();
File audioFile = new File (sdcard. getPath ()
+ "/Music/goodmorningandroid.pdf ");
Intent. setDataAndType (Uri. fromFile (audioFile), "audio/mp3 ");
StartActivity (intent );
}
}
The layout file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button android: text = "Play Audio" android: id = "@ + id/Button01" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
</LinearLayout>
Author: wangjinyu501