我正在做一个应用程序需要通知用户正在进行长途呼叫的呼叫... 我把所有的东西都运行起来,然后在正确的时间发出通知(我可以在状态栏看到它)但没有声音。如果我在没有通话时拨打通知,则会播放声音。
我的通知如下:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello From CallTimerService";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "ss";
Intent notificationIntent = new Intent(this, CallTimer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(2, notification);
我也尝试使用AudioManager.STREAM_更改notification.audioStreamType但没有运气。
现在做任何身体怎么做?或者只是一个好主意,接下来要尝试什么......
答案 0 :(得分:2)
我的应用程序中有一个选项可以在用户拨打电话时播放声音。您需要做的是使用媒体播放器播放声音。这是我的代码:
if(callStateIdle){
notification.sound = Uri.parse(notificationSound);
}else{
new playNotificationMediaFileAsyncTask().execute(notificationSound);
}
这是异步任务:
private static class playNotificationMediaFileAsyncTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
MediaPlayer mediaPlayer = null;
try{
mediaPlayer = new MediaPlayer();
mediaPlayer.setLooping(false);
mediaPlayer.setDataSource(_context, Uri.parse(params[0]));
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
});
return null;
}catch(Exception ex){
Log.e("ERROR: " + ex.toString());
mediaPlayer.release();
mediaPlayer = null;
return null;
}
}
protected void onPostExecute(Void result) {
//Do Nothing
}
}
到目前为止,这对我来说效果很好。
答案 1 :(得分:1)
尝试使用RingtoneManager。以下内容适用于我:RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();