2

曲が終わったときに、なぜ illegalstate 例外が発生するのですか? seekcontrol.setProgress(mediaPlayer.getCurrentPosition()); でこのエラーが発生しています。シークバーと何か関係がありますか、それとも他の何かが原因ですか?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.media3);

    Intent i1 = getIntent();
    kg1 = i1.getStringExtra("Kggg1");
    MixName1 = i1.getStringExtra("mixname1");
    MixReplace=MixName1.replace(".mp3", "  ");
    initViews();
}
private void initViews() {
    mediaPlayer = new MediaPlayer();
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    txtcrnt = (TextView) findViewById(R.id.txtcurnt);
    txtremain = (TextView) findViewById(R.id.txtremain);
    txtcrntbig = (TextView) findViewById(R.id.txtcrntbig);
    txttitle=(TextView) findViewById(R.id.title);
    btnpre = (ImageButton) findViewById(R.id.btnpre);
    btnnext = (ImageButton) findViewById(R.id.btnnext);
    btnplay = (ImageButton) findViewById(R.id.btnplay);
    btnrepeat=(ImageButton) findViewById(R.id.btnrepeat);
    btnback = (Button) findViewById(R.id.btnlout);
    btnplayer = (Button) findViewById(R.id.btnedit);
    btnback.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MyMixes)getParent()).goBack();
        }
    });

    btnplayer.setClickable(true);
    btnplayer.setEnabled(false);
    btnplayer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

    seekcontrol=(SeekBar) findViewById(R.id.seekbarcontrol);

    seekcontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser){
                mediaPlayer.seekTo(progress);
                seekcontrol.setProgress(progress);
            }

        }
    });


    btnplay.setOnClickListener(new OnClickListener() {

        public void onClick(View paramView) {

            btnClick();
        }
    });

    btnplay.setOnClickListener(new OnClickListener() {

        public void onClick(View paramView) {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                if (mediaPlayer != null) {
                    mediaPlayer.pause();
                    btnplay.setImageResource(R.drawable.play);
                }
            } else {
                if (mediaPlayer != null && mediaPlayer != null) {
                    mediaPlayer.start();
                    startPlayProgressUpdater();
                    btnplay.setImageResource(R.drawable.pause);
                }
            }
        }
    });

    btnnext.setOnClickListener(new OnClickListener() {

        public void onClick(View paramView) {

        }
    });

    btnpre.setOnClickListener(new OnClickListener() {

        public void onClick(View paramView) {



        }
    });

    seekvolume = (SeekBar) findViewById(R.id.seekbarvolume);

    new Thread()
    {
        public void run() 
        {
            while (isDownloading)
            {
                if (Data.filenames != null&& Data.filenames.size() >= (getIntent().getExtras().getInt("index") + 1) && Data.filenames.get(getIntent().getExtras().getInt("index")) != null) 
                {
                    try 
                    {
                        mediaPlayer.setDataSource(Data.filenames.get(getIntent().getExtras().getInt("index")));
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        startPlayProgressUpdater();
                        mediaPlayer.setOnCompletionListener(new OnCompletionListener() 
                        {
                            @Override
                            public void onCompletion(MediaPlayer mp) 
                            {
                                mediaPlayer.stop();
                                mediaPlayer.release();
                            }
                        });
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        isDownloading = false;
                    }

                    if (mediaPlayer != null)
                        seekcontrol.setMax(mediaPlayer.getDuration());

                    int maxvolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                    int curntvolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

                    seekvolume.setMax(maxvolume);
                    seekvolume.setProgress(curntvolume);

                    seekvolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
                    {

                        public void onStopTrackingTouch(SeekBar paramSeekBar) {
                        }

                        public void onStartTrackingTouch(SeekBar paramSeekBar) {
                        }

                        public void onProgressChanged(SeekBar paramSeekBar, int progress, boolean paramBoolean) 
                        {
                            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                        }
                    });
                    seekcontrol.setMax(mediaPlayer.getDuration());

                    seekcontrol.setOnTouchListener(new OnTouchListener() 
                    {
                        public boolean onTouch(View v, MotionEvent paramMotionEvent) 
                        {
                            seekChange(v);
                            return false;
                        }
                    });
                }
            }
        };
    }.start();
}

