2016-08-04 32 views
0

我創建了一個鬧鐘應用程序,當您設置鬧鐘應用程序時,播放您選擇的聲音並給出一條消息,說明已設置鬧鐘的時間。鬧鐘應用程序無法正常工作

該proplem是,我從來沒有聲音的聲音。 我在這裏錯過了什麼?

編輯:對不起,這是我有什麼

這是在MainActivity:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ 
AlarmManager alarmManager; 
private PendingIntent pending_intent; 

private TimePicker alarmTimePicker; 
private static MainActivity inst; 
private TextView alarmTextView; 

private AlarmReceiver alarm; 
private Context context; 
Spinner spinner; 
int richard_quote = 0; 


@TargetApi(Build.VERSION_CODES.M) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker); 
    // alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    this.context = this; 

    //alarm = new AlarmReceiver(); 
    alarmTextView = (TextView) findViewById(R.id.alarmText); 

    final Intent myIntent = new Intent(this.context, AlarmReceiver.class); 

    // Get the alarm manager service 
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    // set the alarm to the time that you picked 
    final Calendar calendar = Calendar.getInstance(); 
    //calendar.add(Calendar.SECOND, 3); 
    alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker); 

    //spinner creation 
    Spinner spinner = (Spinner) findViewById(R.id.richard_spinner); 
    // Create an ArrayAdapter using the string array and a default spinner layout 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.dawkins_sounds, android.R.layout.simple_spinner_item); 
    // Specify the layout to use when the list of choices appears 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    // Apply the adapter to the spinner 
    spinner.setAdapter(adapter); 

    // Spinner click listener 
    spinner.setOnItemSelectedListener(this); 

    myIntent.putExtra("extra", "yes"); 
    myIntent.putExtra("quote id", String.valueOf(richard_quote)); 
    pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 






    Button start_alarm= (Button) findViewById(R.id.start_alarm); 
    start_alarm.setOnClickListener(new View.OnClickListener() { 
     @TargetApi(Build.VERSION_CODES.M) 

     @Override 
     public void onClick(View v) { 

      calendar.set(Calendar.HOUR, alarmTimePicker.getHour()); 
      calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute()); 
      calendar.set(Calendar.SECOND, 0); 
     // calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour()); 
     // calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute()); 

      final int hour = alarmTimePicker.getHour(); 
      final int minute = alarmTimePicker.getMinute();; 

      String minute_string = String.valueOf(minute); 
      String hour_string = String.valueOf(hour); 

      if (minute < 10) { 
       minute_string = "0" + String.valueOf(minute); 
      } 

      if (hour > 12) { 
       hour_string = String.valueOf(hour - 12) ; 
      } 


      // myIntent.putExtra("extra", "yes"); 
      // myIntent.putExtra("quote id", String.valueOf(richard_quote)); 
     // pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


      // alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending_intent); 

     // setAlarmText("Alarm ON "); 

      setAlarmText("Alarm set to " + hour_string + ":" + minute_string); 
     } 

    }); 

    Button stop_alarm= (Button) findViewById(R.id.stop_alarm); 
    stop_alarm.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      myIntent.putExtra("extra", "no"); 
      myIntent.putExtra("quote id", String.valueOf(richard_quote)); 
      sendBroadcast(myIntent); 

      alarmManager.cancel(pending_intent); 
      setAlarmText("Alarm canceled"); 

      //setAlarmText("ID is " + richard_quote); 
     } 
    }); 

} 

public void setAlarmText(String alarmText) { 
    alarmTextView.setText(alarmText); 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


@Override 
public void onStart() { 
    super.onStart(); 
    inst = this; 
} 


@Override 
public void onDestroy() { 
    super.onDestroy(); 

    Log.e("MyActivity", "on Destroy"); 
} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

    //Toast.makeText(parent.getContext(), "Spinner item 3!" + id, Toast.LENGTH_SHORT).show(); 
    richard_quote = (int) id; 
} 

@Override 
public void onNothingSelected(AdapterView<?> parent) { 
    // Another interface callback 

} 

}

這是AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    String state = intent.getExtras().getString("extra"); 
    Log.e("MyActivity", "In the receiver with " + state); 

    String richard_id = intent.getExtras().getString("quote id"); 
    Log.e("Goat quote is" , richard_id); 

    Intent serviceIntent = new Intent(context,RingtonePlayingService.class); 
    serviceIntent.putExtra("extra", state); 
    serviceIntent.putExtra("quote id", richard_id); 

    context.startService(serviceIntent); 
} 

}

