Package com. test;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import javax. microedition. Io. connector;
Import javax. microedition. Io. httpconnection;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. form;
Import javax. microedition. lcdui. textfield;
Import javax. microedition. Media. Manager;
Import javax. microedition. Media. player;
Import javax. microedition. Media. playerlistener;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
/**
* Online playback is supported using the net method.
* @ Author gx_jin
*
* @ Time: 2011-11-7
*/
Public class musictest extends MIDlet implements runnable
{
Private command play;
Private command exit;
Private form UI;
Private textfield input;
Private display DIS;
Public musictest ()
{
Dis = display. getdisplay (this );
Play = new command ("play", command. OK, 1 );
Exit = new command ("exit", command. Exit, 1 );
Ui = new form ("music test program ");
Input = new textfield ("Enter address", "http: // 59.151.36.12/gddemo/a8.aac", 300, textfield. url );
Ui. append (input );
Ui. addcommand (play );
Ui. addcommand (exit );
Ui. setcommandlistener (New commandlistener (){
Public void commandaction (command C, displayable arg1)
{
If (C = play)
{
System. Out. println (input. getstring ());
Thread thread = new thread (musictest. This );
Thread. Start ();
} Else if (C = Exit)
{
Try
{
Destroyapp (false );
} Catch (midletstatechangeexception E)
{
E. printstacktrace ();
}
Yydestroyed ();
}
}
});
Dis. setcurrent (UI );
}
Protected void destroyapp (Boolean arg0) throws midletstatechangeexception
{
// Todo auto-generated method stub
}
Protected void pauseapp ()
{
// Todo auto-generated method stub
}
Protected void Startapp () throws midletstatechangeexception
{
// Todo auto-generated method stub
}
Private Static final string streamable = "? Streamable = true ";
Public void playsound (string URL)
{
Player s_player = NULL;
Try
{
Inputstream is = getinputstream (URL + streamable );
If (is = NULL)
{
Input. setstring ("is = NULL ");
}
S_player = manager. createplayer (is, "audio/AAc ");
If (s_player = NULL)
{
Input. setstring ("s_player = NULL ");
}
S_player.addplayerlistener (New playerlistener (){
Public void playerupdate (player arg0, string arg1, object arg2)
{
}
});
S_player.realize ();
S_player.prefetch ();
S_player.start ();
} Catch (exception E)
{
E. printstacktrace ();
// Input. setstring (E. getmessage () + "--- end --");
}
}
Private inputstream getinputstream (string URL) throws ioexception
{
Httpconnection conn = NULL;
If (valuse. Network = valuse. cmwap)
{
String remainurl = URL. substring ("http: //". Length ());
Int Index = remainurl. indexof ("/");
String Path = "";
If (index! =-1)
{
Path = remainurl. substring (INDEX );
Remainurl = remainurl. substring (0, index );
}
String host = "";
String Port = "";
Index = remainurl. indexof (":");
If (index! =-1)
{
Host = remainurl. substring (0, index );
Port = remainurl. substring (index + 1 );
} Else
{
Host = remainurl;
Port = NULL;
}
String proxyhost = "10.0.0.172 ";
String proxyport = "80 ";
String cnwapurl = "http: //" + proxyhost + ":" + proxyport + path;
Conn = (httpconnection) connector. Open (cnwapurl, connector. read_write, true );
If (Port = NULL)
{
Conn. setrequestproperty ("X-Online-host", host + ": 80 ");
} Else
{
Conn. setrequestproperty ("X-Online-host", host + ":" + port );
}
Conn. setrequestmethod (httpconnection. post );
String contenttype = conn. getheaderfield ("Content-Type ");
Input. setstring ("contenttype =" + contenttype );
If (contenttype! = NULL & contenttype. indexof ("text/vnd. WAP. WML ")! =-1)
{
Conn. Close ();
Conn = (httpconnection) connector. Open (cnwapurl, connector. read_write, true );
If (Port = NULL)
{
Conn. setrequestproperty ("X-Online-host", host + ": 80 ");
} Else
{
Conn. setrequestproperty ("X-Online-host", host + ":" + port );
}
Conn. setrequestmethod (httpconnection. post );
}
} Else
{
Conn = (httpconnection) connector. Open (URL, connector. read_write, true );
}
Inputstream is = conn. openinputstream ();
Return is;
}
Public void run ()
{
String url = input. getstring ();
Playsound (URL );
}
}