0

这是我的媒体播放器的完整代码,它的作用是从 URL 读取文件,将它们存储到一个数组中,然后从数组的零位置 (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;

}

}

这些是应用程序开始后我的错误

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
4

4 回答 4

1

不要在 MainThread 上操作网络。

于 2013-05-16T13:27:05.160 回答
1

看起来您正试图在主 UI 线程上运行网络请求。自 3.0 以来,Android 不允许您这样做(我相信)。这样做会导致您的 UI 锁定直到请求完成,从而在执行请求期间使您的应用程序无用。

您必须在 newThread或, 中运行您的请求ASyncTask,以承担 UI 线程的负载。您可以在此处找到有关如何使用多个线程的更多信息

于 2013-05-16T13:28:24.580 回答
1

你得到一个 NetworkOnMainThreadException。它被抛出是因为不允许在应用程序的主线程中进行网络操作。

首先点击谷歌关于这个异常:http: //developer.android.com/reference/android/os/NetworkOnMainThreadException.html

谷歌上关于这个异常的第二次点击:如何修复 android.os.NetworkOnMainThreadException?

于 2013-05-16T13:15:09.747 回答
0

这是一个解决方法,

    StrictMode.ThreadPolicy ourPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(ourPolicy);

在 onCreate() 方法中添加它

于 2013-05-17T05:18:31.680 回答