-1

以下のコードを使用して、2分後にトーストメッセージを取得していますが、同時にサウンドアラートを追加したいと考えています。

Calendar cal = Calendar.getInstance();
     cal.get(Calendar.DATE);
     cal.add(Calendar.MINUTE,2);
     Intent intent = new Intent(this, MyBroadcastReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            alarmManager.set(AlarmManager.RTC_WAKEUP,  cal.getTimeInMillis()
                , pendingIntent);

            Toast.makeText(this, "Time to get",
                    Toast.LENGTH_LONG).show();  

次のコードを使用しようとしました:

int icon = R.drawable.ic_launcher;        // icon from resources
            CharSequence tickerText = "Hello";              // ticker-text
            long when = cal.getTimeInMillis();        // notification time
            Context context = getApplicationContext();      // application Context
            CharSequence contentTitle = "My notification";  // message title
            CharSequence contentText = "Hello World!";      // message text

            Intent notificationIntent = new Intent(this, MyBroadcastReceiver.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 12345, notificationIntent, 0);

            // the next two lines initialize the Notification, using the configurations above
            Notification notification = new Notification(icon, tickerText, when);
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            notification.defaults |= Notification.DEFAULT_SOUND;
4

1 に答える 1

0

SoundPool次のようなクラスを使用できます。

    private SoundPool soundPool;
    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
    soundPoolMap = new HashMap<Integer, Integer>();
    //Put your sounds here, from raw resources
    soundPoolMap.put(SOUND_SCHIVATO, soundPool.load(ctx, R.raw.schivato, 1));
    soundPoolMap.put(SOUND_GONG, soundPool.load(ctx, R.raw.gong, 1));
    soundPoolMap.put(SOUND_AHH, soundPool.load(ctx, R.raw.ahhh, 1));
    ...

    private void playSound(int sound) {
        /* Volume calculations */
        AudioManager mgr = (AudioManager) ctx
                .getSystemService(Context.AUDIO_SERVICE);
        float streamVolumeCurrent = mgr
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float streamVolumeMax = mgr
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = streamVolumeCurrent / streamVolumeMax;

        /* Play the sound with the correct volume */
        soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
}

私のプロジェクトでは、投稿したスニペットを含むすべての音声通知を管理するクラスを作成しました。このクラスは非常に単純で、カスタムサウンドをにロードし、HashMapそれらを再生するためのパブリックメソッドを公開するだけです。

ContextSounPoolのリソースをロードするために必要です。簡単なチュートリアルはこちらから入手できます

于 2012-08-31T13:31:39.013 に答える