单击警报按钮

时间:2014-01-20 15:38:24

标签: android android-alarms

当我点击开始按钮警报开始时,当我点击停止时,它会停止但是当我再次点击开始时,警报将不会启动。请帮助我。这是代码:

public class alarm : Activity
{
    protected MediaPlayer _player;
    protected override void OnCreate (Bundle bundle)
    {
        _player = MediaPlayer.Create (this,Resource.Raw.police_alarm);
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.alarm);
        Button start = FindViewById<Button> (Resource.Id.Start_ALarm);
        start.Click += delegate {
            _player.Start ();


        };
        Button stop = FindViewById<Button> (Resource.Id.Stop_ALarm);
        stop.Click += delegate {
            _player.Stop ();
        };
        //  FindViewById<TextView> (Resource.Id.Start_ALarm).Click+=Start_alarm_click;
        //FindViewById<TextView> (Resource.Id.Stop_ALarm).Click += Stop_alarm_click;

        // Create your application here
    }
    //FindViewById<TextView> (Resource.Id.Start_ALarm).Click+=Start_alarm_click;
    public void Start_alarm_click(object sender, EventArgs eventArgs)
    {
        _player.Start ();

    } 
    public void Stop_alarm_click(object sender, EventArgs eventArgs)
    {
        _player.Stop ();
    }

}

2 个答案:

答案 0 :(得分:0)

来自documentation

  

一旦处于Stopped状态,直到prepare()才能开始播放   或者调用prepareAsync()来将MediaPlayer对象设置为   再次准备状态。

这表明你应该致电

_player.prepare();

然后尝试启动它。

答案 1 :(得分:0)

初始化MediaPlayer应该在onCreate方法之前完成。否则当你点击停止时,播放器对象变为null。因此你可以在声明的地方初始化它在这里代码 _player = MediaPlayer.Create(this,Resource.Raw.police_alarm);