使用自定义 mp3 文件创建铃声 - 不起作用

Create ringtone with custom mp3 file - not working

我有一个可用的警报接收器等,它启动了我的 'Alarm Screen Activity'。 我想让这个 activity 从资源中播放一个 MP3 文件作为闹铃。

找到了很多问题和答案,但没有有效的解决方案:-(

我的文件在:... MyFirstApp\app\src\main\res\raw 文件夹

下面的代码在我的activity的onCreate方法上:

Uri uri_a = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Uri uri_b = Uri.parse("android.resource://com.myapps.myfirstapp/res/raw/" + R.raw.def_alarm_tone);
ringTone = RingtoneManager.getRingtone(getApplicationContext(), uri_b);
ringTone.play();

如果我在第 3 行使用 uri_a - 它会工作并播放默认闹钟铃声。

如果我使用 uri_b - 我听不到声音 - 日志中的错误显示 java.io.FileNotFoundException

那么,关于我做错了什么的任何建议 - 或者替代方案/更好的解决方案???

此外,一些关于管理应用程序的建议 alarms/reminders 会很好。

使用 MediaPlayer 作为警报的替代方案有哪些 pros/cons?

你为什么不尝试这样的事情呢?

 MediaPlayer BG;

 ....
 BG = MediaPlayer.create(getBaseContext(), R.raw.def_alarm_tone);
    BG.setLooping(false);
    BG.setVolume(100, 100);

  ...
  //whenever you want to use it 
 BG.start();

确保文件在 App res/raw 目录中。

原来我在使用 URI 时遇到了问题,因为我的应用程序 ID 与我在 Gradle 脚本(模块:应用程序)中的程序包名称不匹配...因为我最近更改了我的应用程序名称。

已修复该问题,现在可以按预期工作了。