Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stream MP3 To Mobile

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've got the following sample program where you can provide it a link to an MP3 file, it must them stream the file.
But once I select the link it doesn't play, please advise.

Program:

import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.midlet.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import java.io.InputStream;

public class PlayerMIDlet extends MIDlet implements
CommandListener, PlayerListener, Runnable {
private Display display;
private Form form;
private TextField url;
private Command start = new Command("Play",
Command.SCREEN, 1);
private Command stop = new Command("Stop",
Command.SCREEN, 2);
private Player player;

public PlayerMIDlet() {
display = Display.getDisplay(this);
form = new Form("Demo Player");
url = new TextField("Enter URL:", "", 100,
TextField.URL);
form.append(url);
form.addCommand(start);
form.addCommand(stop);
form.setCommandListener(this);
display.setCurrent(form);
}

protected void startApp() {
try {
if(player != null && player.getState() ==
Player.PREFETCHED) {
player.start();
} else {
defplayer();
display.setCurrent(form);
}
}
catch(MediaException me) {
reset();
}
}

protected void pauseApp() {
try {
if(player != null && player.getState() ==
Player.STARTED) {
player.stop();
} else {
defplayer();
}
}
catch(MediaException me) {
reset();
}
}

protected void destroyApp(
boolean unconditional) {
form = null;
try {
defplayer();
}
catch(MediaException me) {
}
}

public void playerUpdate(Player player,
String event, Object data) {
if(event == PlayerListener.END_OF_MEDIA) {
try {
defplayer();
}
catch(MediaException me) {
}
reset();
}
}

public void commandAction(Command c, Displayable d) {
if(c == start) {
start();
} else if(c == stop) {
stopPlayer();
}
}

public void start() {
Thread t = new Thread(this);
t.start();
}

// to prevent blocking, all communication should
// be in a thread
// and not in commandAction
public void run() {
play(getURL());
}

String getURL() {
return url.getString();
}

void play(String url) {
try {
VolumeControl vc;
defplayer();
// create a player instance
HttpConnection conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
InputStream is = conn.openInputStream();
player = Manager.createPlayer(is,"audio/mpeg");
player.addPlayerListener(this);
// realize the player
player.realize();
vc = (VolumeControl) player.getControl("VolumeControl");
if(vc != null) {
Form v = new Form("Playing Audio...");
StringItem si = new StringItem("Status: ",
"Playing...");
v.append(si);;
display.setCurrent(v);
}
player.prefetch();
player.start();
}
catch(Throwable t) {
reset();
}
}

void defplayer() throws MediaException {
if (player != null) {
if(player.getState() == Player.STARTED) {
player.stop();
}
if(player.getState() == Player.PREFETCHED) {
player.deallocate();
}
if(player.getState() == Player.REALIZED ||
player.getState() == Player.UNREALIZED) {
player.close();
}
}
player = null;
}

void reset() {
player = null;
}

void stopPlayer() {
try {
defplayer();
}
catch(MediaException me) {
}
reset();
}
}

 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic