Android:在固定时间执行方法

时间:2014-08-22 15:00:26

标签: java android eclipse if-statement dynamic

我正在创建一个必须在固定时间播放背景音乐的应用程序。到目前为止,我设法重叠声音没有问题,并设置了一个计时器,但现在我想在计时器开始计数后的正好10秒钟开始播放一个声音。 我正在尝试创建一个“if”来检查我的计时器textview的值并执行“start playing”命令,但我不知道如何使它动态检查值。这是解决我问题的正确方法吗?或者我应该使用其他方法吗?

这是我的代码:

import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements OnPreparedListener  {


private MediaPlayer media1 = new MediaPlayer();
private MediaPlayer media2 = new MediaPlayer();
private TextView timerValue;
private long  startTime = 0L;
private Handler customHandler = new Handler(); 

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     timerValue = (TextView) findViewById(R.id.timerValue);

     playaudio();
     Toast.makeText(getApplicationContext(), "Reproduciendo", Toast.LENGTH_LONG).show();
}

protected void playaudio() 
{
    try {
        media1.setDataSource("http://tauruszone.sytes.net/uno.mp3");
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    media1.prepareAsync();
    media1.setOnPreparedListener(this);

try {
    media2.setDataSource("http://tauruszone.sytes.net/dos.mp3");
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
media2.prepareAsync();
media2.setOnPreparedListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onPrepared(MediaPlayer mp) {
    // TODO Auto-generated method stub

    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
    startplaying();
}


private Runnable updateTimerThread = new Runnable() {
    public void run() {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;
        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs));
        customHandler.postDelayed(this, 0);
    }
};

protected void  startplaying() 
{
    media1.start();
// This is the code im struggling with:
    if(timerValue.getText().toString().equals(00:10)){
       media2.start();
       }
    }
}

感谢。

0 个答案:

没有答案