[英]Android: how to Play Mp3 sound as a notification?
I have a countdown that, when it is done, it invokes the following onFinish
method: 我有一个倒计时,完成后,它将调用以下
onFinish
方法:
public void onFinish() {
azanCountdownH1.setText("Get ready now");
}
In order to be able to play a mp3 file at the end of the countdown, I changed the onFinish
method to: 为了能够在倒计时结束时播放mp3文件,我将
onFinish
方法更改为:
public void onFinish() {
playNotification();
azanCountdownH1.setText("Get ready now");
}
Where playNotification
looks exactly like: playNotification
看起来完全像这样:
public static void playNotification(){
MediaPlayer mp = MediaPlayer.create(MyApplicationContext.getAppContext(), R.raw.azan);
mp.start();
}
But it is not working !! 但这是行不通的!
On Android Moniter on Android-Studio
it is showing the following: 在Android箴言报对
Android-Studio
它显示如下:
Any suugestion on what or where the bug might be ?! 关于该错误可能是什么或在哪里的任何建议?
the Context
you pass to MediaPlayer.create() is null . 您传递给MediaPlayer.create()的
Context
为null 。 I would suggest to pass the Context
to your playNotification method: 我建议将
Context
传递给您的playNotification方法:
public void onFinish() {
playNotification(this);
azanCountdownH1.setText("Get ready now");
}
public static void playNotification(Context context){
MediaPlayer mp = MediaPlayer.create(context, R.raw.azan);
mp.start();
}
Try to this code hope this can help you.. 尝试编写此代码,希望对您有所帮助。
public void onFinish() {
azanCountdownH1.setText("Get ready now");
playNotification(youractivity.this);
}
public void playNotification(Context context){
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(context, R.raw.azan);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
}
You might be passing a wrong Context
so just replace Activity.this
with your Activity
name 您可能传递了错误的
Context
所以只需将Activity.this
替换为您的Activity
名称
public static void playNotification(){
MediaPlayer mediaplayer = MediaPlayer.create(Activity.this, R.raw.azan);
if(mediaplayer == null) {
Log.v(TAG, "Create() on MediaPlayer failed.");
} else {
mediaplayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaplayer) {
mediaplayer.stop();
mediaplayer.release();
}
});
mediaplayer.start();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.