【问题标题】:Android notifications play sound from assets folderAndroid 通知从资产文件夹播放声音
【发布时间】:2015-02-19 07:20:15
【问题描述】:

我正在尝试从资产文件夹播放通知声音,但是当通知触发时,会显示通知但没有播放声音,并且 logCat 中没有错误或警告。我正在使用此代码创建通知:

builder = new NotificationCompat.Builder(context)
                    // Set Icon
                    .setSmallIcon(R.drawable.ic_launcher)
                    // Set Ticker Message
                    .setTicker(message)
                    // Set Title
                    .setContentTitle(message)
                    // Set Text
                    .setContentText(context.getString(R.string.app_name))
                    // Add an Action Button below Notification
                    .addAction(R.drawable.share,
                            context.getString(R.string.share), pendingShare)
                    // Set PendingIntent into Notification
                    .setContentIntent(contentIntent)
                    // Dismiss Notification
                    .setAutoCancel(true)
                    .setSound(
                            Uri.parse("file:///android_assets/"
                                    + prefs.getString(Constants.NOTIF_SOUND,
                                            "mp3/al_affassi_full.mp3")));

有人可以帮我解决这个问题吗? 谢谢!

【问题讨论】:

    标签: android android-notifications android-resources android-assets


    【解决方案1】:

    将您的 mp3 文件放在 raw 文件夹下。如果放在asset/下,会被压缩两次,无法使用。

    【讨论】:

      【解决方案2】:

      res 下创建一个名为raw 的文件夹,并将您的mp3 文件复制到该文件夹​​中。

      然后就可以设置了,

      setSound(Uri.parse("android.resource://"your package name"/"
                          + R.raw.your_map3_name);
      

      它应该可以工作。

      【讨论】:

        【解决方案3】:
        Android notifications play sound from raw folder   
         try
            {
        
                int res_sound_id = MainActivity.this.getResources().getIdentifier("sound name", "raw", MainActivity.this.getPackageName());
                Uri u= Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/" +res_sound_id );
        
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                long[] pattern = { 0, 200, 500 };
                Notification mNotification = new Notification.Builder(MainActivity.this)
                        .setContentTitle("Its time to Pray")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentIntent(pIntent)
                        .setSound(u)
                        .setVibrate(pattern)
                        .setAutoCancel(true)
                        .build();
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(0, mNotification);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-14
          • 2012-06-13
          • 2012-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多