How to play a specifc audio file from Android in build rintone list?

不问归期 提交于 2019-12-11 06:45:48

问题


I need to know is it possible to play a particular audio file from android in-built ringtone files. For example assume Tone_23 is in android ringtone list, now i need to play this particular tone, when i click a button. I searched in Google i got a guidance how to call/show RingtonePicker Activity (The entire Ringtone list will be displayed). If this possible means, kindly share your thoughts. Thanks in advance.


回答1:


You can try some thing like this :

/**
 * Play ring tone.
 *
 * @param ringToneTitle the ring tone title
 */
void playRingTone(String ringToneTitle) {
    RingtoneManager ringtoneManager = new RingtoneManager(
            getApplicationContext());
    ringtoneManager.setType(RingtoneManager.TYPE_RINGTONE);

    int length = ringtoneManager.getCursor().getCount();

    for (int i = 0; i < length; i++) {
        Ringtone mRingtone = ringtoneManager.getRingtone(i);
        if (mRingtone != null) {
            Log.d("ringtoneTitle ", mRingtone.getTitle(getApplicationContext()));
            if(ringToneTitle.equalsIgnoreCase(mRingtone
                        .getTitle(getApplicationContext())) {
                mRingtone.play();
            }
        }
    }
}

Hope this helps.

Added : Also have a look at this class RingtoneManager

RingtoneManager provides access to ringtones, notification, and other types of sounds. It manages querying the different media providers and combines the results into a single cursor. It also provides a Ringtone for each ringtone. We generically call these sounds ringtones, however the TYPE_RINGTONE refers to the type of sounds that are suitable for the phone ringer.




回答2:


I think you can only play the ringtone that is set in the system automatically with for example:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();

If you would always want to play a specific tone/file you would have to add it to your assets and play it from there.



来源:https://stackoverflow.com/questions/12588444/how-to-play-a-specifc-audio-file-from-android-in-build-rintone-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!