如何通过我的活动在 Android 中设置铃声? [英] How to set ringtone in Android from my activity?

查看:26
本文介绍了如何通过我的活动在 Android 中设置铃声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我的 Android Activity 中的代码找到一种方法来设置新的默认铃声.

I'm trying to find a way to set a new default ringtone by code from my Android activity.

我已经将铃声下载到bytearray.

推荐答案

最后,我设法将默认铃声设置为我下载的铃声.下面不包含下载代码,只包含设置为默认铃声所需的代码.

Finally, I managed to set the default ringtone to one that i downloaded. The download code is not included below, only what was needed to set it as default ringtone.

File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = this.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);  

无论如何,我不完全理解这段代码在做什么.

Anyway, I do not totally understand what this code is doing.

铃声管理器需要要设置为新铃声的文件的 uri.但是这个uri不能像/sdcard/media/ringtones/mysong.mp3"那样直接到sdcard.那不行!

The Ringtone manager needs a uri to the file that is to be set as new ringtone. But this uri can not be directly to the sdcard like "/sdcard/media/ringtones/mysong.mp3". That does not work!

您需要的是文件的外部文件 uri,它可能类似于"/external/audio/media/46"

What you need is the external file uri of the file which could be something like "/external/audio/media/46"

46是MediaStore数据库中列的id,所以需要先将sdcard文件添加到数据库中.

The 46 is the id of the column in the MediaStore database, so thats why you need to add the sdcard file into the database first.

无论如何,mediastore 是如何维护其 id 的?这个数字可能会变得非常高,因为您多次执行此操作.

Anyway, how does mediastore maintain its ids? This number can get really high, as you do this operation many times.

我需要自己删除这一行吗?问题是有时我什至无法控制文件的删除,因为可以使用文件浏览器直接从 sdcard 中删除它.

Do i need to delete this row my self? Problem is that some times i dont even controll the deleting of the file since it can be deleted directly from the sdcard with a filebrowser.

这篇关于如何通过我的活动在 Android 中设置铃声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