user814154
user814154

Reputation: 67

How to loop sound in android

When press button first sound active. Then press that button again it will stop and second sound active My code is OK?

package com.Randomsentence;
    import java.util.Random;
    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class Randomsentence extends Activity {
      boolean showRandom = false;
      TextView txt;
      int time = 30;
      int random;
      public String[] myString;
      Button bt1;
      boolean check = false;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt=(TextView)findViewById(R.id.txt);
        bt1 = (Button)findViewById(R.id.bt1);
        Medaiplayer mp = new Medaiplayer();
        Mediaplayer mp2 = new Mediaplayer();
        bt1.setOnClickListener(new View.OnClickListener() {

          @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        showRandom = !showRandom;
                t = new Thread() {
                    public void run() {
                        try {
                            while(showRandom){
         mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1); 
         mp.setLooping(true);
         mp.start();
                mp2.reset();
                mp2.prepare();
                sleep(1000);
                handler.sendMessage(handler.obtainMessage());
                            }
                mp.reset();
                mp.prepare();
            mp2 = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile2);      
            mp2.setLooping(true);   
            mp2.start();

                }catch(Exception ex){
                    ex.printStackTrace();
                }
                    }
                };
                t.start();

                }

        });

  }

      // our handler
      Handler handler = new Handler() {
        public void handleMessage(Message msg) {//display each item in a single line
          {

              Random rgenerator = new Random();
              Resources res = getResources();
              myString = res.getStringArray(R.array.myArray);
              String q = myString[rgenerator.nextInt(myString.length)];
              txt.setText(q);

          }
        }
      };
    }

Upvotes: 4

Views: 16487

Answers (2)

Daniel Paiva
Daniel Paiva

Reputation: 11

Your code has several errors. Typos, cases,

ex.: Medaiplayer should be MediaPlayer

This alone would be enough to cause the error. Also, declaring your variables outside the method is a good idea.

Upvotes: 1

Kenny
Kenny

Reputation: 5542

Add the line:

mp.setLooping(true);

Then set false when you want to stop it looping.

Upvotes: 21

Related Questions