2014-01-20 193 views
0

當我點擊開始按鈕報警開始時,當我點擊停止,它停止,但當我再次點擊開始時,報警將無法啓動。請幫助我。下面的代碼:報警按鈕點擊

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

} 

回答

0

documentation:在停止狀態

一次,播放不能啓動,直到準備() 或prepareAsync()被調用的MediaPlayer對象設置爲 再次準備狀態。

這表明在嘗試啓動它之前,你應該叫

_player.prepare(); 

0

初始化MediaPlayer應該在onCreate方法之前完成。否則當你點擊停止播放器對象變爲null時。因此你可以在聲明的地方初始化它,代碼如下: _player = MediaPlayer.Create(this ,Resource.Raw.police_alarm);