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

NetworkOnMainThreadException が発生します。アプリケーションのメインスレッドでネットワーク操作を行うことが許可されていないため、スローされます。

この例外について Google で最初にヒット: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

この例外に関する Google での 2 番目のヒット: How to fix 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 に答える