开发者

Using a thread with media player

开发者 https://www.devze.com 2023-04-03 02:00 出处:网络
I\'m trying to get this to work the way I want and it just won\'t I think I had it at some point but I was trying to fix something else and changed a few things and messed it up. Basically I\'m this b

I'm trying to get this to work the way I want and it just won't I think I had it at some point but I was trying to fix something else and changed a few things and messed it up. Basically I'm this button is a play button. It is supposed to run media player through another thread so that it reacts and buffers faster. Issue is after it plays through to the end and I click play it is ended and keeps running the onCompletion. When I would like it to start over and play from the beginning again.

public void onClick(View v) {

                if(playClick%2==0){
                    song1.setBackgroundResource(R.drawable.button_pause);

                    try{
                        if(playClick==0){
                            playThread();
                            if(lastPlayedExists){
                                lastPlayed.release();
                            }
                        }

                        mp.start();
                        mpExists = true;
                        playClick++;

                        lastPlayed = mp;
                        lastPlayedExists=true;


                        mp.setOnCompletionListener(new OnCompletionListener(){

                            @Override
                            public void onCompletion(MediaPlayer media) {
                                mp.reset();
                                song1.setBackgroundResource(R.drawable.button_play);

                            }

                        });
                    }catch (NullPointerException e){

                    }

                }
                else if (playClick%2==1){
                    song1.setBackgroundResource(R.drawable.button_play);
                    mp.pause();

                    playClick++;
                }

            }
        }
    });

And this is the thread for media player

private void playThread(){
    try{
        new Thread(new Runnable() {
            public void run() {

                try {
                    mp.setDataSource(song);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
           开发者_如何转开发         // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {

                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }).start();
    }catch(IllegalStateException e){

    }

}

I have tried it with the onCompletion having mp.reset(), mp.stop(), and mp.release() along with trying lastPlayed.release() all in different tests. As well as none of them being there and it just being the button change and the progress bar reset. If you know how to fix this and make it replay as it should I would greatly appreciate it. Or if you know how to make the thread stop and restart some how so that I can just make it recall and do the thread over again that would work too (atm when that has been tried it crashes because the thread has already been started). Thanks and I appreciate any input.


mp.setOnCompletionListener(new OnCompletionListener(){

                        @Override
                        public void onCompletion(MediaPlayer media) {
                            playClick=2;
                            song1.setBackgroundResource(R.drawable.button_play);

                        }

                    });
0

精彩评论

暂无评论...
验证码 换一张
取 消