這是RingtonrPlayingService

public class RingtonePlayingService extends Service { 

private boolean isRunning; 
private Context context; 
MediaPlayer mMediaPlayer; 
private int startId; 

@Override 
public IBinder onBind(Intent intent) 
{ 
    Log.e("MyActivity", "In the alarm service"); 
    return null; 
} 


@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 


    final NotificationManager mNM = (NotificationManager) 
      getSystemService(NOTIFICATION_SERVICE); 

    Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 

    Notification mNotify = new Notification.Builder(this) 
      .setContentTitle("alarm is talking" + "!") 
      .setContentText("Click me!") 
      .setSmallIcon(R.drawable.ic_action_call) 
      .setContentIntent(pIntent) 
      .setAutoCancel(true) 
      .build(); 

    String state = intent.getExtras().getString("extra"); 

    Log.e("what is going on here ", state); 

    assert state != null; 
    switch (state) { 
     case "no": 
      startId = 0; 
      break; 
     case "yes": 
      startId = 1; 
      break; 
     default: 
      startId = 0; 
      break; 
    } 

    // get richard's thing 
    String richard_id = intent.getExtras().getString("quote id"); 
    Log.e("Service: goatid is " , richard_id); 

    if(!this.isRunning && startId == 1) { 
     Log.e("if there was not sound ", " and you want start"); 

     assert richard_id != null; 
     if (richard_id.equals("0")) { 

      int min = 1; 
      int max = 2; 

      Random r = new Random(); 
      int random_number = r.nextInt(max - min + 1) + min; 
      Log.e("random number is ", String.valueOf(random_number)); 

      if (random_number == 1) { 
       mMediaPlayer = MediaPlayer.create(this, R.raw.alarm2); 
      } 
      else if (random_number == 2) { 
       mMediaPlayer = MediaPlayer.create(this, R.raw.alarm1); 
      } 

      else { 
       mMediaPlayer = MediaPlayer.create(this, R.raw.alarm2); 
      } 
     } 
     else if (richard_id.equals("1")) { 
      mMediaPlayer = MediaPlayer.create(this, R.raw.alarm2); 
     } 
     else if (richard_id.equals("2")) { 
      mMediaPlayer = MediaPlayer.create(this, R.raw.alarm1); 
     } 

     else { 
      mMediaPlayer = MediaPlayer.create(this, R.raw.alarm2; 
     } 


     mMediaPlayer.start(); 

     mNM.notify(0, mNotify); 

     this.isRunning = true; 
     this.startId = 0; 

    } 
    else if (!this.isRunning && startId == 0){ 
     Log.e("if there was not sound ", " and you want end"); 

     this.isRunning = false; 
     this.startId = 0; 

    } 

    else if (this.isRunning && startId == 1){ 
     Log.e("if there is sound ", " and you want start"); 

     this.isRunning = true; 
     this.startId = 0; 

    } 
    else { 
     Log.e("if there is sound ", " and you want end"); 

     mMediaPlayer.stop(); 
     mMediaPlayer.reset(); 

     this.isRunning = false; 
     this.startId = 0; 
    } 


    Log.e("MyActivity", "In the service"); 

    return START_NOT_STICKY; 
} 



@Override 
public void onDestroy() { 
    Log.e("JSLog", "on destroy called"); 
    super.onDestroy(); 

    this.isRunning = false; 
} 

}

+0

您的代碼不顯示任何內容,表明您保存了用戶選擇要播放的聲音。所以,這是直接的猜測。您的鬧鐘代碼可能有問題 - 但您根本沒有發佈。 – iheanyi

回答

0

你不能正確啓動的MediaPlayer。

通讀這一點,特別是在使用的MediaPlayer與服務部分:

https://developer.android.com/guide/topics/media/mediaplayer.html

本質上,步驟是

  1. 創建播放
  2. 準備播放器
  3. 開始播放

您跳過了第2步,並且第2步需要某些工作才能在服務發生播放時正常運行。您也不能正確管理MediaPlayer資源。

更多細節在這裏:

https://developer.android.com/reference/android/media/MediaPlayer.html

其中特別說(在頁面的頂部,剛剛過去的狀態圖)

一個MediaPlayer對象必須首先前進入準備狀態可以開始播放 。