这是我的媒体播放器的完整代码,它的作用是,它从URL读取文件,将它们存储到数组中,然后从阵列的位置ZERO开始播放(song_url)。我想我的模拟器有问题,是吗?请帮帮我,谢谢你。
package com.hiphop.streamingmediaplayer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Jsonmedia extends Activity {
private MediaPlayer mp;
Button play;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jsonview);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
// Create a URL for the desired page
URL url = new URL(
"http://reallifethug.webs.com/temp_list.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
int ch;
int EOF = -1; // end of file
String list[];
String str = "";
while ((ch = in.read()) != EOF) {
str += (char) ch;
}
in.close();
list = str.split("\n");
for (ch = 0; ch < list.length; ++ch)
list[ch] += "\n";
String song_name[] = new String[list.length];
String song_url[] = new String[list.length];
for (int i = 0; i < list.length; i++) {
song_name[i] = "";
song_url[i] = "";
int a = 0;
char c;
while ((c = list[i].charAt(a++)) != ' ')
song_name[i] += c;
while ((c = list[i].charAt(a++)) != '\n')
song_url[i] += c;
System.out.println(song_name[i]);
System.out.println(song_url[i]);
}
mp.setDataSource(song_url[0]);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.jsonmedia, menu);
return true;
}
}
这些是应用程序STARTS后的错误
05-16 12:42:20.918: I/Process(1832): Sending signal. PID: 1832 SIG: 9
05-16 12:42:21.678: E/Trace(1882): error opening trace file: No such file or directory (2)
05-16 12:42:22.228: I/Choreographer(1882): Skipped 73 frames! The application may be doing to o much work on its main thread.
05-16 12:42:22.288: D/gralloc_goldfish(1882): Emulator without GPU emulation detected.
点击“播放”按钮后,这些是我的错误,应用程序“不幸停止”
05-16 12:46:00.698: D/AndroidRuntime(1882): Shutting down VM
05-16 12:46:00.698: W/dalvikvm(1882): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-16 12:46:00.758: E/AndroidRuntime(1882): FATAL EXCEPTION: main
05-16 12:46:00.758: E/AndroidRuntime(1882): android.os.NetworkOnMainThreadException
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-16 12:46:00.758: E/AndroidRuntime(1882): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-16 12:46:00.758: E/AndroidRuntime(1882): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-16 12:46:00.758: E/AndroidRuntime(1882): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
05-16 12:46:00.758: E/AndroidRuntime(1882): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-16 12:46:00.758: E/AndroidRuntime(1882): at java.net.URL.openStream(URL.java:462)
05-16 12:46:00.758: E/AndroidRuntime(1882): at com.hiphop.streamingmediaplayer.Jsonmedia$1.onClick(Jsonmedia.java:38)
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.view.View.performClick(View.java:4204)
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.view.View$PerformClick.run(View.java:17355)
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.os.Handler.handleCallback(Handler.java:725)
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.os.Looper.loop(Looper.java:137)
05-16 12:46:00.758: E/AndroidRuntime(1882): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-16 12:46:00.758: E/AndroidRuntime(1882): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:46:00.758: E/AndroidRuntime(1882): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 12:46:00.758: E/AndroidRuntime(1882): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-16 12:46:00.758: E/AndroidRuntime(1882): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-16 12:46:00.758: E/AndroidRuntime(1882): at dalvik.system.NativeStart.main(Native Method)
05-16 12:46:04.348: I/Process(1882): Sending signal. PID: 1882 SIG: 9
答案 0 :(得分:1)
您收到NetworkOnMainThreadException。抛出它是因为不允许在应用程序的主线程中执行网络操作。
关于此例外的第一次点击谷歌:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
谷歌关于此例外的第二次搜索:How to fix android.os.NetworkOnMainThreadException?
答案 1 :(得分:1)
不要在MainThread上运行网络。
答案 2 :(得分:1)
看起来您正在尝试在主UI线程上运行网络请求。自3.0以来,Android不允许你这样做(我相信)。这样做会导致UI锁定,直到请求完成,在请求执行期间使您的应用程序无效。
您必须在新的Thread
或ASyncTask
中运行您的请求,才能加载UI线程。您可以找到有关如何使用多个线程here.
答案 3 :(得分:0)
这是一种解决方法,
StrictMode.ThreadPolicy ourPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ourPolicy);
在onCreate()方法中添加它