如何以设定的间隔将声音循环不同的次数 - android

时间:2015-08-21 23:07:37

标签: android audio android-mediaplayer

我过去曾做过一些关于android的事情,但这是我第一次触及它超过一年而且从昨天起就一直困在这个问题上。

我正在与某人合作开展项目,我需要按照设定的时间间隔播放一定次数的声音。 (例如,一分钟后播放一次,两分钟后播放两次,三分钟后播放三次,然后继续播放)。我可以在任何时间间隔播放声音,这不是问题,但我无法弄清楚如何让它在每个间隔播放正确的次数。它或者最终无限循环,每次间隔时间播放一次或播放一次并停止播放。

尝试TimerTask,切换到Handler / Runnable,尝试使用for循环并使用if语句与计数器。经过两次多次尝试,几小时的研究和有限的经验,这是我遇到的一个问题,我无法弄清楚。

这是我目前为此特定功能获得的代码。我遇到了Runnable tenMinChime和OnCompletionListener chimeCompletion的问题。任何指导都非常感谢。

public class MainActivity extends AppCompatActivity {

TextView chimeOn, chimeOff;
Handler chimeHandler = new Handler();
MediaPlayer.OnCompletionListener chimeCompletion;
MediaPlayer cp;
int chimeCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (screen == 1) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    setContentView(R.layout.activity_main);
    chimeOn = (TextView) findViewById(R.id.chimeOn);
    chimeOff = (TextView) findViewById(R.id.chimeOff);
    chimeOn.setTextColor(0xFFbebebe);
    chimeOff.setTextColor(0xFF000000);

    cp = MediaPlayer.create(this, R.raw.placeholder_chime);

    chimeOn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (chime == 0) {
                chimeOn.setTextColor(0xFF000000);
                chimeOff.setTextColor(0xFFbebebe);
                handler2.postDelayed(tenMinChime, 5000);
                chime = 1;
            }
        }
    });

    chimeOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (chime == 1) {
                chimeOff.setTextColor(0xFF000000);
                chimeOn.setTextColor(0xFFbebebe);
                handler2.removeCallbacks(tenMinChime);
                chime = 0;
            }
        }
    });

    chimeCompletion = new MediaPlayer.OnCompletionListener() {
        int count = 0;
        int maxCount = chimeCount;
        @Override
        public void onCompletion(MediaPlayer mp) {
            if(count < maxCount) {
                count++;
                cp.seekTo(0);
                cp.start();
                cp.setOnCompletionListener(chimeCompletion);
            }
        }
    };

public Runnable tenMinChime = new Runnable() {
    public void run() {
        chimeCount+=1;
        cp.start();
        cp.setOnCompletionListener(chimeCompletion);
    }
};

1 个答案:

答案 0 :(得分:0)

也许这可以解决你的问题?

 public class MainActivity extends AppCompatActivity {

    TextView chimeOn, chimeOff;
    Handler handler2 = new Handler();
    MediaPlayer.OnCompletionListener chimeCompletion;
    MediaPlayer cp;
    int chimeCount = 0;
    int mPlayCount=0;
    int mMaxPlayCount=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (screen == 1) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }

        setContentView(R.layout.activity_main);
        chimeOn = (TextView) findViewById(R.id.chimeOn);
        chimeOff = (TextView) findViewById(R.id.chimeOff);
        chimeOn.setTextColor(0xFFbebebe);
        chimeOff.setTextColor(0xFF000000);

        cp = MediaPlayer.create(this, R.raw.placeholder_chime);

        chimeOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chime == 0) {
                    chimeOn.setTextColor(0xFF000000);
                    chimeOff.setTextColor(0xFFbebebe);
                    handler2.postDelayed(tenMinChime, 5000);
                    chime = 1;
                }
            }
        });

        chimeOff.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (chime == 1) {
                    chimeOff.setTextColor(0xFF000000);
                    chimeOn.setTextColor(0xFFbebebe);
                    handler2.removeCallbacks(tenMinChime);
                    cp.setOnCompletionListener(null);
                    chime = 0;
                    mMaxPlayCount=0;
                    mPlayCount=0;

                }
            }
        });

        chimeCompletion = new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if(mPlayCount < mMaxPlayCount) {
                    mPlayCount++;
                    cp.seekTo(0);
                    cp.start();
                }
            }
        };

    public Runnable tenMinChime = new Runnable() {
        public void run() {
            mPlayCount=0;
            mMaxPlayCount= 3;
            cp.setOnCompletionListener(chimeCompletion);
            cp.start();
        }
    };