itakanzebo
itakanzebo

Reputation: 81

Audio on J2ME, I don't know where is wrong?

I want to do 2 task at same time:

Here is my code:

String wavPath = "file:///" + currentPath + fileName;
FileConnection fc;
try {
    fc = (FileConnection) Connector.open( wavPath );

    if ( !fc.exists() ) {
        throw new IOException( "File does not exists." );
    }

    InputStream is = fc.openInputStream();
    // to do something with raw data as print samples of it

    Player player = Manager.createPlayer( wavPath );
    player.realize();
    player.prefetch();
    player.start();
} catch ( IOException e1 ) {
    e1.printStackTrace();
}

But nothing run, audio file doesn't not run. If I remove line:

InputStream is = fc.openInputStream();

Audio file run very well. But I want to do 2 task at same time, i don't know how to do it. Anybody can help me ?

I have tried use 2 thread, but it still doesn't work, audio file run (thread 1) but thread 2 not run:

new Thread( new Runnable() {
    public void run() {
        try {
            Manager.createPlayer( "file:///E:/" + fileName ).start();
        } catch ( MediaException e ) {
            e.printStackTrace();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }
}).start();

new Thread( new Runnable() {
    public void run() {
        FileConnection fc;
        try {
            fc = (FileConnection) Connector.open( "file:///E:/" + fileName );
            InputStream is = fc.openInputStream();

            byte[] b = new byte[10];
            int length = is.read( b, 0, 10 );
            for ( int i = 0; i < length; i++ ) {
                form.append( b[i] + "" );
            }
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }
}).start();

Upvotes: 2

Views: 484

Answers (2)

ThomasRS
ThomasRS

Reputation: 8287

Your Player instances are garbage collected. Also, file access during playback will likely be 'implementation specific', meaning that it will work on some models and not others. So read the data first, copy it to another file etc, if you want something solid.

You can always go the DataSource-path too.

Upvotes: 2

Anantha Sharma
Anantha Sharma

Reputation: 10098

why don't you use threading... create a thread to play the wav file and use another thread to do read a file...

refer here for further details on threading in J2ME


public class View_PlayMidlet extends MIDlet {

    PlayerThread musicPlayer = new PlayerThread();

    public void startApp() {
        String fileName = "file://e:/abcd.wav";
        musicPlayer.setPlayableFile(fileName);
        FileConnection fc = null;
        InputStream is = null;
        String fileContent = null;
        try {
            fc = (FileConnection) Connector.open("file:///E:/" + fileName);
            is = fc.openInputStream();

            byte[] b = new byte[10];
            int length = is.read(b, 0, 10);

            fileContent = new String(b);

            // display the content of fileContent variable in a form.
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                }
            }
            if (fc != null) {
                try {
                    fc.close();
                } catch (IOException ex) {
                }
            }
        }
        // by this time the file is displayed & you can start playing the file.
        Thread t = new Thread(musicPlayer);
        t.start();

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

public class PlayerThread implements Runnable {

    private String fileName;
    private Player player;

    public PlayerThread() {
    }

    public void run() {
        try {
            player = Manager.createPlayer(fileName);
            player.prefetch();
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopMusic() {
        try {
            player.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPlayableFile(String fileName) {
        this.fileName=fileName;
    }
}

this would roughly be what you are looking for.

Upvotes: 3

Related Questions