0

我有一个MediaPlayer对象和一个微调器,所有歌曲都位于 SD 卡上。我正在尝试创建每个播放、暂停、停止、上一个和下一个按钮的代码。

When an item from the spinner is being selected, i'm getting the MediaPlayer from it, and set its data source and calling prepare method. 这是代码:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            Context context = getApplicationContext();
            CharSequence text = "onItemSelected";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(sdcard_playlist.get(arg2));
                applyValuesToEqualizer();
                mediaPlayer.prepare();
                index = arg2;



            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
            } catch (SecurityException e1) {
                e1.printStackTrace();
            } catch (IllegalStateException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

这是每个停止和下一步按钮的代码:

public void stopSong(View view) {
    if (isPlaying) {
    mediaPlayer.reset();

    isPlaying = false;
    spinner.setSelection(index, false);  // index is the index of the chosen item from spinner
    seekbar.setProgress(0);
    } 
}

public void nextSong(View view) {
    if (isPlaying) {
        mediaPlayer.reset();
        isPlaying = false;
        spinner.setSelection(index + 1, false);
        seekbar.setProgress(0);
        playPauseSong(findViewById(R.id.pause_music_button));
    } else {
        spinner.setSelection(index + 1, false);
        seekbar.setProgress(0);
    }
}

发生的事情是,当调用 nextSong() 时,一切正常,并且显示了 onItemSelected() 中的 toast,但是当调用 stopSong() 时,没有执行 onItemSelected() 并且未显示 toast,这首歌正在停止,但是当我再次单击播放按钮时,出现异常:在状态 1 中调用开始,错误 (-38, 0),这是因为 mediaPlayer 已重置并且没有再次准备好。

提前致谢 :)

4

2 回答 2

1

我已经解决了这个问题:如果我从已选择的微调器中选择一个项目,spinner.setSelection(index, false);则不会onItemSelected再次调用,只有在index更改时才会调用它。所以我做了以下事情:

我创建了一个字符串变量并将其值设置为dataSource

private String dataSource;

onItemSelected,我添加了这个:

mediaPlayer.reset();
dataSource = sdcard_playlist.get(arg2);
mediaPlayer.setDataSource(dataSource);
applyValuesToEqualizer();
mediaPlayer.prepare();
index = arg2;

所以stopSong()方法变成了:

public void stopSong(View view) {
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(dataSource);
            applyValuesToEqualizer();
            mediaPlayer.prepare();
        } catch (IOException e) {
            Log.e(TAG, "Error in stopSong() method");
        }
        isPlaying = false;

        seekbar.setProgress(0);
    } 
}
于 2013-09-04T09:05:34.163 回答
0
new Handler().postDelayed(new Runnable() {        
                public void run() {
                    spinner.setSelection(index, false);
                }
              }, 100);

尝试这个

于 2013-09-03T14:48:15.577 回答