我正在尝试创建一个播放实时广播的应用程序。我正在使用以下方法打开http连接,并提供此URL“http://64.62.xxx.xxxx:xxx /” 但我得到了nullpointer异常,
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;
}
有没有其他方法可以从这种网址流式传输数据,或者我需要在上述方法中添加其他内容?
我创建了一个MediaPlayer实例。我的logcat显示以下错误:
05-25 15:50:08.067:DEBUG / MediaPlayer(1032):无法在客户端打开文件,尝试服务器端 05-25 15:50:08.189:WARN / MediaPlayer(1032):info / warning(1,26) 05-25 15:50:08.217:ERROR / PlayerDriver(31):命令PLAYER_INIT已完成,错误或信息PVMFFailure 05-25 15:50:08.217:ERROR / MediaPlayer(1032):错误(1,-1) 05-25 15:50:08.237:WARN / PlayerDriver(31):PVMFInfoErrorHandlingComplete 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):创建失败: 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):java.io.IOException:准备失败:status = 0x1 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.media.MediaPlayer.prepare(Native Method) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.media.MediaPlayer.create(MediaPlayer.java:608) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在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):在android.view.View.performClick(View.java:2364) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.view.View.onTouchEvent(View.java:4179) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.widget.TextView.onTouchEvent(TextView.java:6541) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.view.View.dispatchTouchEvent(View.java:3709) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在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):在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):在android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.os.Handler.dispatchMessage(Handler.java:99) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在android.os.Looper.loop(Looper.java:123) 05-25 15:50:08.357:DEBUG / MediaPlayer(1032):在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在127ms内释放了14个对象/ 560个字节 05-25 15:51:13.280:DEBUG / AndroidRuntime(1032):关闭VM 05-25 15:51:13.280:WARN / dalvikvm(1032):threadid = 3:线程退出未捕获的异常(组= 0x4001b188) 05-25 15:51:13.280:ERROR / AndroidRuntime(1032):未捕获的处理程序:由于未捕获的异常而导致主线程退出
谢谢!
答案 0 :(得分:0)
首先检查来自服务器的连接你可以使用HTTP调试器 因为你想播放音频 创建一个mediaplayer instanse并在SetDataSource
中设置filedescriptorm_player.setOnPreparedListener(this);
m_player.setAudioStreamType(AudioManager.STREAM_MUSIC);
m_player.setDataSource(filedescriptor fd);
m_player.prepareAsync();