gpt4 book ai didi

android - 从 res/raw 文件夹设置铃声

转载 作者:行者123 更新时间:2023-11-29 14:49:41 29 4
gpt4 key购买 nike

我正在尝试以编程方式将 /res/raw/ 文件夹中的音频文件设置为默认铃声。我有如下文件:myapp/res/raw/file.mp3

我有下面的代码,但不幸的是它完全无法设置铃声。我不确定是什么导致了这个问题,但想知道是否有人有任何见解可以分享。

Uri path = Uri.parse("android.resource://com.mikan.myapp/res/raw/file.mp3");
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
path);
Log.i("TESTT", "Ringtone Set to Resource: "+ path.toString());
RingtoneManager.getRingtone(getApplicationContext(), path)
.play();

我收到以下异常(在 RingtoneManager 中捕获,让它看起来好像什么都没发生:

03-25 20:20:23.024: D/MediaPlayer(24659): setDataSource IOException happend : 
03-25 20:20:23.024: D/MediaPlayer(24659): java.io.FileNotFoundException: More than two path segments: android.resource://com.mikan.myapp/res/raw/file.mp3
03-25 20:20:23.024: D/MediaPlayer(24659): at android.content.ContentResolver.getResourceId(ContentResolver.java:861)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:653)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:979)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:933)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.media.Ringtone.setUri(Ringtone.java:228)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.media.RingtoneManager.getRingtone(RingtoneManager.java:647)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.media.RingtoneManager.getRingtone(RingtoneManager.java:628)
03-25 20:20:23.024: D/MediaPlayer(24659): at com.mikan.myapp.CustomApp.saveas(CustomApp.java:139)
03-25 20:20:23.024: D/MediaPlayer(24659): at com.mikan.myapp.ListAdapter$1.onClick(ListAdapter.java:83)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.view.View.performClick(View.java:4475)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.view.View$PerformClick.run(View.java:18786)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.os.Handler.handleCallback(Handler.java:730)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.os.Looper.loop(Looper.java:137)
03-25 20:20:23.024: D/MediaPlayer(24659): at android.app.ActivityThread.main(ActivityThread.java:5419)
03-25 20:20:23.024: D/MediaPlayer(24659): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:20:23.024: D/MediaPlayer(24659): at java.lang.reflect.Method.invoke(Method.java:525)
03-25 20:20:23.024: D/MediaPlayer(24659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-25 20:20:23.024: D/MediaPlayer(24659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-25 20:20:23.024: D/MediaPlayer(24659): at dalvik.system.NativeStart.main(Native Method)
03-25 20:20:23.024: D/MediaPlayer(24659): Couldn't open file on client side, trying server side

最佳答案

您不能从资源中设置铃声。

RingtoneManager.setActualDefaultRingtoneUri 需要设备中的铃声文件,Uri 应该来自内容解析器。

 File ring = new File("pathOfYourFile");
Uri path = MediaStore.Audio.Media.getContentUriForPath(ring.getAbsolutePath());
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE,path);

还要确保添加权限

 <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

关于android - 从 res/raw 文件夹设置铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22645511/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com