关于J2ME的音频,我不知道哪里出错了?

时间:2011-07-10 13:00:00

标签: java audio java-me inputstream

我想同时做两个任务:

  • 播放音频文件。

  • 阅读它的原始数据以做某事

这是我的代码:

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();
}

但没有运行,音频文件不会运行。如果我删除行:

InputStream is = fc.openInputStream();

音频文件运行得很好。但是我想同时做2个任务,我不知道怎么做。有人可以帮帮我吗?

我试过使用2个线程,但它仍然不起作用,音频文件运行(线程1)但线程2没有运行:

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();

2 个答案:

答案 0 :(得分:3)

为什么不使用线程... 创建一个线程来播放wav文件 并使用另一个线程来读取文件...

请参阅here以获取有关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;
    }
}

这大致就是你要找的东西。

答案 1 :(得分:2)

您的播放器实例是垃圾收集的。此外,播放期间的文件访问可能是“特定于实现”,这意味着它可以在某些模型上工作,而不是在其他模型上工作。因此,首先读取数据,将其复制到另一个文件等,如果你想要一些可靠的东西。

您也可以始终go DataSource - 路径。