android spinner.setSelection(position,false);没有执行

时间:2013-09-03 14:32:54

标签: android media-player spinner

我有一个MediaPlayer对象和一个旋转器,所有歌曲都位于SD卡上。 我正在尝试创建每个Play,Pause,Stop,Previous和Next按钮的代码。

当选择微调器中的项目时,我从中获取MediaPlayer,并设置其数据源并调用prepare方法。这是代码:

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) {
        }
    });

这是每个Stop和Next按钮的代码:

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已重置且未再次准备。

提前致谢:)

2 个答案:

答案 0 :(得分: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);
    } 
}

答案 1 :(得分:0)

new Handler().postDelayed(new Runnable() {        
                public void run() {
                    spinner.setSelection(index, false);
                }
              }, 100);

试试这个