Java 如何在Android中获取铃声名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19187834/
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
How to get Ringtone name in Android?
提问by stacksonstacks
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. Should I just try to get the title from the URI? Thanks
然后我完美地看到了uri。我应该尝试从 URI 中获取标题吗?谢谢
采纳答案by Erik Nedwidek
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.htmlfor the documentation, but the short story: Ringtone.getTitle(Context ctx);
有关文档,请参阅http://developer.android.com/reference/android/media/Ringtone.html,但简短的故事: Ringtone.getTitle(Context ctx);
回答by toni
I had problems with 'MediaPlayer finalized without being released'. I use this:
我遇到了“MediaPlayer 最终确定但未发布”的问题。我用这个:
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.htmlfor the documentation.
有关文档,请参阅https://developer.android.com/training/secure-file-sharing/retrieve-info.html。
回答by Matvey Rybakov
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. I changed it a bit and it works about 10x faster:
当我尝试接受的答案时,我个人遇到了严重的性能问题,加载一个包含 30 个铃声的列表需要大约 2 秒钟。我对其进行了一些更改,它的运行速度提高了大约 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();