【问题标题】:Android: Playing own sound on alarmAndroid:在警报时播放自己的声音
【发布时间】:2018-06-16 04:00:50
【问题描述】:

我的应用有闹钟功能。我的要求是在闹钟响起时播放自己的声音,但我无法做到这一点。它仅在警报响起时显示通知。我要播放的声音文件位于 Res 的 raw 文件夹中。 下面我发布我的代码:

在我的活动课上:

Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
        AlarmIntent.putExtra("Ringtone", 
                Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
        PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 
                (60 * 1000), (24 * 60 * 60 * 1000), Sender);

在接收器类中:

public void onReceive(Context context, Intent intent) { 

    Intent in = new Intent(context, SnoozeEvent.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
    manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.sound = (Uri)intent.getParcelableExtra("Ringtone");
    manager.notify(1, notification);  
}

【问题讨论】:

    标签: android alarmmanager


    【解决方案1】:

    您可以在捆绑包或 SD 卡中设置自定义声音。仅将声音文件的路径设置为通知声音。使用下面的代码。

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

    【讨论】:

    • @iAndroid 这对我来说不起作用你如何设置声音?
    • @user3233280 请替换 url 字符串中的包名,并在 raw 文件夹中添加声音文件。
    【解决方案2】:

    尝试改变这两行:

    //In your activity:
    AlarmIntent.putExtra("Ringtone",getResources().getResourceName(R.raw.shankh_final_mid));
    ....   
    //In your reciever
    notification.sound = (Uri)(Uri.parse(intent.getStringExtra("Ringtone")));
    

    【讨论】:

      【解决方案3】:

      试试这个代码来发出你自己的通知声音:

      NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Intent notificationIntent = new Intent(this, YourActivity.class);
      PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
      Notification note  = new Notification(R.drawable.icon, "Your Text to Show", System.currentTimeMillis());
      note.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
      int nid = 123456789;
      manager.notify(nid, note);
      

      【讨论】:

      • 我的通知声音在 res 的 raw 文件夹中。我不想把它写到 sd 卡上
      【解决方案4】:

      这个答案有点过时了。

      我必须打开自己的问题here 才能在警报频道上播放。

      这是我的工作解决方案。

      mediaPlayerScan = new MediaPlayer();
      try {
        mediaPlayerScan.setDataSource(getContext(),
                Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));
      
        if (Build.VERSION.SDK_INT >= 21) {
          mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
                  .setUsage(AudioAttributes.USAGE_ALARM)
                  .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                  .build());
        } else {
          mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
        }
        mediaPlayerScan.prepare();
      } catch (IOException e) {
        e.printStackTrace();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多