protected void btnClick() {
    i++;
    i = i % 2;
    startSong(i);
}
 private void startSong(int i) {
        if (i == 1) {
            System.out.println("11111" + i);
            btnplay.setBackgroundResource(R.drawable.pause);
            try {
                System.out.println("start try chech------");
                mediaPlayer.start();
                startPlayProgressUpdater();
            } catch (Exception e) {
                mediaPlayer.pause();
            }
        }
        if (i == 0) {
            btnplay.setBackgroundResource(R.drawable.play);
            mediaPlayer.pause();
            System.out.println("00000" + i);
        }
    }

public void startPlayProgressUpdater() {

    Log.e("position", mediaPlayer.getCurrentPosition()+"");
    seekcontrol.setProgress(mediaPlayer.getCurrentPosition());

    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();

                int curtimeplay = mediaPlayer.getCurrentPosition();
                int curntseconds = (int) (curtimeplay / 1000) % 60;
                int curntminutes = (int) ((curtimeplay / (1000 * 60)) % 60);
                txtcrnt.setText(curntminutes + ":" + curntseconds);
                txtcrntbig.setText(curntminutes + ":" + curntseconds);

                int duratplay = mediaPlayer.getDuration();
                int totaldurationseconds = (int) (duratplay / 1000) % 60;
                int totaldurationminutes = (int) ((duratplay / (1000 * 60)) % 60);
                int remianingtime = duratplay - curtimeplay;
                int remianingtimeseconds = (int) (remianingtime / 1000) % 60;
                int remianingtimeminutes = (int) ((remianingtime / (1000 * 60)) % 60);
                txtremain.setText(remianingtimeminutes + ":"
                        + remianingtimeseconds);
            }
        };
        handler.postDelayed(notification, 1000);
    } else {
        mediaPlayer.pause();

        btnplay.setBackgroundResource(R.drawable.play);

        seekcontrol.setProgress(mediaPlayer.getCurrentPosition());
    }

}

 protected void seekChange(View v) {
        if (mediaPlayer.isPlaying()) {
            SeekBar sb = (SeekBar) v;
            mediaPlayer.seekTo(sb.getProgress());
        }
    }

ログキャット

07-14 21:45:31.530: E/AndroidRuntime(13870): FATAL EXCEPTION: main
07-14 21:45:31.530: E/AndroidRuntime(13870): java.lang.IllegalStateException
07-14 21:45:31.530: E/AndroidRuntime(13870):    at android.media.MediaPlayer.getCurrentPosition(Native Method)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at com.nelitemusic.MediaPlay1.startPlayProgressUpdater(MediaPlay1.java:253)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at com.nelitemusic.MediaPlay1$9.run(MediaPlay1.java:259)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at android.os.Handler.handleCallback(Handler.java:587)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at android.os.Looper.loop(Looper.java:132)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at android.app.ActivityThread.main(ActivityThread.java:4126)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at java.lang.reflect.Method.invokeNative(Native Method)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at java.lang.reflect.Method.invoke(Method.java:491)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
07-14 21:45:31.530: E/AndroidRuntime(13870):    at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

1
seekcontrol.setProgress(mediaPlayer.getCurrentPosition());

内部と呼ばれ、内部startPlayProgressUpdater()と呼ばれinner threadます。SeekBarUIスレッドでのみ触れることができるため、を使用してHandler投稿する必要があります

seekcontrol.setProgress(mediaPlayer.getCurrentPosition());

UIスレッドキューに入れます。

于 2012-07-14T12:23:46.887 に答える