简体   繁体   中英

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

this is the songslist class :

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

}

tried use via file stored in project (its under src/res):

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

as well as from HTTP:

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

Nothing works, what am I doing wrong?

EDIT: I've tried to delete my application and just copy paste it to a new project and it worked for some reason.. now I encounter new problems: 1) I try to play a song - this is the link http://puu.sh/6n9jC.mp3 its not playing so I guess there's a limited file size for what can be played can someone tell me what is this limit ? 2) Im trying to record the audio with RecordPlayer but its always 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();
    }
}

I even tried to print if the system is supporting audio capture and the result were true but I get NullPointException at this line :

recordOutput = new ByteArrayOutputStream();

although I tried to get the recordcontrol from the player it is still null :

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

I think I read that it'll always give NullPointerException unless you run it on a real device and not an emulator is that true ? can someone verify it ? and if so what can I do if I don't own a device currently any other way to use recordcontrol feature in emulator (assuming recordcontrol isn't working on emulators).

File size is 8MB (maybe play on your phone), try to this code

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) {
            }
        }
    }

In your code, i guess you miss "m_player.prefetch()", try this. And print your Exception message...

This code in general for file, resourcce, 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) {
        }
    }
}

In general when it comes to J2ME development, you should always test your app on multiple real devices. Emulators can't be trusted.

Also, J2ME is very fragmented, and various devices have various bugs and behaves differently with the same code. This will affect any app on many areas. One area being audio playback.

For example, some devices requires that you use the realize() and prefetch() methods, while other devices will crash if you use prefetch(). The only possible solution (if you wish to support as many devices as possible) is to use multiple try/catch blocks.

See this link for a detailed explanation and other tips'n'tricks on audio playback with MIDP2.0 http://indiegamemusic.com/help.php?id=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM