在Xamarin Android中播放铃声一分钟

如何解决在Xamarin Android中播放铃声一分钟

我的警报项目快要完成了,我有代码可以每秒比较当前时间和目标时间,以查看是否为true,并在警报到期时播放铃声。我的问题是我似乎无法在指定的时间内播放铃声然后停止 这是我的源文件...

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

铃声可以永远播放,谢谢。我需要一些代码才能签入并管理播放时间。现在可以使用了...

解决方法

您可以使用TimerTaskTimer安排指定的时间来做某事。

例如,每秒创建一个 TimerTask 来计数并停止计时器任务,直到60秒。

class MyTask : TimerTask
{
    Timer mTimer;
    Ringtone ringtone;
    MainActivity mainActivity;

    public MyTask(Timer timer,Ringtone r,MainActivity mainActivity)
    {
        this.mTimer = timer;
        this.ringtone = r;
        this.mainActivity = mainActivity;
    }
    int i;
    public override void Run()
    {
        if (i < 60)
        {
            i++;
        }
        else
        {
            mTimer.Cancel();
            ringtone.Stop();
            mainActivity.StartActivity(typeof(NextActivity));
        }

           
    }
}

然后在 Activity.cs 中,我们可以在方法中调用Ringtone.Play(),例如单击按钮的方法:

private void Button_Click(object sender,EventArgs e)
{
    Timer timer = new Timer();
    timer.Schedule(new MyTask(timer,r,this),1000);
    r.Play();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?