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

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

问题描述

我试图找到一种方法,从我的Andr​​oid活动通过设置code一个新的默认铃声。

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

我已经下载铃声到字节组

推荐答案

最后,我成功地设置默认铃声的一个,我下载。 下载code不低于在内,只需要怎样将其设置为默认铃声。

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
);  

反正,我不完全理解这是什么code在做什么。

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

的铃声管理器需要的URI是要被设置为新的铃声文件。但是,此URI不能直接像/sdcard/media/ringtones/mysong.mp3的SD卡。那不行!

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 /外部/音频/媒体/ 46

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

46是在MediaStore数据库中列的ID,所以这就是为什么你需要将SD卡的文件先添加到数据库中。

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保持其IDS?这个数字可以得到非常高的,因为你这样做很多次。

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

我是否需要删除此行我的自我?问题是,有些时候,我甚而不CONTROLL文件的删除,因为它可以直接从一个filebrowser的SD卡被删除。

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天全站免登陆