繁体   English   中英

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

[英]android spinner.setSelection(position, false); is not executed

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

当从微调器中选择一项时,我正在从中获取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) {
        }
    });

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

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()中的吐司,但是当调用stopSong()时,onItemSelected()未执行且吐司未显示,这首歌正在停止,但是当我再次单击“播放”按钮时,出现一个异常:在状态1中开始调用,错误(-38,0),这是因为mediaPlayer已重置并且没有再次准备。

提前致谢 :)

我已经解决了这个问题:如果我要从已经选择的微调器中选择一个项目,则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);
    } 
}
new Handler().postDelayed(new Runnable() {        
                public void run() {
                    spinner.setSelection(index, false);
                }
              }, 100);

尝试这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM