open http connection for audio streaming
I am trying to create an application to play a live radio.I am using the following method to open the http connection,and giving this url "http://64.62.xxx.xxxx:xxx/" But i am getting nullpointer exception,
public InputStream OpenHttpConnection(String urlString) throws IOException
开发者_运维技巧{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Error connecting");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
conn.setDoInput(true);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
System.out.println("Response : "+response);
if (response == HttpURLConnection.HTTP_OK)
{
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
Is there any other way for streaming data from such kind of url or i need to add something else in the above method ??
I created an instance of the MediaPlayer.My logcat is displaying the following error :
05-25 15:50:08.067: DEBUG/MediaPlayer(1032): Couldn't open file on client side, trying server side 05-25 15:50:08.189: WARN/MediaPlayer(1032): info/warning (1, 26) 05-25 15:50:08.217: ERROR/PlayerDriver(31): Command PLAYER_INIT completed with an error or info PVMFFailure 05-25 15:50:08.217: ERROR/MediaPlayer(1032): error (1, -1) 05-25 15:50:08.237: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): create failed: 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): java.io.IOException: Prepare failed.: status=0x1 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.media.MediaPlayer.prepare(Native Method) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.media.MediaPlayer.create(MediaPlayer.java:608) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.media.MediaPlayer.create(MediaPlayer.java:585) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at org.sample.AudioPlayer.AudioPlayer.PlayRadio(AudioPlayer.java:190) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at org.sample.AudioPlayer.AudioPlayer.access$1(AudioPlayer.java:123) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at org.sample.AudioPlayer.AudioPlayer$3.onClick(AudioPlayer.java:77) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.View.performClick(View.java:2364) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.View.onTouchEvent(View.java:4179) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.widget.TextView.onTouchEvent(TextView.java:6541) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.View.dispatchTouchEvent(View.java:3709) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.os.Handler.dispatchMessage(Handler.java:99) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.os.Looper.loop(Looper.java:123) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at android.app.ActivityThread.main(ActivityThread.java:4363) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at java.lang.reflect.Method.invokeNative(Native Method) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at java.lang.reflect.Method.invoke(Method.java:521) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 05-25 15:50:08.357: DEBUG/MediaPlayer(1032): at dalvik.system.NativeStart.main(Native Method) 05-25 15:51:05.277: DEBUG/dalvikvm(97): GC freed 14 objects / 560 bytes in 127ms 05-25 15:51:13.280: DEBUG/AndroidRuntime(1032): Shutting down VM 05-25 15:51:13.280: WARN/dalvikvm(1032): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 05-25 15:51:13.280: ERROR/AndroidRuntime(1032): Uncaught handler: thread main exiting due to uncaught exception
Thanks!!
First of check the connection from the server u could use a HTTP Debugger As u want to play audio Create a mediaplayer instanse and set the filedescriptor in SetDataSource
m_player.setOnPreparedListener(this);
m_player.setAudioStreamType(AudioManager.STREAM_MUSIC);
m_player.setDataSource(filedescriptor fd);
m_player.prepareAsync();
精彩评论