Process audio_becoming_noisy intent
Some well-written applications can automatically stop playing when audio noise occurs (output through an external speaker. For example, when a user is using a headset to listen to music, this may happen if the headset is accidentally disconnected from the device. However, this behavior does not automatically occur. If you do not implement this function, the audio may be output through an external speaker, which may be unwanted by the user.
By processing the action_audio_becoming_noisy intent, you can ensure that the playing of the music stops when this happens. You can register a receiver by adding the following content to your application list:
<Cycler Android: Name = ". musicintentreceiver">
<Intent-filter>
<Action Android: Name = "android. Media. audio_becoming_noisy"/>
</Intent-filter>
</Cycler>
The registered musicintenstmer class is used as the broadcast receiver of the intent. Then we should implement this class:
Public class musicintentreceiver implements Android. content. broadcastreceiver {
@ Override
Public void onreceive (context CTX, intent ){
If (intent. getaction (). Equals (
Android. Media. audiomanager. action_audio_becoming_noisy )){
// Signal your service to stop playback
// (Via an intent, for instance)
}
}
}
Receives media data from the content Resolver
In the media playback application, you can use another useful function to obtain music on your device. Query contentresolver to obtain external media:
Contentresolver = getcontentresolver ();
Uri uri = Android. provider. mediastore. Audio. Media. external_content_uri;
Cursor cursor = contentresolver. Query (Uri, null );
If (cursor = NULL ){
// Query failed, handle error.
} Else if (! Cursor. movetofirst ()){
// No media on the device
} Else {
Int titlecolumn = cursor. getcolumnindex (Android. provider. mediastore. Audio. Media. Title );
Int idcolumn = cursor. getcolumnindex (Android. provider. mediastore. Audio. Media. _ id );
Do {
Long thisid = cursor. getlong (idcolumn );
String thistitle = cursor. getstring (titlecolumn );
//... Process entry...
} While (cursor. movetonext ());
}
Use this mediaplayer object as follows:
long id =/* retrieve it from somewhere */;
Uri contentUri =ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
mMediaPlayer =newMediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(getApplicationContext(), contentUri);
// ...prepare and start...