JMU
Auditory Content in J2ME
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Differences from J2SE
Presenting Sampled Auditory Content

An Example

j2meexamples/src/auditory/AudioPlayer.java
        package auditory;

import java.io.*;
import java.util.*;
import javax.microedition.media.*;


/**
 * AudioPlayer objects can be used to play audio files
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class AudioPlayer implements PlayerListener
{
    private boolean                stopAll;
    private Enumeration            currentPlayList;
    private InputStream            inputStream;
    private Player                 player;
    
    /**
     * Default Constructor
     */
    public AudioPlayer()
    {
        stopAll = false;
    }
    
    
    /**
     * Play a collection of sound files sequentially
     *
     * @param names   The name of the files/resources
     */
    public void play(Enumeration names)
    {
        currentPlayList = names;
        playNext();
    }
    
    
    /**
     * Play a sound file
     *
     * @param  name   The name of the audio file/resource
     * @return        false if the sound does not play.
     */
    public boolean play(String name)
    {
        boolean played;
        
        played  = false;
        stopAll = false;
        
        try
        {
            inputStream = getClass().getResourceAsStream("/"+name);
            if ((inputStream != null) && (inputStream.available() > 0))
            {
                // Interrupt any active sounds
                if (player != null)
                {
                    if (player.getState() == Player.STARTED)
                    {
                        player.stop();
                        inputStream.close();
                    }
                }
                
                player = Manager.createPlayer(inputStream, "audio/X-wav");
                player.addPlayerListener(this);
                player.start();
                played = true;
            }
        }
        catch (MediaException me)
        {
            played = false;
        }
        catch (IOException ioe)
        {
            played = false;
        }
        return played;
    }
    
    
    /**
     * Play the next file in the playlist
     */
    private void playNext()
    {
        if (currentPlayList.hasMoreElements())
        {
            String toPlay   = (String)currentPlayList.nextElement();
            play(toPlay);
        }
    }
    
    
    /**
     * Handle playerUpdate events (required by PlayerListener)
     */
    public void playerUpdate(Player player, String event, Object eventData)
    {
        if (event == PlayerListener.END_OF_MEDIA)
        {
            stop(); // This will generate a STOPPED event
        }
        else if (event == PlayerListener.STOPPED)
        {
            if (!stopAll) playNext();
        }
    }
    
    
    /**
     * Stop the current audio file
     */
    public void stop()
    {
        try
        {
            if (player != null)      player.stop();
            if (inputStream != null) inputStream.close();
        }
        catch (Exception e)
        {
            // Ignore
        }
    }
}