设置音频文件作为铃声 [英] setting audio file as Ringtone

查看:149
本文介绍了设置音频文件作为铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存 SD卡/媒体/音频/铃声取值文件夹中的文件。该文件将出现在铃声选择列表从设置/音响/电话铃声。

I have save file in sdcard/media/audio/ringtones folder. That file will appear in list of ringtone selection from settings/sound/phone Ringtone.

不过,我想设置该文件从我的code铃声。 这是我的code。

But I want to set that file as a ringtone from my code. Here is my code.

  File k = new File(path, filename);

  ContentValues values = new ContentValues();
  values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
  values.put(MediaStore.MediaColumns.TITLE, "TwiAppclip");
  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
  values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
  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);

  Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
       .getAbsolutePath());
  Uri newUri = getApplicationContext().getContentResolver().insert(uri, values);

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

在这里 URI 我收到,但我得到了 newUri = NULL 。我想这就是为什么它是不是设置为铃声。

here uri I am getting But I got newUri = null. I think that's why its is not setting as ringtone.

任何人都知道问题出在哪里?我怎么 newUri 正确?

Anyone know where is the problem? how do I get newUri proper?

推荐答案

音频设置为铃声只有一次,但解决这个问题是 - 如果你想再次运行相同的code,你会插入重复进入MediaStore的表,但SQLite数据库不会允许你。你要么重命名文件,并添加它的另一个实例,或者进去,删除该条目,然后重试。所以我每次都删除该条目,然后重新插入。

Audio is set as ringtone only once, but solution to this problem is - If you are trying to run the same code again, you'll be inserting a duplicate entry into MediaStore's table, but the SQLite database won't allow you. You have to either rename your file and add another instance of it, or go in, remove the entry, and then try again. So I removed that entry every time and then insert it again.

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(activity.this,
        RingtoneManager.TYPE_RINGTONE, newUri);

这篇关于设置音频文件作为铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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