【发布时间】:2020-10-14 17:26:43
【问题描述】:
我的闹钟项目几乎完成了,我有代码可以每秒比较当前时间和目标时间,看看是否属实,并在闹钟成熟时播放铃声。我的问题是我似乎无法在指定的时间段内播放铃声然后停止 这是我的源文件...
class SecondActivity : AppCompatActivity{
//System Timer defintion with interval of one second in class
System.Timers.Timer _stopringtone = new System.Timers.Timer(1000);
protected override void OnCreate(Bundle savedInstanceState){
//Defining path for Ringtone's default uri
path = RingtoneManager.GetDefaultUri(RingtoneType.Ringtone);
//Ringtone instance
r = RingtoneManager.GetRingtone(Application.Context, path);
//Timer elpase event handler
_stopringtone.Elapsed += _stopringtone_Elapsed;
//Timer disabled by default will be enabled by alarm maturity event
_stopringtone.Enabled = false;
}
//This event checks every second if the two time variables are equal and plays a ringtone alongside a vibration pattern
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
RunOnUiThread(() =>
{
//Code to check current time and target time defined here
//Logic to compare the two variables
if (span == span1){
//Event Elapse method returns true so alarm time has matured, play some music and vibration
//Vibration definition
Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
//Vibration pattern definition
long[] pattern = { 0, 2000, 2000 };
//Play vibration
vibrator.Vibrate(pattern,0);
//Play Ringtone Uri,This is where I need code to play the ringtone for interval 60 seconds
r.Play();
//Alarm matured so enable timer to count down 20 seconds and shut off music
_stopringtone.Enabled = true;
}
}
//CountDown Logic method
private void _stopringtone_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
RunOnUiThread(() =>
{
int a = 0;
int b = a++;
if (b == 10)
{
r.Stop();
}
});
}
}
铃声永远播放,我需要一些代码来签入和管理播放时间,谢谢。现在可以使用了...
【问题讨论】:
-
设置定时器并在铃声响起时停止铃声
-
精湛的逻辑杰森,我不敢相信我从来没有想过它,一个系统类计时器,就像用来触发警报事件的那个?
-
我试过这个委托,但是编译器报错说,
+=不能应用于Timer和Anonymous method类型的操作数,这是委托_stopringtone += delegate(object sender,System.Timers.ElapsedEventArgs e) { }; -
您将代理分配给
Elapsed事件,而不是Timer -
好吧,由于逆向工程,自定义逻辑不是一个很好的工程方法
标签: android xamarin intervals ringtone