简体   繁体   English

Android 将 rintone 设置为库中存在的一个

[英]Android set rintone to one existing in library

I want my app to set a ringtone.我希望我的应用设置铃声。 The user has selected one from the existing library entries before using the system picker and gets this extra back: RingtoneManager.EXTRA_RINGTONE_PICKED_URI用户在使用系统选择器之前从现有的库条目中选择了一个,并获得了额外的返回:RingtoneManager.EXTRA_RINGTONE_PICKED_URI

In this example I have picked "Andromeda" from the default ringtones and get this path: content://media/internal/audio/media/103在此示例中,我从默认铃声中选择了“Andromeda”并获得以下路径:content://media/internal/audio/media/103

When I try to set it at a given time I run this code:当我尝试在给定时间设置它时,我运行以下代码:

Uri ur = Uri.parse(ringtoneFile.getAbsolutePath()); RingtoneManager.setActualDefaultRingtoneUri(context, ringtoneType, uri);

I have also tried this version:我也试过这个版本:

Uri ur = Uri.parse(ringtoneFile.getAbsolutePath()); android.provider.Settings.System.putString(context.getContentResolver(), android.provider.Settings.System.RINGTONE, uri.toString());

Neither works.两者都不起作用。 The system's sound settings will look like this:系统的声音设置将如下所示:

[1]:https://i.stack.imgur.com/ZWj1H.png

Only 103 is shown, not "Andromeda" as I would expect.只显示了 103 个,而不是我期望的“仙女座”。 When I have the emulator called it just makes a ding sound, so it probably can't play the desired file and uses some fallback one.当我调用模拟器时,它只会发出叮当声,因此它可能无法播放所需的文件并使用一些备用文件。

There are plenty of examples here where people pick a custom file from the filesystem and add that to the library anew using "ContentValues".这里有很多示例,人们从文件系统中选择自定义文件并使用“ContentValues”将其重新添加到库中。 But I do not want to add anything myself, I just want to set one from the default ringtones.但我不想自己添加任何东西,我只想从默认铃声中设置一个。

As an alternative I have tried to code as well.作为替代方案,我也尝试过编码。 It does add an additional entry to the library.它确实向库中添加了一个附加条目。 Unfortunately old ones are not deleted, but pile up.不幸的是,旧的没有被删除,而是堆积起来。 Also I get the same ding sound when calling the emulator, not the one I selected.调用模拟器时,我也得到相同的叮当声,而不是我选择的那个。

private boolean applyRingTone(File ringtoneFile, int ringtoneType, Context context)
{
    Miscellaneous.logEvent("i", "Profile", "Request to set ringtone to " + ringtoneFile.getAbsolutePath(), 3);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, context.getResources().getString(R.string.app_name) + " ringtone");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
    values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
    values.put(MediaStore.Audio.Media.IS_RINGTONE, ringtoneType == RingtoneManager.TYPE_RINGTONE);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, ringtoneType == RingtoneManager.TYPE_NOTIFICATION);
    values.put(MediaStore.Audio.Media.IS_ALARM, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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

    try
    {
        RingtoneManager.setActualDefaultRingtoneUri(context, ringtoneType, uri);
        Miscellaneous.logEvent("i", "Profile", "Ringtone set to: " + uri.toString(), 1);
        return true;
    }
    catch (Throwable t)
    {
        String message = "Error setting ringtone: " + Log.getStackTraceString(t);
        Miscellaneous.logEvent("e", "Profile", message, 1);
    }
    
    return false;
}

I haven't quite solved my problem, yet, but at least spotted the culprit.我还没有完全解决我的问题,但至少发现了罪魁祸首。

The way to set the file as ringtone does work.将文件设置为铃声的方法确实有效。 However the path of the source file is incorrect.但是源文件的路径不正确。 That is pretty weird because it is determined by using a filepicker:这很奇怪,因为它是通过使用文件选择器确定的:

Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
fileIntent.setType("audio/*");
startActivityForResult(Intent.createChooser(fileIntent, "Select a ringtone"), intentCodeRingtonePickerCallsFile);

This returns as path:这将作为路径返回:

content://com.android.externalstorage.documents/document/primary%3AMusic%2Fmusicfile.mp3

After converting it to a File object it'll become将其转换为文件 object 后,它将变为

/document/primary%3AMusic%2F04.%20Elevator%20Girl.mp3

When just hard-coding the path into the sourcecode (in a format a regular Linux user would assume it needs to have) it is actually simply:当只是将路径硬编码到源代码中时(以常规 Linux 用户认为它需要的格式),它实际上很简单:

/sdcard/Music/musicfile.mp3

And that works flawlessly.这完美无瑕。 I'll have to figure out how to correctly determine the path, but the method to set a file as ringtone is functional.我必须弄清楚如何正确确定路径,但是将文件设置为铃声的方法是可行的。

UPDATE: I have an answer to my path problem: Get Real Path For Uri Android更新:我对我的路径问题有一个答案: Get Real Path For Uri Android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM