MainActivity .java
public class MainActivity extends Activity implements OnClickListener {
final String SUGGESTED_URL = "http://www.vorbis.com/music/Epoq-Lepidoptera.ogg";
Button mPlayButton;
Button mPauseButton;
Button mSkipButton;
Button mRewindButton;
Button mStopButton;
Button mEjectButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPlayButton = (Button) findViewById(R.id.playbutton);
mPauseButton = (Button) findViewById(R.id.pausebutton);
mSkipButton = (Button) findViewById(R.id.skipbutton);
mRewindButton = (Button) findViewById(R.id.rewindbutton);
mStopButton = (Button) findViewById(R.id.stopbutton);
mEjectButton = (Button) findViewById(R.id.ejectbutton);
mPlayButton.setOnClickListener(this);
mPauseButton.setOnClickListener(this);
mSkipButton.setOnClickListener(this);
mRewindButton.setOnClickListener(this);
mStopButton.setOnClickListener(this);
mEjectButton.setOnClickListener(this);
}
public void onClick(View target) {
if (target == mPlayButton){
//startService(new Intent(MusicService.ACTION_PLAY));
Intent in = new Intent(MainActivity.this,MusicPlayer.class);
startService(in);
}else if (target == mPauseButton)
startService(new Intent(MusicService.ACTION_PAUSE));
else if (target == mSkipButton)
startService(new Intent(MusicService.ACTION_SKIP));
else if (target == mRewindButton)
startService(new Intent(MusicService.ACTION_REWIND));
else if (target == mStopButton)
startService(new Intent(MusicService.ACTION_STOP));
else if (target == mEjectButton) {
showUrlDialog();
}
}
void showUrlDialog() {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("Manual Input");
alertBuilder.setMessage("Enter a URL (must be http://)");
final EditText input = new EditText(this);
alertBuilder.setView(input);
input.setText(SUGGESTED_URL);
alertBuilder.setPositiveButton("Play!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {
Intent i = new Intent(MusicService.ACTION_URL);
Uri uri = Uri.parse(input.getText().toString());
i.setData(uri);
startService(i);
}
});
alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {}
}); alertBuilder.show();
}
MusicPlayerList .java
public class MusicPlayerList extends ListActivity {
private static final String MEDIA_PATH = new String("/sdcard/"); // ROOT 경로를 지정합니다.
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private int currentPosition = 0; // 재생할 곡의 위치입니다.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("실행C");
super.onCreate(savedInstanceState);
setContentView(R.layout.musiclistview);
System.out.println("실행D");
//updateSongList(); // SD카드로부터 파일 목록을 불러오는 역할을 합니다.
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.musiclistview, songs);
setListAdapter(songList);
}
}
// List 아이템을 클릭했을 때의 event를 처리합니다.
protected void onListItemClick(ListView l, View v, int position, long id) {
currentPosition = position;
playSong(MEDIA_PATH + songs.get(position));
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
Toast.makeText(this, "재생 : " + songPath, Toast.LENGTH_SHORT).show();
TextView status = (TextView)findViewById(R.id.playStatus);
status.setText("재생중 : " + songPath);
// 한 곡의 재생이 끝나면 다음 곡을 재생하도록 합니다.
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void nextSong() {
if (++currentPosition >= songs.size()) {
// 마지막 곡이 끝나면, 재생할 곡을 초기화합니다.
currentPosition = 0;
TextView status = (TextView)findViewById(R.id.playStatus);
status.setText("준비됨");
} else {
// 다음 곡을 재생합니다.
Toast.makeText(getApplicationContext(), "다음 곡을 재생합니다.", Toast.LENGTH_SHORT).show();
playSong(MEDIA_PATH + songs.get(currentPosition));
}
}
}
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.musicplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_title" >
<activity
android:name=".MainActivity"
android:label="@string/app_title"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter></intent-filter>
</activity>
<service
android:name=".MusicService"
android:exported="false" >
<intent-filter>
<action android:name="com.example.android.musicplayer.action.TOGGLE_PLAYBACK" />
<action android:name="com.example.android.musicplayer.action.PAUSE" />
<action android:name="com.example.android.musicplayer.action.SKIP" />
<action android:name="com.example.android.musicplayer.action.REWIND" />
<action android:name="com.example.android.musicplayer.action.STOP" />
</intent-filter>
<intent-filter>
<action android:name="com.example.android.musicplayer.action.URL" />
<data android:scheme="http" />
</intent-filter>
</service>
<receiver android:name=".MusicIntentReceiver" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<activity
android:name=".MusicPlayerList"
android:label="@string/app_title"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
</application>
</manifest>
로그쳇에서는
05-30 12:50:04.379: I/System.out(20366): 실행A
05-30 12:50:04.389: I/System.out(20366): 실행B
05-30 12:50:04.399: V/MediaPlayer(20366): constructor
05-30 12:50:04.399: V/MediaPlayer(20366): setListener
05-30 12:50:04.399: W/dalvikvm(20366): threadid=1: thread exiting with uncaught exception (group=0x4001d8a0)
05-30 12:50:04.409: E/AndroidRuntime(20366): FATAL EXCEPTION: main
05-30 12:50:04.409: E/AndroidRuntime(20366): java.lang.RuntimeException: Unable to instantiate service com.example.android.musicplayer.MusicPlayerList: java.lang.ClassCastException: com.example.android.musicplayer.MusicPlayerList
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2943)
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.app.ActivityThread.access$3300(ActivityThread.java:125)
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.os.Looper.loop(Looper.java:123)
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-30 12:50:04.409: E/AndroidRuntime(20366): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 12:50:04.409: E/AndroidRuntime(20366): at java.lang.reflect.Method.invoke(Method.java:521)
05-30 12:50:04.409: E/AndroidRuntime(20366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:869)
05-30 12:50:04.409: E/AndroidRuntime(20366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)
05-30 12:50:04.409: E/AndroidRuntime(20366): at dalvik.system.NativeStart.main(Native Method)
05-30 12:50:04.409: E/AndroidRuntime(20366): Caused by: java.lang.ClassCastException: com.example.android.musicplayer.MusicPlayerList
05-30 12:50:04.409: E/AndroidRuntime(20366): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2940)
05-30 12:50:04.409: E/AndroidRuntime(20366): ... 10 more
라고나오네요
빨간색 intent 소스를 실행하면 파란색 리스트뷰를 띄워주는 걸로넘어가야되지만...
넘어가지않고 이런에러만 뜨네요. 폰에서run 해도 예상치않게종료되엇다는말만나오네요
정확히는 다른버튼은 다잘작동되는데 play버튼만 누르면 에러뜨면서 프로그램종료가됩니다.




제가 난독증인지 질문 파악이 안됩니다.
빨간색 소스에서는 MusicPlayer 라는 서비스를 실행하는데 로그에는 MusicPlayerList 가 나와있고..
MusicPlayerList 는 엑티비티니까 startService 대신 startActivity 써보세요.