Notification sound not playing in J2ME

731 views Asked by At

I am working on a J2ME application.
I am using Nokia 6131 NFC phone. I am using NetBeans IDE.
I have 4 forms and I am playing some notification sounds for the user while filling the form.

The problem is sound goes off suddenly after 3 to 4 min and the only solution is to exit the application and again open it.

My Code

public void playSoundOK()
{
        try
        {
            InputStream is = getClass().getResourceAsStream("/OK.wav");
            Player player = Manager.createPlayer(is,"audio/X-wav");

            player.realize();
            player.prefetch();
            player.start();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

Exception

at com.nokia.mid.impl.isa.mmedia.audio.AudioOutImpl.openSession(AudioOutImpl.java:206)
at com.nokia.mid.impl.isa.mmedia.MediaOut.openDataSession(MediaOut.java:282)
at com.nokia.mid.impl.isa.mmedia.MediaPlayer.doPrefetch(MediaPlayer.java:155)
at com.nokia.mid.impl.isa.amms.audio.AdvancedSampledPlayer.doPrefetch(+4)
at com.nokia.mid.impl.isa.mmedia.BasicPlayer.prefetch(BasicPlayer.java:409)
at org.ird.epi.ui.UtilityClass.playSoundOK(UtilityClass.java:139)
at org.ird.epi.ui.EnrollmentForm.targetDetected(+695)
at javax.microedition.contactless.DiscoveryManager.notifyTargetListeners(DiscoveryManager.java : 700)
at javax.microedition.contactless.DiscoveryManager.access$1200(DiscoveryManager.java:103)
at javax.microedition.contactless.DiscoveryManager$Discoverer.notifyIndication(DiscoveryManager.java:882)
at com.nokia.mid.impl.isa.io.protocol.external.nfc.isi.NFCConnectionHandler$IndicationNotifier.run(+67) javax.microedition.media.MediaException: AUD
2

There are 2 answers

0
michael aubert On

I would advise you to split NFC and audio playback into 2 different threads.

It is typically a bad idea to call a method that should take some time to complete (like prefetch) from inside an API callback (like targetDetected) because it makes you rely on a particularly robust kind of internal threading model that may not actually exist in your phone's implementation of MIDP.

You should have one thread whose sole purpose is to play the sounds that your application can emit. Use the NFC callback to send a non-blocking command to play a sound (typically using synchronized access to a queue of commands). The audio playback thread can decide to ignore commands if they were issued at a time when it was busy playing a sound (no point in notifying the users of multiple simultaneous NFC contacts)

0
Telmo Pimentel Mota On

You should close your player. Add the following code to your method:


    PlayerListener listener = new PlayerListener() {
        public void playerUpdate(Player player, String event, Object eventData) {
            if (PlayerListener.END_OF_MEDIA.equals(event)) {
                player.close();
            }
        }
    };
    player.addPlayerListener(listener);