向媒体播放器添加重复按钮

时间:2019-03-20 18:19:19

标签: android android-mediaplayer media-player media

我想向我的媒体播放器添加一个重复按钮(onClick)。我有播放,暂停和停止按钮,如下面的代码所示。我如何添加“重复”按钮以使音乐在Media Player中播放,并通过从用户单击的“重复”按钮来像循环一样进行重复。 这是我的媒体播放器代码:

public class Musica extends AppCompatActivity implements Runnable {
private Button pause;
private Button stop;
private SeekBar mseek;
private MediaPlayer mp;
private Thread soundThread;
private Button play;

//list
AdRequest adRequest;
private AdView adView;

ListView listm;

String[] itemname = {
        "music 1",
        "music 2",
        "music 3",
        "music 4",
        "music 5",
        "music 6",
        "Lullaby 1",
        "Lullaby 2",
        "Lullaby 3",
        "Lullaby 4",
        "Lullaby 5",

};


Integer[] imgid = {
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,
        R.drawable.musicon,

};


@Override
protected void onDestroy() {
    super.onDestroy();
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_musica);

    getSupportActionBar().hide();



    adView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .build();
    adView.loadAd(adRequest);

    adView.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
            adView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int error) {
            adView.setVisibility(View.GONE);
        }

    });


    CustomListAdapterMusic adapter = new CustomListAdapterMusic(this, itemname, imgid);
    listm = (ListView) findViewById(R.id.listmusic);
    listm.setAdapter(adapter);

    listm.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // TODO Auto-generated method stub

            String Slecteditem = itemname[+position];
            Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();


            if (position == 0) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.babyone);
                mp.start();
            }


            if (position == 1) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.babytwo);
                mp.start();
            }


            if (position == 2) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.water);
                mp.start();
            }


            if (position == 3) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.ocean);
                mp.start();
            }


            if (position == 4) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.rain);
                mp.start();
            }


            if (position == 5) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.sm);
                mp.start();
            }
            if (position == 6) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.classica);
                mp.start();
            }


            if (position == 7) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.relax6);
                mp.start();
            }

            if (position == 8) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.twinkle7);
                mp.start();
            }

            if (position == 9) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.ninar11);
                mp.start();
            }

            if (position == 10) {

                stopPlaying();

                mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.lullaby9);
                mp.start();
            }



            play = (Button) findViewById(R.id.bplay);
            pause = (Button) findViewById(R.id.bpause);
            stop = (Button) findViewById(R.id.bstop);
            mseek = (SeekBar) findViewById(R.id.seekBar);


            setupListeners();
            soundThread = new Thread(Musica.this);
            soundThread.start();


        }


    });


}

private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
}


private void setupListeners()

{


    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp.start();

        }
    });


    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp.pause();

        }
    });


    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View currentView) {
            mp.stop();

        }
    });


    mseek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser) {
                mp.seekTo(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


}


@Override
public void run() {
    int currentPosition = 0;
    int soundTotal = mp.getDuration();
    mseek.setMax(soundTotal);


    while (mp != null && currentPosition < soundTotal) {

        try {
            Thread.sleep(300);
            currentPosition = mp.getCurrentPosition();
        } catch (InterruptedException SoundException) {
            return;

        } catch (Exception otherException) {
            return;

        }
        mseek.setProgress(currentPosition);


    }


}

}

1 个答案:

答案 0 :(得分:0)

下面的代码会有所帮助

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        if(is_repeat_enable){
            mp.start(); //for repeat only current media file
            // or play first media file
        }
    }
});