venerdì, 12 dicembre 2008

The following article shows how a simple mp3 file in your jar application can be played using the mmapi classes (included in the JSR-135 optional package or in all the MIDP 2.0 devices which include a subset of mmapi).

Suppose the file is sound.mp3. You can create a class implementing this method:

    public void playSound(final String soundFile, final Display display) {  
        try {
            InputStream is =
                getClass().getResourceAsStream( "/sound.mp3" );
           
            Player player = Manager.createPlayer( is, "audio/mpeg" );
            player.addPlayerListener( this );
            player.realize();
            player.prefetch( );
            player.start();
        }
        catch ( Exception e ) {        
            e.printStackTrace();
           
            if ( player != null ) {
                player.close();
                player = null;
            }
        }
    }


In the method we have added a PlayerListener (player.addPlayerListener( this )) so we suppose the class implementing the playSound method implements the following PlayerListener interface playerUpdate ( Player player, String event, Object arg2 ) method:

public void playerUpdate ( Player player, String event, Object arg2 ) {
        if ( event.equals( STOPPED ) || event.equals( ERROR ) || event.equals( END_OF_MEDIA ) ||  event.equals( DEVICE_UNAVAILABLE ) || event.equals( CLOSED ) ) {
           
player.deallocate();
            player.close();
            player = null;
        }
    } 


This method frees resources when the player ends the sound playback or an unexpected error occurs. The same method can be used to play sound from network or using the FileConnection API (in those case the URL of the file changes).


Did you like the article? Buy me a coffee!

postato da: emanuelepec alle ore 09:18 | Permalink | commenti
categoria:tipsandtricks, j2me, mmapi


mercoledì, 23 luglio 2008

If you want to set the volume in the MediaPlayer using the following code:

                InputStream is = getClass().getResourceAsStream( "/sound.amr" );
                Player player = Manager.createPlayer(is, soundType);
               
player.addPlayerListener( this );
               
player.realize();
                VolumeControl volumeControl =
                (VolumeControl)
player .getControl( "VolumeControl" );
                if ( volumeControl != null ) {
                    volumeControl.setLevel( volume );
                }

when you launch the player with:

                player.start();

no sound can be heard on the LG KE970 Shine without getting any error.

This device requires to get the prefetched status if you want to set the volume and play the sound correctly. The working code is then:


                InputStream is = getClass().getResourceAsStream( "/sound.amr" );
                Player player = Manager.createPlayer(is, soundType);
               
player.addPlayerListener( this );
               
player.realize();
               
player.prefetch( );
                VolumeControl volumeControl =
                (VolumeControl)
player .getControl( "VolumeControl" );
                if ( volumeControl != null ) {
                    volumeControl.setLevel( volume );
                }


Maybe other phones are affected as well (especially LG). Post comment if you other phones with the same problem.


Did you like the article? Buy me a coffee!

postato da: emanuelepec alle ore 20:44 | Permalink | commenti
categoria:bug , tipsandtricks, j2me, lg , mmapi


venerdì, 08 febbraio 2008

If you look at the documents or at the posts in the Sony Ericsson Developer website, you can see that Audio Capture using the MMAPI (JSR-135) should be supported starting from the Java Platform 6.

Reading the system properties from within a midlet, the assumption would be confirmed because the System.getProperty("supports.audio.capture") returns "true" and also listing the supported content types you get the result where several audio types are supported for the protocol "capture".

Unfortunately if you try to record audio in a phone like Sony-Ericsson W850 using this code:

 try {
// Create a Player that captures live audio.
Player p = Manager.createPlayer("capture://audio");
p.realize();
// Get the RecordControl, set the record stream,
// start the Player and record for 5 seconds.
RecordControl rc = (RecordControl)p.getControl("RecordControl");
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
p.start();
Thread.currentThread().sleep(5000);
rc.commit();
p.close();
} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException ie) { }


the ByteArrayOutputStream output has size equals to 0.

This is a known bug according with the Sony Ericsson Developer website says:

http://developer.sonyericsson.com/entry.jspa?externalID=705&categoryID=6

in K610 and K800 but it seems the same in the W850 (and maybe in other phones also).

Furthermore, the previous link gives a workaround to save the stream in a file but it doesn't say anything about saving the audio in a byte array stream.

If anyone else has other phone where he experienced the same problem, please post a comment!


Did you like the article? Buy me a coffee!

postato da: emanuelepec alle ore 16:24 | Permalink | commenti
categoria:bug , j2me, sony ericsson, mmapi


domenica, 13 gennaio 2008

Nokia Series60 3rd EditionThe method setDisplaySize() for VideoControl doesn't work properly for S60 3rd edition.
The implementation doesn't user the parameters passed but uses always the max allowed size mantaining the aspect ratio.

Here is the link to the Nokia Technical Library about the bug:

Go to Technical Library


Did you like the article? Buy me a coffee!

postato da: emanuelepec alle ore 22:25 | Permalink | commenti
categoria:bug , nokia, j2me, mmapi


martedì, 27 novembre 2007

Starting from 3rd release it is possible take picture from J2ME application using the camera in the Nokia series40.

There is a difference between in the startPlayer action between series40 and series60.
In the series60 you can start the video to take the picture using the string capture://video. This doesn't work for series40 where you have to use capture://image.

You can make your application able to understand which string have to use writing a code like that:

Player p;
try {
    Player p = Manager.createPlayer("capture://image");
}
catch (Exception e) {
    p = Manager.createPlayer("capture://video");
}.....

It is important use capture://image first because some series 40 support the capture://video but in that way you can take video film but not snapshot.


Did you like the article? Buy me a coffee!

postato da: emanuelepec alle ore 08:04 | Permalink | commenti
categoria:nokia, j2me, mmapi


Blogs Directory