简体   繁体   English

如何在Android中获取铃声名称?

[英]How to get Ringtone name in Android?

I'm allowing my user to pick a ringtone for notifications in my app. 我允许我的用户在我的应用中为通知选择铃声。 I want to store the URI of the sound along with the human readable title of the sound. 我想存储声音的URI以及人类可读的声音标题。

So far the URI code works great: 到目前为止,URI代码非常有用:

Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

But when I try to get the title, and set it as a button text, I don't get anything. 但是当我试图获得标题并将其设置为按钮文本时,我什么也得不到。 Seems to have no title? 好像没有头衔?

String title = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_TITLE);
button.setText(title);

But my button text is empty. 但我的按钮文字是空的。 If I do: 如果我做:

button.setText(uri.toString());

then I see the uri perfectly. 然后我完全看到了uri。 Should I just try to get the title from the URI? 我应该尝试从URI获取标题吗? Thanks 谢谢

This should get it: 这应该得到它:

Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
String title = ringtone.getTitle(this);

Refer to http://developer.android.com/reference/android/media/Ringtone.html for the documentation, but the short story: Ringtone.getTitle(Context ctx); 有关文档,请参阅http://developer.android.com/reference/android/media/Ringtone.html ,但简短的故事:Ringtone.getTitle(Context ctx);

I personally had a serious performance problem when I tried the accepted answer, it took about 2 seconds to just load a list of 30 ringtones. 当我尝试接受的答案时,我个人遇到了严重的性能问题,只需加载一个30个铃声列表就需要2秒钟。 I changed it a bit and it works about 10x faster: 我改了一下它的速度提高了大约10倍:

uri = ringtoneMgr.getRingtoneUri(cursor.getPosition());
ContentResolver cr = getContext().getContentResolver();
String[] projection = {MediaStore.MediaColumns.TITLE};
String title;
Cursor cur = cr.query(uri, projection, null, null, null);
    if (cur != null) {
        if (cur.moveToFirst()) {
            title = cur.getString(0);
            cur.close();

I had problems with 'MediaPlayer finalized without being released'. 我遇到了'MediaPlayer未经发布而最终确定'的问题。 I use this: 我用这个:

Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
returnCursor.moveToFirst();
String title = returnCursor.getString(returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
returnCursor.close();

Refer to https://developer.android.com/training/secure-file-sharing/retrieve-info.html for the documentation. 有关文档,请参阅https://developer.android.com/training/secure-file-sharing/retrieve-info.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM