簡體   English   中英

J2ME媒體播放器無法播放

[英]J2ME media player doesn't play

public class Midlet extends MIDlet implements CommandListener{

  Player p;

  public void startApp() {
      Display.getDisplay(this).setCurrent(new SongsList(this));
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
      notifyDestroyed();
  }

  public void commandAction(Command cmnd, Displayable dsplbl) {
      if (cmnd.getLabel().equals("Exit"))
      {
          destroyApp(true);
      }
      else
      {
          try {
              //InputStream is = getClass().getResourceAsStream("/res/getlucky.mpeg");
              //p = Manager.createPlayer(is, "audio/mpeg");
              p = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
              p.realize();
              p.start();
          } catch (IOException ex) {
              ex.printStackTrace();
          } catch (MediaException ex) {
              ex.printStackTrace();
          }  
      }
  }
}

這是歌曲列表類:

public class SongsList extends List{

public SongsList(Midlet midlet)
{
    super("Songs", List.IMPLICIT);
    append("get lucky", null);
    addCommand(new Command("Exit", Command.EXIT, 0));
    addCommand(new Command("Select", Command.OK, 0));
    setCommandListener(midlet);
}

}

通過存儲在項目中的文件(在src / res下)嘗試使用:

inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");

以及來自HTTP:

//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");

什么都沒用,我在做什么錯?

編輯:我試圖刪除我的應用程序,只是將其復制粘貼到一個新項目中,並且由於某種原因它起作用了。.現在我遇到了新問題:1)我嘗試播放一首歌-這是鏈接http:// puu.sh/6n9jC.mp3它沒有播放,所以我猜想可以播放的文件有限,有人可以告訴我這是什么限制嗎? 2)我試圖用RecordPlayer錄制音頻,但它始終為null

public AudioAnalyzer()
{
    try {
        thread = new Thread(this);
        recordFinished = false;
        //inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
        //audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
        audioPlayer = Manager.createPlayer("http://puu.sh/35YTG.mp3");
        //audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
        audioPlayer.realize();
        System.out.println(System.getProperty("supports.audio.capture"));
        recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
        recordOutput = new ByteArrayOutputStream();
        recordControl.setRecordStream(recordOutput);
        recordControl.startRecord();
        audioPlayer.start();
        //thread.start();
    } catch (MediaException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

我什至嘗試打印該系統是否支持音頻捕獲並且結果為true,但我在此行得到NullPointException:

recordOutput = new ByteArrayOutputStream();

盡管我試圖從播放器獲取recordcontrol,但它仍然為null:

recordControl = (RecordControl)audioPlayer.getControl("RecordControl");

我想我讀到它將始終提供NullPointerException,除非您在真實設備上運行它,而不是模擬器是真的嗎? 有人可以驗證嗎? 如果是這樣,並且如果我當前不擁有設備,則不能以其他任何方式在模擬器中使用recordcontrol功能(假設recordcontrol在模擬器上不起作用)怎么辦?

文件大小為8MB(可能在手機上播放),請嘗試使用此代碼

public void initMedia(final String aFileUrl) {
        if (m_player == null) {
            try {
                m_player = Manager.createPlayer(aFileUrl);
                m_player.addPlayerListener(this);
                m_player.realize();
                m_player.prefetch();
                m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");                
            } catch (IOException ex) {
            } catch (Exception ex) {
            } catch (OutOfMemoryError e) {
            }
        }
    }

在您的代碼中,我想您會錯過“ m_player.prefetch()”,請嘗試此操作。 並打印您的異常消息...

此代碼通常用於文件,資源,http ...

public void initMedia(final String aProtocol, final String aMediaSource) {
    if (m_player == null) {
        try {
            if (aMediaSource.indexOf("file://") == 0) {
                InputStream iRecordStream = Connector.openInputStream(aMediaSource);
                m_player = Manager.createPlayer(iRecordStream, "audio/amr");
            } else {
                m_player = Manager.createPlayer(aProtocol);
            }
            m_player.addPlayerListener(this);
            m_player.realize();
            boolean isPrefetch = true;
            try {
                m_player.prefetch();
            } catch (Exception ex) {
                isPrefetch = false;
            }
            // trick to pass prefetch error
            if (!isPrefetch) {
                if (m_player != null) {
                    m_player.close();
                    m_player = null;                        
                }
                if (aMediaSource.indexOf("file://") == 0) {
                    InputStream iRecordStream = Connector.openInputStream(aMediaSource);
                    m_player = Manager.createPlayer(iRecordStream, "audio/amr");
                } else {
                    m_player = Manager.createPlayer(aProtocol);
                }
                m_player.addPlayerListener(this);
                m_player.realize();
                m_player.prefetch();
            }                

            m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");                
        } catch (IOException ex) {
        } catch (Exception ex) {
        } catch (OutOfMemoryError e) {
        }
    }
}

通常,在進行J2ME開發時,您應該始終在多個真實設備上測試您的應用程序。 仿真器不能被信任。

此外,J2ME的碎片非常分散,各種設備都有各種錯誤,並且在相同的代碼下的行為也有所不同。 這將影響許多領域的任何應用程序。 一個區域是音頻播放。

例如,某些設備要求您使用Achieve()和prefetch()方法,而其他設備如果使用prefetch()將崩潰。 唯一可能的解決方案(如果您希望支持盡可能多的設備)是使用多個try / catch塊。

有關MIDP2.0音頻播放的詳細說明和其他提示,請參閱此鏈接,網址為http://indiegamemusic.com/help.php?id=1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM