该程序是一个简单的音乐播放器,但只要按下该按钮,下一个按钮就会关闭该应用程序。
public void next(View view) {
Toast.makeText(getApplicationContext(), "Next song",
Toast.LENGTH_SHORT).show();
mp.reset();
if(SONG_NUMBER < (SONG_NUMBER - 1)){
mp.selectTrack(SONG_NUMBER + 1);
SONG_NUMBER = SONG_NUMBER + 1;
}
else {
mp.start();
SONG_NUMBER = 0;
}
mp.start();
}
这是代码的其余部分
package com.android.SimplePlayer;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.example.SimplePlayer.R;
import com.example.SimplePlayer.R.id;
import com.example.SimplePlayer.R.layout;
import com.example.SimplePlayer.R.string;
import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
public class MainActivity extends ListActivity implements OnCompletionListener {
private static final String SD_PATH = new String(Environment.getExternalStorageDirectory().getPath() + "/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private View play;
private View pause;
private View stop;
private View next;
public static int SONG_NUMBER=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updatePlaylist();
play = (ImageButton)findViewById(R.id.imageButton1);
pause = (ImageButton)findViewById(R.id.imageButton2);
stop = (ImageButton)findViewById(R.id.imageButton3);
next = (ImageButton)findViewById(R.id.imageButton4);
mp.setOnCompletionListener(this);
play.setEnabled(false);
pause.setEnabled(false);
stop.setEnabled(false);
next.setEnabled(true);
}
@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
try {
SONG_NUMBER=position;
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
next.setEnabled(true);
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private void updatePlaylist() {
File home = new File(SD_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.song_item,songs);
setListAdapter(songList);
}
}
public void play(View view){
Toast.makeText(getApplicationContext(), "Playing song",
Toast.LENGTH_SHORT).show();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
next.setEnabled(true);
}
public void pause(View view){
Toast.makeText(getApplicationContext(), "Pausing song",
Toast.LENGTH_SHORT).show();
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
next.setEnabled(true);
}
public void stop(View view){
Toast.makeText(getApplicationContext(), "Stopping song",
Toast.LENGTH_SHORT).show();
mp.stop();
play.setEnabled(false);
pause.setEnabled(false);
stop.setEnabled(false);
}
public void next(View view) {
Toast.makeText(getApplicationContext(), "Next song",
Toast.LENGTH_SHORT).show();
mp.reset();
if(SONG_NUMBER < (SONG_NUMBER - 1)){
mp.selectTrack(SONG_NUMBER + 1);
SONG_NUMBER = SONG_NUMBER + 1;
}
else {
mp.start();
SONG_NUMBER = 0;
}
mp.start();
}
@Override
public void onCompletion(MediaPlayer mp1) {
try {
mp.reset();
mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER + 1));
if(SONG_NUMBER < (SONG_NUMBER - 1)){
SONG_NUMBER = SONG_NUMBER + 1;
}
else {
mp.start();
SONG_NUMBER = 0;
}
mp.prepare();
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
next.setEnabled(true);
}
catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
任何人都知道如何解决这个问题?我会非常感激。
以下是我的logcat错误
10-19 01:32:16.428: E/MediaPlayer(7511): start called in state 1
10-19 01:32:16.428: E/MediaPlayer(7511): error (-38, 0)
10-19 01:32:16.428: E/MediaPlayer(7511): start called in state 0
10-19 01:32:16.461: E/MediaPlayer(7511): Error (-38,0)
10-19 01:32:16.473: E/MediaPlayer(7511): start called in state 2
10-19 01:32:16.473: E/MediaPlayer(7511): error (-38, 0)
10-19 01:32:16.474: E/MediaPlayer(7511): prepareAsync called in state 0
10-19 01:32:16.491: D/AndroidRuntime(7511): Shutting down VM
10-19 01:32:16.491: W/dalvikvm(7511): threadid=1: thread exiting with uncaught exception (group=0x41ff39a8)
10-19 01:32:16.534: E/AndroidRuntime(7511): FATAL EXCEPTION: main
10-19 01:32:16.534: E/AndroidRuntime(7511): java.lang.IllegalStateException
10-19 01:32:16.534: E/AndroidRuntime(7511): at android.media.MediaPlayer.prepare(Native Method)
10-19 01:32:16.534: E/AndroidRuntime(7511): at com.android.SimplePlayer.MainActivity.onCompletion(MainActivity.java:148)
10-19 01:32:16.534: E/AndroidRuntime(7511): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2044)
10-19 01:32:16.534: E/AndroidRuntime(7511): at android.os.Handler.dispatchMessage(Handler.java:107)
10-19 01:32:16.534: E/AndroidRuntime(7511): at android.os.Looper.loop(Looper.java:194)
10-19 01:32:16.534: E/AndroidRuntime(7511): at android.app.ActivityThread.main(ActivityThread.java:5400)
10-19 01:32:16.534: E/AndroidRuntime(7511): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 01:32:16.534: E/AndroidRuntime(7511): at java.lang.reflect.Method.invoke(Method.java:525)
10-19 01:32:16.534: E/AndroidRuntime(7511): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
10-19 01:32:16.534: E/AndroidRuntime(7511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
10-19 01:32:16.534: E/AndroidRuntime(7511): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
if(SONG_NUMBER < (SONG_NUMBER - 1)){
mp.selectTrack(SONG_NUMBER + 1);
SONG_NUMBER = SONG_NUMBER + 1;
} else {
mp.start();
SONG_NUMBER = 0;
}
嘿,在下一次点击时,它总是进入其他部分,因为该条件永远不会满足。并且你已经添加了mp.reset()以及稍后你正在尝试mp.start()你必须在启动音乐之前将数据源添加到mp。
答案 1 :(得分:0)
Yout逻辑是错误的。 (SONG_NUMBER < (SONG_NUMBER - 1))
永远不会是true
。
所以会发生两次调用mp.start()
,一个在else
子句中,一个在它之后。这就是异常说明start called in state...
您可能想要做的是:
if(SONG_NUMBER < songs.size()-1){
SONG_NUMBER += 1;
mp.selectTrack(SONG_NUMBER);
} else {
SONG_NUMBER = 0;
}
mp.start();
答案 2 :(得分:0)
public void prev(View view) {
Toast.makeText(getApplicationContext(), "Previous song",
Toast.LENGTH_SHORT).show();
try {
if(SONG_NUMBER < songs.size()+1){
SONG_NUMBER -= 1;
mp.reset();
mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER));
}
else {
Toast.makeText(getApplicationContext(), "Cannot play previous song",
Toast.LENGTH_SHORT).show();
prev.setEnabled(false);
}
mp.start();
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
根据下一个按钮,我尝试制作上一个按钮。除非你正在播放第一首歌,否则它会有效。我想要它,这样如果按下上一个按钮,prev.setEnabled()
会变为false,音乐会继续播放。我也想要它,以便说它在尝试时无法播放以前的歌曲。