2013-05-16 39 views
0

這是我的媒體播放器的完整代碼,它的功能是從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; 

} 

} 

這是我的錯誤,應用程序啓動

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 

回答

1

不要MainThread操作網絡。

+0

這將有加這個看起來更好,如果是評論 –

1

看起來你正嘗試在主UI線程上運行網絡請求。 Android 3.0不允許你這樣做(我相信)。這樣做會導致您的用戶界面鎖定,直到請求完成,從而在執行請求期間使您的應用無用。

您將不得不在新的ThreadASyncTask中運行您的請求,以便加載UI線程。你可以找到關於如何使用多線程here.

0

這裏是一個解決方法的詳細信息,

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

在onCreate()方法