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

发布于 2024-11-19 16:41:49 字数 1677 浏览 3 评论 0原文

我想同时执行 2 个任务:

  • 播放音频文件。

  • 读取它的原始数据来执行某些操作

这是我的代码:

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

I want to do 2 task at same time:

  • Play an audio file.

  • Read raw data of it to do something

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2024-11-26 16:41:49

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

请参阅此处了解更多信息有关 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;
    }
}

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

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.

如痴如狂 2024-11-26 16:41:49

您的 Player 实例已被垃圾收集。此外,播放期间的文件访问可能是“特定于实现的”,这意味着它适用于某些型号,而不适用于其他型号。因此,如果您想要可靠的东西,请先读取数据,然后将其复制到另一个文件等。

您可以随时前往DataSource -路径也是如此。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文