如何通过在切换按钮之间切换来停止播放声音?

How do I stop playing sound by switching between toggle button?

我制作了一个闹钟,并使用拨动开关启动闹钟。我想要的是一旦用户再次点击按钮,它就应该停止闹钟(响铃)。在 ON 和 OFF 之间切换将分别启动和停止。

但是现在,我只能启动闹钟。事实上,它永远不会停止。如果我关闭按钮,声音会播放得更快,这很奇怪。我怎样才能阻止它? 我已经粘贴了我假设出现问题的唯一部分代码。如果您需要查看我的整个应用程序代码,我会 post 它。 [注意:其他一切正常]

主要活动:

    package com.mycompany.alarmclock;
//I haven't post the imported stuff here, they are in my file
import android.support.v7.app.ActionBarActivity;
import java.util.Calendar;
public class MainActivity extends Activity {

    AlarmManager alarmManager;
    private PendingIntent pendingIntent;//an action to be performed by other/foreign application
    private TimePicker alarmTimePicker;//In where the user set the time
    private static MainActivity inst;
    private TextView alarmTextView;//the area where alarm message/notification will be displayed


    public static MainActivity instance() {
        return inst;
    }

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

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
        alarmTextView = (TextView) findViewById(R.id.alarmText);
        ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    }


    public void onToggleClicked(View view) {
        if (((ToggleButton) view).isChecked()) {//if toggle button is "ON" do the alarming function
            Log.d("MainActivity", "Alarm On");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
            Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
//In this else statement I need to add the logic so that the alarm clock stops after clicking the toggle button
        } else {//else if the toggle button is pressed again, switch off the alarm /sound
            alarmManager.cancel(pendingIntent);
            setAlarmText("");
            Log.d("MyActivity", "Alarm Off");
        }

    }

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

    }

}

报警接收器:

package com.mycompany.alarmclock;
//I haven't posted my imported stuff here, they are on my file.
import android.support.v4.content.WakefulBroadcastReceiver;

import java.net.URI;
public class AlarmReceiver extends WakefulBroadcastReceiver {
 @Override
    public void onReceive(final Context context, Intent intent) {
        MainActivity inst=MainActivity.instance();
        inst.setAlarmText("Get Up! Get up!");
        Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();

        //this will send a notification message
        ComponentName comp = new ComponentName(context.getPackageName(),
                AlarmService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

在您的切换按钮关闭代码中,您可以添加以下行:

Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.stop();

编辑:

查看您的代码后,这可能并非在所有情况下都有效。如果您的 activity 被关闭并且警报被触发,则不会打开 activity 来停止铃声。即使通过通知启动它,切换按钮也会处于错误状态。当通知被触摸时,铃声应该会停止。

当警报触发时,它会调用 AlarmReceiver,后者会调用 AlarmService。 AlarmService 是创建通知的对象。它指定按下通知时将调用的 Intent。那就是你的 Main activity,AlarmActivity.

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, AlarmActivity.class), 0);

您可以在 intent 中传递 extras 以通知您的 activity 闹钟处于响铃状态并随后将其关闭,或者将您的开关设置为打开并让用户关闭它。

我没有看到对 completeWakefulIntent(intent) 的调用,它会释放您的唤醒锁,但这可能是示例的问题。

编辑

铃声需要原始实例才能停止。正确的方法是创建一个服务,但是对于这个演示来说有点复杂。我建议创建一个静态引用和一个静态方法来访问它。

报警接收器:

    private static Ringtone mRingtone  = null;
...
    mRingtone  = RingtoneManager.getRingtone(context, alarmUri);
    mRingtone.play();
...
}

    public static void stopRingtone() {
        mRingtone.stop();
    }

警报活动:

        AlarmReceiver.stopRingtone();
        alarmManager.cancel(pendingIntent);
        setAlarmText("");
        Log.d("MyActivity", "Alarm Off");