java - J2ME上的音频,不知道哪里错了?

标签 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个线程,但仍然不起作用,音频文件运行(线程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();

最佳答案

为什么不使用线程... 创建一个线程来播放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;
    }
}

这大概就是您正在寻找的内容。

关于java - J2ME上的音频,不知道哪里错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6641238/

相关文章:

blackberry - eclipse中调试黑莓应用 "source not found"问题

java - 如何在j2me应用程序中启动浏览器而不确认

Java 获取 IPv4 地址

java - double 值与常量的比较

java - 如何强制 while 循环一次执行一个可运行程序。

java - 如何使用 Eclipse 为诺基亚创建 J2ME midlet

java - 在 JDBC 的 QUERY WHERE 子句中指定变量名

ios - 如何播放不会停止 iTunes 或其他音乐播放器的声音警报或音效?

linux - 如何从音频文件中获取振幅列表?

c# - 检测用户何时按下Shift键