我建立一个ping功能,通过蓝牙找到丢失的手机.我需要手机发出声音,即使它被设置为静音/静音,就像闹钟通常如何工作一样.我以为我可以将我的通知的streamtype放到AudioManager.STREAM_ALARM但它不起作用.它仅在手机声音打开时发出声音.这就是我设置它的方式:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

builder.setSmallIcon(R.drawable.ic_spenwallet)

.setContentTitle("Ping")

.setContentText("Device is trying to find your phone.")

.setAutoCancel(false)

.setSound(sound,STREAM_ALARM)

.setVibrate(vibratePattern)

.addAction(cancelAction);

如果我尝试:

Notification notification = builder.build();

notification.audioStreamType = AudioManager.STREAM_ALARM;

我从Android Studio收到一条警告,即不推荐使用audioStreamType.是这样的吗?即使打开静音模式,还有其他方法可以发出通知声音吗? (最好是振动)

我为此目的创建了一个专用的媒体播放器,但我认为不应该这样做.继承人我是怎么做到的:

MediaPlayer mediaPlayer = new MediaPlayer();

final String packageName = getApplicationContext().getPackageName();

Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);

try {

mediaPlayer.setDataSource(this,sound);

} catch (IOException e) {

e.printStackTrace();

}

final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {

mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

mediaPlayer.setLooping(false);

try {

mediaPlayer.prepare();

} catch (IOException e) {

e.printStackTrace();

}

mediaPlayer.start();

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