0

ここに私のコードがあります。アプリに meidaplayer があります。ユーザーが音楽を再生してからアクティビティを終了すると、すべて問題ありませんが、ユーザーが音楽を再生せずにアクティビティを終了すると、強制的に終了します。

どうしたの?

public class Elementary1Activity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
        private SeekBar seekBar;
        private ImageButton startMedia;
        private ImageButton pauseMedia;
        private MediaPlayer mp;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.elementary1);             

            TextView title1 = (TextView) findViewById(R.id.title1);
            TextView tx1 = (TextView) findViewById(R.id.textView1);
            Typeface font = Typeface.createFromAsset(getAssets(), "font.ttf");
            title1.setTypeface(font);
            tx1.setTypeface(font);

            AudioControl();    



        }



        public void AudioControl(){
            seekBar = (SeekBar) findViewById(R.id.seekBarnew2);
            startMedia = (ImageButton) findViewById(R.id.playbuttonnew2);
            pauseMedia = (ImageButton) findViewById(R.id.pausebuttonnew2);
            seekBar.setOnSeekBarChangeListener(this);
            startMedia.setOnClickListener(this);
            pauseMedia.setOnClickListener(this); 
        }



        public void run() {
            int currentPosition= 0;
            int total = mp.getDuration();
            while (mp!=null && currentPosition<total) {
                try {
                    Thread.sleep(1000);
                    currentPosition= mp.getCurrentPosition();
                } catch (InterruptedException e) {
                    return;
                } catch (Exception e) {
                    return;
                }            
                seekBar.setProgress(currentPosition);
            }
        }

        public void onClick(View v) {
            pauseMedia.setVisibility(View.VISIBLE);
            seekBar.setVisibility(View.VISIBLE);
            startMedia.setVisibility(View.INVISIBLE);
            if (v.equals(startMedia)) {
                if (mp != null && mp.isPlaying()) return;
                if(seekBar.getProgress() > 0) {
                    mp.start();
                    return;
                }
                mp = MediaPlayer.create(Elementary1Activity.this, R.raw.e1);
                mp.start();            

                seekBar.setProgress(0);
                seekBar.setMax(mp.getDuration());
                new Thread(this).start();
            }

            if (v.equals(pauseMedia) && mp!=null) {
                mp.pause();
                pauseMedia.setVisibility(View.INVISIBLE);
                startMedia.setVisibility(View.VISIBLE);

            }       

        }



        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser) mp.seekTo(progress);

        }

        public void onStop() {
               super.onStop();
               mp.stop();
            }
    }
4

1 に答える 1