user1696050
user1696050

Reputation: 1

How to add warning tone after received NFCdate succeed?

I have done an android project on NFC and I want to add a warning tone after receiving NFC date succeed ,how can I do it?

Upvotes: 0

Views: 78

Answers (1)

Jacek Milewski
Jacek Milewski

Reputation: 3304

Use SoundPool class. Place your audio files in /res/raw/ and first init loading them (for example in OnCreate Method) and then request to play selected audio file. here is code example:

private SoundPool soundPool;
private boolean tiltSoundsLoaded = false;
private int tiltSoundID;
private int tiltFailureSoundID;

public void initTiltSounds(Context context) {
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

       soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    @Override
         public void onLoadComplete(SoundPool soundPool, int sampleId,
             int status) {
           tiltSoundsLoaded = true;
         }
       });
       tiltSoundID = soundPool.load(context, R.raw.swosh_sound_effect, 1);
       tiltFailureSoundID = soundPool.load(context, R.raw.fail_metallic, 1);
}

public void playTiltSound(AudioManager audioManager, boolean success) {
    try{
        int soundToPlay;
        if(success)
            soundToPlay = tiltSoundID;
        else
            soundToPlay = tiltFailureSoundID;

        float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (tiltSoundsLoaded) 
            soundPool.play(soundToPlay, actualVolume, actualVolume, 1, 0, 1f);
        else 
            Log.e(LOG_TAG, "Tilt Sound not loaded");
    }catch(Exception e){
            Log.e(LOG_TAG, "Could not play tilt sound");
    }
}

Upvotes: 1

Related Questions