Android setSound -无法获取文件的URI

k4ymrczo  于 2022-12-09  发布在  Android
关注(0)|答案(2)|浏览(159)

我看过很多关于如何执行setSound部分的文章,但到目前为止,我还无法简单地引用我想播放的文件。我使用的是Capacitor 4,因此我想播放的文件可能存储在与本机应用程序不同的位置。
下面是应用程序的文件结构,我只想加载“FinalCountdown.wav”

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/assets/public/assets/sounds/FinalCountdown.wav");  //Here is FILE_NAME is the name of file that you want to play
      File file = new File(sound.toString());
      Log.w("[PUSH NOTIFICATION]","File Check");
      if(file.exists()){
        Log.w("[PUSH NOTIFICATION]","FILE FOUND at " + sound.toString());
      }else{
        Log.w("[PUSH NOTIFICATION]","File NOT FOUND!!! at " + sound.toString());
      }

检查始终记录(软件包名称模糊化)

W/[PUSH NOTIFICATION]: File Check
W/[PUSH NOTIFICATION]: File NOT FOUND!!! at android.resource://**********/assets/public/assets/sounds/FinalCountdown.wav

为了完整起见,我在下面的代码中向通知通道添加了声音

AudioAttributes audioAttributes = new AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .build();

channel.setSound(sound, audioAttributes);

我意识到这一定很简单,所以任何帮助都很感激。

vd2z7a6w

vd2z7a6w1#

可以用途:
在将inputStream保存到内部存储器并使用之后。

AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager .open("FinalCountdown.wav");
dkqlctbz

dkqlctbz2#

将档案声音放入res/raw,然后使用

channel.setSound(Uri.parse("android.resource://"
                        + context.packageName + "/$soundRawId"), audioAttributes)

相关问题