将音频文件设置为铃声

发布于 2024-10-10 10:20:15 字数 1155 浏览 4 评论 0原文

我已将文件保存在 sdcard/media/audio/ringtone 文件夹中。该文件将出现在 settings/sound/phone Ringtone 的铃声选择列表中。

但我想将该文件设置为我的代码中的铃声。 这是我的代码。

  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。我认为这就是为什么它没有设置为铃声的原因。

有谁知道问题出在哪里吗?如何正确获取 newUri

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒路情人 2024-10-17 10:20:15

音频仅设置为铃声一次,但此问题的解决方案是 - 如果您尝试再次运行相同的代码,您将在 MediaStore 的表中插入重复的条目,但 SQLite 数据库不允许您这样做。您必须重命名文件并添加它的另一个实例,或者进入,删除该条目,然后重试。所以我每次都删除该条目,然后再次插入。

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

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);
春庭雪 2024-10-17 10:20:15

您可以重用它,而不是删除之前插入的 uri:

    // check if file already exists in MediaStore
    String[] projection = {MediaStore.Audio.Media._ID};
    String selectionClause = MediaStore.Audio.Media.DATA + " = ? ";
    String[] selectionArgs = {ringtoneFile.getAbsolutePath()};
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selectionClause, selectionArgs, null);
    Uri insertedUri;
    if (cursor == null || cursor.getCount() < 1) {
        // not exist, insert into MediaStore
        ContentValues cv = new ContentValues();
        cv.put(MediaStore.Audio.Media.DATA, ringtoneFile.getAbsolutePath());
        cv.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName());
        insertedUri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cv);
    } else {
        // already exist
        cursor.moveToNext();
        long id = cursor.getLong(0);
        insertedUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
    }
    RingtoneManager.setActualDefaultRingtoneUri(context, type, insertedUri);

Instead of deleting the previously inserted uri, you can reuse it:

    // check if file already exists in MediaStore
    String[] projection = {MediaStore.Audio.Media._ID};
    String selectionClause = MediaStore.Audio.Media.DATA + " = ? ";
    String[] selectionArgs = {ringtoneFile.getAbsolutePath()};
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selectionClause, selectionArgs, null);
    Uri insertedUri;
    if (cursor == null || cursor.getCount() < 1) {
        // not exist, insert into MediaStore
        ContentValues cv = new ContentValues();
        cv.put(MediaStore.Audio.Media.DATA, ringtoneFile.getAbsolutePath());
        cv.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName());
        insertedUri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cv);
    } else {
        // already exist
        cursor.moveToNext();
        long id = cursor.getLong(0);
        insertedUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
    }
    RingtoneManager.setActualDefaultRingtoneUri(context, type, insertedUri);
ゝ杯具 2024-10-17 10:20:15
RingtoneManager.setActualDefaultRingtoneUri(
Context,
RingtoneManager.TYPE_RINGTONE,
Uri
.parse("Media file uri"));

我认为这会解决你的问题。

RingtoneManager.setActualDefaultRingtoneUri(
Context,
RingtoneManager.TYPE_RINGTONE,
Uri
.parse("Media file uri"));

I think this will solve ur problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文