如何循环的声音在安卓声音

2023-09-06 17:24:48 作者:世界不及你重要

在preSS按钮乍一听活跃。然后preSS该按钮再次将停止和第二声控我的code是OK?

 包com.Randomsentence;
    导入了java.util.Random;
    进口android.app.Activity;
    进口android.content.res.Resources;
    进口android.os.Bundle;
    进口android.os.Handler;
    进口android.os.Message;
    进口android.util.Log;
    进口android.view.View;
    进口android.widget.Button;
    进口android.widget.TextView;

    公共类Randomsentence延伸活动{
      布尔showRandom = FALSE;
      TextView的TXT;
      INT时间= 30;
      INT随机的;
      公众的String [] MyString中;
      按钮BT1;
      布尔查= FALSE;

      @覆盖
      公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        TXT =(TextView中)findViewById(R.id.txt);
        BT1 =(按钮)findViewById(R.id.bt1);
        Medaiplayer MP =新Medaiplayer();
        媒体播放器MP2 =新的媒体播放器();
        bt1.setOnClickListener(新View.OnClickListener(){

          @覆盖
      公共无效的onClick(视图v){
        // TODO自动生成方法存根
        !showRandom = showRandom;
                T =新的Thread(){
                    公共无效的run(){
                        尝试 {
                            而(showRandom){
         熔点= MediaPlayer.create(getApplicationContext(),R.raw.AudioFile1);
         mp.setLooping(真正的);
         mp.start();
                mp2.reset();
                。MP2 prepare();
                睡眠(1000);
                handler.sendMessage(handler.obtainMessage());
                            }
                mp.reset();
                MP prepare()。
            MP2 = MediaPlayer.create(getApplicationContext(),R.raw.AudioFile2);
            mp2.setLooping(真正的);
            mp2.start();

                }赶上(例外前){
                    ex.printStackTrace();
                }
                    }
                };
                t.start();

                }

        });

  }

      //我们的处理
      处理程序处理程序=新的处理程序(){
        公共无效的handleMessage(信息MSG){//在一行中显示每个项目
          {

              随机rgenerator =新的随机();
              资源RES = getResources();
              MyString的= res.getStringArray(R.array.myArray);
              串Q = myString的[rgenerator.nextInt(myString.length)];
              txt.setText(q)的;

          }
        }
      };
    }
 

解决方案

添加一行:

  mp.setLooping(真正的);
 
安卓系统的手机照相声音怎样关闭

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

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

          }
        }
      };
    }

解决方案

Add the line:

mp.setLooping(true);

Then set false when you want to stop it looping.