【问题标题】:How to loop sound in android如何在android中循环声音
【发布时间】:2011-06-26 02:33:55
【问题描述】:

当按下按钮第一次响起时。然后再次按下该按钮,它将停止并且第二个声音激活我的代码正常吗?

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);

          }
        }
      };
    }

【问题讨论】:

    标签: java android audio


    【解决方案1】:

    添加行:

    mp.setLooping(true);
    

    然后当你想停止循环时设置为 false。

    【讨论】:

    • 在调用 .create(...) 之后、调用 .start() 之前添加它
    • 看起来不错,它是否按照您的意愿运行?
    • 我还没有跑步,因为我现在没有 sumsung Galaxy。明天早上我可以运行它。
    【解决方案2】:

    您的代码有几个错误。错别字,案例,

    例如:Medaiplayer 应该是 MediaPlayer

    仅此一项就足以导致错误。此外,在方法之外声明变量也是一个好主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多