java-memidpmmapi

play file wav j2me - IllegalArgumentException at Manager.createPlayer()


I got problem about play wav file in my application.

This is my error:

java.lang.IllegalArgumentException
 at javax.microedition.media.Manager.createPlayer(), bci=8
 at Tajwid.Tajwid.run(Tajwid.java:649)
 at Tajwid.Tajwid.actionPerformed(Tajwid.java:186)
 at com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
 at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:257)

This is my code:

public void run() {
    try {
        InputStream is = getClass().getResourceAsStream("/tes.wav");
        player = Manager.createPlayer(is, "audio/x-wav");

        player.realize();
        // get volume control for player and set volume to max
        vc = (VolumeControl) player.getControl("VolumeControl");
        if (vc != null) {
            vc.setLevel(100);
        }
        player.prefetch();
        player.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

Device Configuration : CLDC-1.1
Device Profile MIDP 2.0


Solution

  • Error message you've got has sufficient information to figure what went wrong in the code.

    Look at it a bit closer:

        java.lang.IllegalArgumentException
         at javax.microedition.media.Manager.createPlayer()...
    

    It says something went wrong in Manager.createPlayer(). From your code, it is apparent that you use method Manager.createPlayer(java.io.InputStream stream, java.lang.String type).

    If you look into API documentation for the method you use (available online), you'll find the explanation when this exception occurs:

        Throws:
            java.lang.IllegalArgumentException - Thrown if stream is null. 
    

    Above means that stream parameter (is in your code) passed to the method is null.

    You could add some logging right after initialization of the is to debug this issue easier:

    InputStream is = getClass().getResourceAsStream("/tes.wav");
    // add some logging to see if initialization was OK or not:
    System.out.println("input stream is null: [" + (is == null) + "]");
    

    That way, when running your MIDlet in emulator, you will see whether is was initialized as expected or not.

    Actually, looking at the code I would guess that you made a typo in file name passed to getResourceAsStream: "/tes.wav" looks like a mis-typed "/test.wav".