从资产文件夹设置通知声音

发布于 2024-12-25 18:41:00 字数 1457 浏览 3 评论 0原文

我将声音文件附加到资产文件夹中以便在通知中播放声音,但我无法从资产文件夹中播放声音。

我有设置页面,用户可以从中设置通知的声音,用户也可以播放声音。这部分已经完成。我可以使用媒体播放器播放资产文件夹中的声音。我可以在通知创建时使用相同的逻辑,但是当用户单击通知时可能会发生问题,因为媒体播放器仍在播放声音。

notification.sound = Uri.parse("file:///android_asset/Sound/Long/AlarmClock.mp3");

也尝试过这样的方法

notification.sound = Uri.parse("android_asset/Sound/Long/AlarmClock.mp3");

,但我的问题仍然没有解决。

还有其他方法可以设置资产目录中的通知声音吗?

Logcat

01-10 09:57:01.509: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
01-10 09:57:01.509: ERROR/MediaPlayer(52): error (1, -4)
01-10 09:57:01.519: WARN/NotificationService(52): error loading sound for android_asset/Sound/Long/audio.mp3
01-10 09:57:01.519: WARN/NotificationService(52): java.io.IOException: Prepare failed.: status=0x1
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.MediaPlayer.prepare(Native Method)
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.AsyncPlayer.startSound(AsyncPlayer.java:64)
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.AsyncPlayer.access$200(AsyncPlayer.java:33)
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.AsyncPlayer$Thread.run(AsyncPlayer.java:99)
01-10 09:57:01.519: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete

或任何其他想法?请给我提示。

I am attaching the sound files in the assets folder for play sound in the notification but I am not able to play sound from the assets folder.

I have setting page from which user can set the sound for the notification and also user can play sound. This part is completed. I am able to play sound from the assets folder using Media Player. I can use same logic at the Notification creation time but problem can be happened when user click on the Notification because media player still playing the sound.

I tried

notification.sound = Uri.parse("file:///android_asset/Sound/Long/AlarmClock.mp3");

also tried like this

notification.sound = Uri.parse("android_asset/Sound/Long/AlarmClock.mp3");

but my issue is still not solved.

Is there any other way to set the sound in the notification from the assets directory ?

Logcat

01-10 09:57:01.509: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
01-10 09:57:01.509: ERROR/MediaPlayer(52): error (1, -4)
01-10 09:57:01.519: WARN/NotificationService(52): error loading sound for android_asset/Sound/Long/audio.mp3
01-10 09:57:01.519: WARN/NotificationService(52): java.io.IOException: Prepare failed.: status=0x1
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.MediaPlayer.prepare(Native Method)
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.AsyncPlayer.startSound(AsyncPlayer.java:64)
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.AsyncPlayer.access$200(AsyncPlayer.java:33)
01-10 09:57:01.519: WARN/NotificationService(52):     at android.media.AsyncPlayer$Thread.run(AsyncPlayer.java:99)
01-10 09:57:01.519: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete

or any other idea ? Please give me hint.

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

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

发布评论

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

评论(6

自控 2025-01-01 18:41:00

将您的mp3文件放入res->raw文件夹中,尝试从raw文件夹中获取声音文件。

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

put ur mp3 file in res->raw folder, try to fetch sound file from raw folder.

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

OR

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
指尖上的星空 2025-01-01 18:41:00

这对我来说非常有效。我在另一个帖子里找到的。

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");

This worked perfectly for me. I found it on another post.

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
夜声 2025-01-01 18:41:00

资产是该应用程序的私有资源,我认为您无法将它们设置为铃声,您将需要一个可由媒体进程访问的公共文件来设置铃声。您必须将文件复制到 SD 卡才能将其设置为铃声。

assets are private resources to that app, I think you wont be able to set them as ringtone, you will need a public file which is accessible by the media process to set ringtone. You will have to copy the file to sdcard to set it as ringtone.

清晨说晚安 2025-01-01 18:41:00

我无法直接访问通知的资产音频文件。
因此,我按照以下步骤操作,因此我可以在通知中播放音频,

  1. 并将该文件复制到数据目录。
  2. 通知声音仍然无法访问数据目录,因此我创建了一个自定义 Content Provider 来共享现在位于数据文件夹中的阅读更多内容中的音频文件。
  3. 现在,在创建通知时,我已将文件的 Uri 设置为通知声音。

现在使用此步骤我可以为通知设置自定义声音。

I was not able to access directly the assets audio file to the Notification.
So i follow this steps and so I am able to play audio in the Notification

  1. I copy that files to the data directory.
  2. Still data directory is not accessible to the Notification sound so I create a custom Content Provider to share the audio file in the read more which are now in data folder.
  3. Now at the time of notification creation I had set the Uri of the file to the Notification sound.

Now using this steps I am able to set custom sound to the Notification.

一页 2025-01-01 18:41:00

从资产文件夹加载内容的正确路径是 file:///android_assets/relative_path_to_your_file。请注意此处的三个斜杠。虽然我没有尝试以这种方式设置通知声音,但我已经多次成功地将其与 WebView 一起使用,并且希望它能以相同的方式工作。

The correct path to load something from your assets folder is file:///android_assets/relative_path_to_your_file. Note the three slashes here. Although I have not tried to set a notification sound this way, I have used this with a WebView with success a number of times, and would expect it to work the same way.

甜妞爱困 2025-01-01 18:41:00

ContentProvider 是必经之路。
我认为不需要像@Dharmendra 建议的那样将文件复制到数据目录。
您可以通过ContentProvider直接访问资产文件夹

public AssetFileDescriptor openAssetFile(Uri uri, String mode)
        throws FileNotFoundException {
    String filePart = Sound.getSoundFileName(uri);

    try {
        return getContext().getResources().getAssets().openFd(filePart + ".ogg");
    }catch(IOException e) {
        Log.d(TAG, "fail " + e.getMessage());

    }
    return null;
}

ContentProvider is the way to go.
I don't think there is a need to copy files to the data directory like @Dharmendra suggests.
You can access the asset folder directly though the ContentProvider

public AssetFileDescriptor openAssetFile(Uri uri, String mode)
        throws FileNotFoundException {
    String filePart = Sound.getSoundFileName(uri);

    try {
        return getContext().getResources().getAssets().openFd(filePart + ".ogg");
    }catch(IOException e) {
        Log.d(TAG, "fail " + e.getMessage());

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