라디오재생이 되는 StreamAudio를 만들려고 하는데요. 오류가 나서 원인이 뭘까 생각해본 결과 Activity에서 똑같이 돌아서 그런거같아
쓰레드를 따로 구성하려는데요. 어떻게 해야할지 감이 안잡힙니다. 코드는 아래와 같습니다.
parsePlaylistFile()이 부분을 쓰레드를 통해 따로 구성하려고 하는데 어찌해야할지 조언부탁드립니다.
이코드로 실행횄을 시 디버그를 통해 돌려본 결과 HttpResponse httpResponse = httpClient.execute(getRequest);에서 죽어버립니다.
지금 logcat오류는 아래와 같이 뜨구요. 문제가 무엇인지 아시는 분 제발 알려주세요....ㅠㅜ
05-07 11:30:33.021: D/CLIPBOARD(8939): Hide Clipboard dialog at Starting input: finished by someone else... !
05-07 11:30:54.511: V/URI(8939): http://live.kboo.fm:8000/high.m3u
05-07 11:30:54.531: D/AndroidRuntime(8939): Shutting down VM
05-07 11:30:54.531: W/dalvikvm(8939): threadid=1: thread exiting with uncaught exception (group=0x40c401f8)
05-07 11:30:54.541: E/AndroidRuntime(8939): FATAL EXCEPTION: main
05-07 11:30:54.541: E/AndroidRuntime(8939): android.os.NetworkOnMainThreadException
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-07 11:30:54.541: E/AndroidRuntime(8939): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-07 11:30:54.541: E/AndroidRuntime(8939): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-07 11:30:54.541: E/AndroidRuntime(8939): at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-07 11:30:54.541: E/AndroidRuntime(8939): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-07 11:30:54.541: E/AndroidRuntime(8939): at com.example.streamaudio.MainActivity.parsePlaylistFile(MainActivity.java:91)
05-07 11:30:54.541: E/AndroidRuntime(8939): at com.example.streamaudio.MainActivity.onClick(MainActivity.java:71)
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.view.View.performClick(View.java:3620)
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.view.View$PerformClick.run(View.java:14322)
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.os.Handler.handleCallback(Handler.java:605)
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.os.Looper.loop(Looper.java:137)
05-07 11:30:54.541: E/AndroidRuntime(8939): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-07 11:30:54.541: E/AndroidRuntime(8939): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:30:54.541: E/AndroidRuntime(8939): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:30:54.541: E/AndroidRuntime(8939): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
05-07 11:30:54.541: E/AndroidRuntime(8939): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
05-07 11:30:54.541: E/AndroidRuntime(8939): at dalvik.system.NativeStart.main(Native Method)
package com.example.streamaudio;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Vector;
import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener, OnCompletionListener, OnPreparedListener {
Vector<PlaylistFile> playlistItems; Button ParseButton; Button PlayButton; Button StopButton; EditText editTextUrl; String baseURL = ""; MediaPlayer mediaplayer;
int currentPlaylistItemNumber = 0;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
ParseButton = (Button) findViewById(R.id.parse); PlayButton = (Button) findViewById(R.id.play); StopButton = (Button) findViewById(R.id.stop); editTextUrl = (EditText) this.findViewById(R.id.edittexturl); editTextUrl.setText("http://live.kboo.fm:8000/high.m3u");
ParseButton.setOnClickListener(this); PlayButton.setOnClickListener(this); StopButton.setOnClickListener(this);
PlayButton.setEnabled(false); StopButton.setEnabled(false);
mediaplayer = new MediaPlayer(); mediaplayer.setOnCompletionListener(this); mediaplayer.setOnPreparedListener(this);
}
public void onClick(View view) { // TODO Auto-generated method stub if (view == ParseButton) { parsePlaylistFile(); } else if (view == PlayButton) { playPlaylistItem(); } else if (view == StopButton) { stop(); }
}
private void parsePlaylistFile() { // TODO Auto-generated method stub playlistItems = new Vector<PlaylistFile>();
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(editTextUrl.getText().toString());
Log.v("URI", getRequest.getURI().toString());
try { HttpResponse httpResponse = httpClient.execute(getRequest); if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { Log.v("HTTP ERROR", httpResponse.getStatusLine() .getReasonPhrase()); } else { InputStream inputStream = httpResponse.getEntity().getContent(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line; while ((line = bufferedReader.readLine()) != null) { Log.v("PLAYLISTLINE", "ORIG: " + line);
if (line.startsWith("#")) {
} else if (line.length() > 0) { String filepath = "";
if (line.startsWith("http://")) { filepath = line; } else { filepath = getRequest.getURI().resolve(line) .toString(); } PlaylistFile playlistFile = new PlaylistFile(filepath); playlistItems.add(playlistFile); } } inputStream.close(); } } catch (ClientProtocolException e) { // TODO: handle exception e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } PlayButton.setEnabled(true);
}
private void playPlaylistItem() { // TODO Auto-generated method stub PlayButton.setEnabled(false); currentPlaylistItemNumber = 0; if (playlistItems.size() > 0) { String path = ((PlaylistFile) playlistItems .get(currentPlaylistItemNumber)).getFilePath(); try { mediaplayer.setDataSource(path); mediaplayer.prepareAsync(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); // TODO: handle exception } }
}
public void onPrepared(MediaPlayer _mediaPlayer) { // TODO Auto-generated method stub StopButton.setEnabled(true); Log.v("HTTPAUDIOPLAYLIST", "Playing"); mediaplayer.start();
}
public void onCompletion(MediaPlayer mediaPlayer) { // TODO Auto-generated method stub Log.v("ONCOMPLETE", "called"); mediaplayer.stop(); mediaplayer.reset(); if (playlistItems.size() > currentPlaylistItemNumber + 1) { currentPlaylistItemNumber++; String path = ((PlaylistFile) playlistItems .get(currentPlaylistItemNumber)).getFilePath(); try { mediaplayer.setDataSource(path); mediaplayer.prepareAsync(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); // TODO: handle exception } }
}
private void stop() { // TODO Auto-generated method stub mediaplayer.pause(); PlayButton.setEnabled(true); StopButton.setEnabled(false);
} public class PlaylistFile { String filePath;
public PlaylistFile(String _filePath) { filePath = _filePath; }
public void setFilePath(String _filePath) { filePath = _filePath; }
public String getFilePath() { return filePath; } }
}