How Can I Create A Notification That Plays An Audio File When Being Tapped?
I would like to display a notification and when the user taps on it a sound file should be played. In Android Studio I have copied the file test.mp3 into the folder app\res\raw. Th
Solution 1:
I have managed it by creating the intent like this:
IntentplaySoundIntent=newIntent(this, PlaySoundActivity.class);
PendingIntentpendingIntent= PendingIntent.getActivity(this, 0, playSoundIntent, 0);
and adding the activity:
publicclassPlaySoundActivityextendsAppCompatActivity
{
privatestaticMediaPlayer mediaPlayer;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mediaPlayer = MediaPlayer.create(this, R.raw.test);
mediaPlayer.start();
}
@OverridepublicvoidonStop()
{
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}
}
Although this works, I am still interested in why the former approach didn't work. I have seen many code snippets like that. And still I wonder how I could debug the former approach to understand the error. Can anybody help me?
Post a Comment for "How Can I Create A Notification That Plays An Audio File When Being Tapped?"