12-05-2009 01:30 PM
package com.rim.samples.device.helloworlddemo;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import javax.microedition.io.*;
import java.io.InputStream;
import javax.microedition.media.*;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
class HelloWorldDemo extends UiApplication {
public static void main(String[] args) {
HelloWorldDemo theApp = new HelloWorldDemo();
theApp.enterEventDispatcher();
}
private HelloWorldDemo() {
pushScreen(new HelloWorldScreen());
}
}
final class HelloWorldScreen extends MainScreen {
HelloWorldScreen() {
LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.FIELD_HCENTER);
setTitle(title);
try {
String murl = "http://homepage.mac.com/brentris/my_song.mp3;devic
HttpConnection conn = (HttpConnection) Connector.open(murl, Connector.READ_WRITE, false);
InputStream is = conn.openInputStream();
Player _player = Manager.createPlayer(is, "audio/mpeg");
_player.start();
} catch(Exception e) {
System.out.println("Default error connecting to stream...unrecoverable");
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("error=8 " + e.getClass());
}
}
public void close() {
System.exit(0);
super.close();
}
}
--------------------------------------------------
Above is my entire code. It's a CLDC app built with JDE 4.6.0.12. I am using the 4.6.0.266 BlackBerry Bold 9000 Simulator and a 4.6.0 Bold 9000 actual device on AT&T.
In this sample I am using a Garageband MP3 I created (but my final goal is to stream a 64kbit mp3 stream, I"m hiding that for privacy reasons).
This one plays, but only like 50% of the time, maybe. Sometimes it launches and plays the whole song (1 minute, 32 seconds). Sometimes it starts playing but dies after about 5 seconds. Sometimes it refuses to play at all and won't even launch the application.
What in the world is going on here? I posted my entire code above. Somebody please help me.
Thanks!
12-07-2009 09:34 AM
bump. any ideas why it doesn't work reliably?
12-07-2009 11:21 AM
Oops, I had skimed this earlier and didn't realize that it was streaming. Try passing the URL directly into the createPlayer function.
12-07-2009 12:01 PM
sorry could you write out the code for me?
12-07-2009 12:33 PM
I have not tried this myself, I just modified your code.
package com.rim.samples.device.helloworlddemo;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import javax.microedition.io.*;
import java.io.InputStream;
import javax.microedition.media.*;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
class HelloWorldDemo extends UiApplication {
public static void main(String[] args) {
HelloWorldDemo theApp = new HelloWorldDemo();
theApp.enterEventDispatcher();
}
private HelloWorldDemo() {
pushScreen(new HelloWorldScreen());
}
}
final class HelloWorldScreen extends MainScreen {
HelloWorldScreen() {
LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.FIELD_HCENTER);
setTitle(title);
try {
String murl = "http://homepage.mac.com/brentris/my_song.mp3";
boolean found = false;
String[] supTypes = Manager.getSupportedContentTypes("http");
for(int i = 0; i < supTypes.length; i++)
{
if(subTypes[i].equals("audio/mpeg"))
{
found = true;
break;
}
}
if(!found)
{
System.out.println("MP3 not supported.");
return;
}
Player _player = Manager.createPlayer(murl);
_player.start();
} catch(Exception e) {
System.out.println("Default error connecting to stream...unrecoverable");
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("error=8 " + e.getClass());
}
}
public void close() {
System.exit(0);
super.close();
}
}
12-07-2009 01:46 PM
Hmm, it doesn't seem to be playing the audio. Do I need to open an httpconnection and inputstream?
12-07-2009 08:02 PM - edited 12-07-2009 09:02 PM
I forgot about size. I just tried it myself and got HTTP error 413 (Entity too large). Let me try one more thing and then I will post my results.
EDIT: I modified the code back to your original ocde but then edited it again so it buffered the file before creating the player:
HttpConnection conn = (HttpConnection) Connector.open(murl, Connector.READ, false);
InputStream is = conn.openInputStream();
int len = (int)conn.getLength();
byte[] buf = new byte[len];
int bytesRead = 0;
int readIn = 0;
int bufferSize = 1024;
while ((bytesRead != len) && (readIn != -1))
{
readIn = is.read(buf, bytesRead, bufferSize);
bytesRead += readIn;
}
is.close();
conn.close();
ByteArrayInputStream barIS = new ByteArrayInputStream(buf);
Player _player = javax.microedition.media.Manager.createPlayer(barI S, "audio/mpeg");
_player.start();
The problem is getLength returns 24, I doubt your MP3 is 24 bytes. I don't know what happened but if you look at the documentation for getLength it might help you find the problem.