java 如何播放从 RingtonePreference 中选择的铃声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10460499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:08:07  来源:igfitidea点击:

How to play ringtone selected from RingtonePreference

javaandroidandroid-emulator

提问by Om3ga

I am trying to play a ringtone which is selected from a RingtonePreference. How can I play it?

我正在尝试播放从 RingtonePreference 中选择的铃声。我怎么玩?

Here is my xml file code

这是我的 xml 文件代码

<RingtonePreference
    android:title="Choose Alarm"
    android:key="ringtone"
    android:summary="this is summary"
    ></RingtonePreference>

Here is what I am doing in java

这是我在 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.

请帮忙。

回答by FabianCook

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

这应该是你想要的 :) 我希望它有效

回答by Lalit Poptani

Hereis a sample project from Commonsware for the same you can download and check its working.

是来自 Commonsware 的示例项目,您可以下载并检查其工作情况。

You can get the Uri from the String that you are getting by using,

您可以从使用的字符串中获取 Uri,

SharedPreferences getAlarms = PreferenceManager.
                                  getDefaultSharedPreferences(getBaseContext());
String alarms = getAlarms.getString("ringtone", "default ringtone");
Uri uri = Uri.parse("alarms");

Then you can play the uri using MediaPlayer.

然后你可以使用MediaPlayer.

回答by Yasitha Chinthaka

You can take preferred ringtone from preferences and You can easily play the ringtone using RingtoneManager class

您可以从首选项中获取首选铃声,并且可以使用 RingtoneManager 类轻松播放铃声

SharedPreferences getAlarms = PreferenceManager.
                              getDefaultSharedPreferences(getBaseContext());
String alarms = getAlarms.getString("ringtone", "default ringtone");
Uri uri = Uri.parse(alarms);

Ringtone r = RingtoneManager.getRingtone(context, uri);
r.play();