Skip to main content

Playing Mp3 OutOfMeryError

4 replies [Last post]
tadeu_fo
Offline
Joined: 2007-06-02

Hello Everybody,

I am trying to create a J2ME application to play mp3 files. I can play files with at least 100-200K but when I try to play larger files (2MB) the emulator closes with OutOfMemoryError.

I think it is trying to load the file complete on the memory so it blows, how should I do to play those files?

Greetings from Brazil.

Here is the code I am using:
=========================
/*
* ExemploPlayer.java
*
* Created on 9 de Junho de 2007, 20:19
*/

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Control;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.protocol.DataSource;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* Exemplo de aplicação que toca arquivos multimídia
* @author Tadeu Ferreira Oliveira
* @version 0.1
*/
public class ExemploPlayer extends MIDlet {
public void startApp() {
FileConnection fc;
Player player;
try {
//loading file from cell memory card
fc = (FileConnection) Connector.open("file:///b/mobile/u2.mp3");
//obtaining stream
InputStream in = fc.openInputStream();
player = Manager.createPlayer(in, "audio/mpeg");
player.prefetch();
player.start();
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}
=========================

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
terrencebarr
Offline
Joined: 2004-03-04

Hi,

I did get an answer now from the WTK team.

MP3 is not officially supported in WTK 2.5 but will supported in the next WTK version. So that might be one reason its not working as expected. Furthermore, MP3 playback is based on the Windows Wav player implementation which does data buffering and might incur out of memory problems when playing back large files. However, in that case a MediaException ("format not supported") should occur so it might be throwing the wrong exception, which is probably a bug.

Until WTK fully supports and is tested for MP3 playback we suggest you use smaller files while testing your app with WTK and use the real (bigger) file on the device (since the device will have a different and platform-specific playback engine).

Hope that helps,

-- Terrence

terrencebarr
Offline
Joined: 2004-03-04

Hi,

I am still trying to contact someone from the WTK team who can answer this. The link you found may be a hint but I would like to confirm it. I hope to have an answer soon.

-- Terrence

terrencebarr
Offline
Joined: 2004-03-04

Hi,

Can you confirm you are using WTK 2.5 for CLDC/MIDP?

Thanks,

-- Terrence

tadeu_fo
Offline
Joined: 2007-06-02

Hi, Terrence

Yes I do confirm I am using the latest stable version : Sun Java(TM) Wireless Toolkit 2.5 for CLDC/MIDP

It seems that the problem is that the player always tries to load all the file to the memory before playing and at this moment happens the problem.

I have found this:
[i]"Unfortunately, the player constructed by the Wireless toolkit is trying to read the entire content at one go. Even if I were to make a StreamingVideoControl, it will not display the video until it has read the whole file, therefore defeating the purpose of the streaming aspect of this whole experiment."
[/i] here: http://today.java.net/pub/a/today/2006/08/22/experiments-in-streaming-ja...

I am looking for a way it not loads all the file but only a few part that is being played.