简体   繁体   English

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

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

I have a MediaPlayer object and a spinner with all the songs located on the sd card. 我有一个MediaPlayer对象和一个微调器,所有歌曲都位于sd卡上。 I'm trying to create the codes of each Play, Pause, Stop, Previous and Next buttons. 我正在尝试创建每个“播放”,“暂停”,“停止”,“上一个”和“下一个”按钮的代码。

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. 当从微调器中选择一项时,我正在从中获取MediaPlayer,并设置其数据源并调用prepare方法。 Here's the code: 这是代码:

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

And here's the code for each Stop and Next buttons: 这是每个“停止”和“下一步”按钮的代码:

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

What's is happening is that when nextSong() is called, everything is working fine and the toast in onItemSelected() is shown, but when stopSong() is called, onItemSelected() is not being executed and the toast is not shown, the song is being stopped but when I click play button again, i get an exception: start called in state 1, error (-38, 0), it's because the mediaPlayer is reset and not prepared again. 发生的是,当调用nextSong()时,一切正常,并且显示了onItemSelected()中的吐司,但是当调用stopSong()时,onItemSelected()未执行且吐司未显示,这首歌正在停止,但是当我再次单击“播放”按钮时,出现一个异常:在状态1中开始调用,错误(-38,0),这是因为mediaPlayer已重置并且没有再次准备。

Thanks in advance :) 提前致谢 :)

I have solved the issue: If I'm selecting an item from the spinner that is already selected, spinner.setSelection(index, false); 我已经解决了这个问题:如果我要从已经选择的微调器中选择一个项目,则spinner.setSelection(index, false); won't call onItemSelected again, it's only called if the index is changed. 不会再次调用onItemSelected ,仅在更改index时才调用。 So i did the following: 所以我做了以下事情:

I created a string variable and set its value to dataSource : 我创建了一个字符串变量,并将其值设置为dataSource

private String dataSource;

in onItemSelected , I added this: onItemSelected ,我添加了以下内容:

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

So stopSong() method became: 因此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);

try this 尝试这个

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

相关问题 Android Spinner.setSelection(int,false)创建混乱的Spinners - Android Spinner.setSelection(int, false) creates messed up Spinners Android Spinner.setSelection()不起作用 - Android Spinner.setSelection() doesn't work spinner.setSelection 帮助 - spinner.setSelection help Spinner.SetSelection(0)重置Xamarin.Android中的第二个Spinner - Spinner.SetSelection(0) resets second Spinner in Xamarin.Android spinner.setSelection 不调用 setOnItemSelectedListener 函数 - spinner.setSelection not calling setOnItemSelectedListener functions Sqlite数据库android中Spinner setselection(position)中的问题 - Issues in Spinner setselection(position) with Sqlite Database android Spinner.setSelection不会正确触发OnItemSelectedListener - Spinner.setSelection doesn't trigger OnItemSelectedListener properly Jetpack Compose:在 DropDownMenu 中模仿 spinner.setSelection() - Jetpack Compose: Mimicking spinner.setSelection() inside of a DropDownMenu Android Spinner 视图:setSelection (int position) 和 setSelection (int position, boolean animate) 之间的区别? - Android Spinner view: difference between setSelection (int position) and setSelection (int position, boolean animate)? Android:Spinner setSelection by Tag - Android: Spinner setSelection by Tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM