本文介绍了如何从铃声preference选择播放铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打它是由一个铃声preference选择铃声。我怎样才能打吗?
下面是我的xml文件code
<铃声preference
机器人:标题=选择报警
机器人:关键=铃声
机器人:汇总=这是总结
>< /铃声preference>
下面是我在做什么在Java
共享preferences getAlarms = preferenceManager.getDefaultShared preferences(getBaseContext());
串报警= getAlarms.getString(铃声,默认铃声);
当我使用吐司这样
Toast.makeText(getApplicationContext(),报警器,Toast.LENGTH_LONG).show();
然后,它显示了这种类型的路径
内容:// /内部/音频/媒体/ 50
但我不知道怎么玩这个。
请帮助。
解决方案
私人无效报警(){
共享preferences getAlarms = preferenceManager。
getDefaultShared preferences(getBaseContext());
串报警= getAlarms.getString(铃声,默认铃声);
开放的我们的uri = Uri.parse(报警);
playSound(这一点,URI);
//调用mMediaPlayer.stop();当你想要的声音停止
}
私人MediaPlayer的mMediaPlayer;
私人无效playSound(上下文的背景下,开放的警报){
mMediaPlayer =新的MediaPlayer();
尝试 {
mMediaPlayer.setDataSource(背景下,警报);
最后AudioManager audioManager =(AudioManager)上下文
.getSystemService(Context.AUDIO_SERVICE);
如果(audioManager.getStreamVolume(AudioManager.STREAM_ALARM)!= 0){
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer prepare()。
mMediaPlayer.start();
}
}赶上(IOException异常E){
的System.out.println(OOPS);
}
}
这这里应该是你想要的:)我希望它的工作原理
I am trying to play a ringtone which is selected from a RingtonePreference. How can I play it?
Here is my xml file code
<RingtonePreference
android:title="Choose Alarm"
android:key="ringtone"
android:summary="this is summary"
></RingtonePreference>
Here is what I am doing in java
SharedPreferences getAlarms = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String alarms = getAlarms.getString("ringtone", "default ringtone");
When I use toast like this
Toast.makeText(getApplicationContext(), alarms, Toast.LENGTH_LONG).show();
Then it shows this kind of path
content://media/internal/audio/media/50
But I do not know how to play this one.
Help Please.
解决方案
private void alarm(){
SharedPreferences getAlarms = PreferenceManager.
getDefaultSharedPreferences(getBaseContext());
String alarms = getAlarms.getString("ringtone", "default ringtone");
Uri uri = Uri.parse(alarms);
playSound(this, uri);
//call mMediaPlayer.stop(); when you want the sound to stop
}
private MediaPlayer mMediaPlayer;
private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
System.out.println("OOPS");
}
}
This here should be what you want :) I hope it works
这篇关于如何从铃声preference选择播放铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!