1

我正在尝试在 J2ME 中创建音乐播放器应用程序。我想知道我们如何播放存储在存储卡和手机内存中的 mp3 文件。下面是我试图执行的代码(在其他部分)。它没有任何错误,但不播放任何声音。

playButton.addClickListener(new ClickListener() {
     public void onClick(Component arg0) {
        try{
            if(player.getState() == player.STARTED){
               player.stop();
               player_GUI.playButton.setText("play");
            }
            else{
            player = Manager.createPlayer("file:///E/song1.mp3");
        player_GUI.playButton.setText("pause");
                player.realize();
        player.prefetch();
        player.start();
            }
        }
        catch(Exception e){
           CatchError.showError("click play ", e.getMessage());
        }
 }
        });
4

1 回答 1

3

您不能直接作为字符串参数访问 Memorycard。对于任何类型的内存访问,您需要创建 FileConnection。我给出下面的代码。

Player p ;
  FileConnection connection = (FileConnection) Connector.open("file:///M:/sampleVideo/file1.mpg");
        // If you run on the simulator then it is memorycard and if it device then it is specific to that device
       // FileConnection connection = (FileConnection) Connector.open("file:///memorycard/sampleVideo/6.mp4");
                                InputStream inStream = null;
                                inStream = connection.openInputStream();
                                try {
                                    p = Manager.createPlayer(inStream, "video/mpg");
                                    p.addPlayerListener(this);
                                   p.realize();

首先,您必须找出该设备的存储卡文件夹名称,然后只有您将其硬编码为E :。您可以使用 Sun 的无线工具包示例 PDAPDemo 找到它,将其安装在设备上并找到正确的文件夹名称。然后您就可以玩了存储卡中的媒体文件。

于 2011-11-17T13:13:57.553 回答