简体   繁体   中英

Android Audio won't loop

Trying to get android audio to loop once button is hit. The audio file does in fact play just once; just doesn't loop at all. The following code doesn't work:

final Button b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mp = MediaPlayer.create(context, R.raw.sound4);
                mp.setLooping(true);
                try {
                    if (mp.isPlaying()) {
                        mp.stop();
                        mp.release();}

                } catch(Exception e) { e.printStackTrace(); }

                mp.start();



            }
        });

This also doesn't work

final Button b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mp = MediaPlayer.create(context, R.raw.sound4);
                mp.setLooping(true);
                mp.start();

        });

Basically all the advice I've been getting is just add .setLooping(true). But it just doesn't work. Please help!

try call mp.setLooping(true); after : mp.start();

Update

try set mP.prepare();

final Button b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mp = MediaPlayer.create(context, R.raw.sound4);
            mp.prepare();
            mp.start();
            mp.setLooping(true);


    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM