如何停止通过切换按钮之间切换播放声音? [英] How do I stop playing sound by switching between toggle button?

查看:508
本文介绍了如何停止通过切换按钮之间切换播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个闹钟还有我使用了一个拨动开关,开始我报警。我想只要在按钮上的用户再次点击,就应该停止报警(环)。 ON和OFF之间切换启动西港岛线分别停止。

但是现在,我只能开始我报警。事实上,它永远不会停止。如果我关闭声音得到更快的起的按钮,这是非常奇怪的。我怎样才能阻止它?
我已经贴code,其中我假设发生问题的唯一部分。如果你需要看我的整个应用程序的code,我将它张贴。
[注:一切工作正常]

MainActivity:

 包com.mycompany.alarmclock;
//我没有在这里发表进口的东西,他们在我的文件
进口android.support.v7.app.ActionBarActivity;
进口的java.util.Calendar;
公共类MainActivity延伸活动{    AlarmManager alarmManager;
    私人的PendingIntent的PendingIntent; //动作被其它/外国申请执行
    私人TimePicker alarmTimePicker; //在用户设置的时间
    私有静态MainActivity研究所;
    私人TextView的alarmTextView; //其中报警信息/通知将显示区域
    公共静态MainActivity实例(){
        回到研究所;
    }    公共无效调用onStart(){
        super.onStart();
        研究所=这一点;
    }    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        alarmTimePicker =(TimePicker)findViewById(R.id.alarmTimePicker);
        alarmTextView =(的TextView)findViewById(R.id.alarmText);
        切换按钮alarmToggle =(切换按钮)findViewById(R.id.alarmToggle);
        alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
    }
    公共无效onToggleClicked(查看视图){
        如果(((切换按钮)视图).isChecked()){//如果切换按钮为ON做报警功能
            Log.d(MainActivity,报警开);
            台历挂历= Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE,alarmTimePicker.getCurrentMinute());
            意图myIntent =新意图(MainActivity.this,AlarmReceiver.class);
            的PendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,myIntent,0);
            alarmManager.set(AlarmManager.RTC,calendar.getTimeInMillis(),的PendingIntent);
//在这个else语句,我需要让闹钟点击切换按钮后停止添加逻辑
        }其他{//否则,如果切换按钮再次pssed $ P $,关闭闹钟/声音
            alarmManager.cancel(的PendingIntent);
            setAlarmText();
            Log.d(MyActivity,闹钟关);
        }    }    公共无效setAlarmText(字符串alarmText){
        alarmTextView.setText(alarmText);    }}

AlarmReceiver:

 包com.mycompany.alarmclock;
//我没有张贴了我这里进口的东西,他们是我的文件。
进口android.support.v4.content.WakefulBroadcastReceiver;进口java.net.URI中;
公共类AlarmReceiver扩展WakefulBroadcastReceiver {
 @覆盖
    公共无效的onReceive(最终上下文的背景下,意图意图){
        MainActivity研究所= MainActivity.instance();
        inst.setAlarmText(起来起来!);
        乌里alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);        如果(alarmUri == NULL){
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        铃声铃声= RingtoneManager.getRingtone(背景下,alarmUri);
        ringtone.play();        //这会发送通知消息
        组件名补偿=新的组件名(context.getPackageName()
                AlarmService.class.getName());
        startWakefulService(上下文,(intent.setComponent(化合物)));
        的setResult code(Activity.RESULT_OK);
    }
}


解决方案

在您的切换按钮关闭code可以添加行:

 乌里alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
铃声铃声= RingtoneManager.getRingtone(背景下,alarmUri);
ringtone.stop();

编辑:

看着你的code后,这可能并不适用于所有情况。如果你的活动被杀死,报警触发将没有活动开停铃声。甚至通过通知启动它,切换按钮将处于错误状态。当通知被触摸的铃声或许应该停止。

在报警火灾,它会调用AlarmReceiver,它调用AlarmService。该AlarmService是什么创造了通知。它指定将被调用时通报pssed $ P $的意图。那是你的主要活动中,AlarmActivity。

 的PendingIntent contentIntent = PendingIntent.getActivity(这一点,0,
            新的意图(这一点,AlarmActivity.class),0);

您可以通过额外的意图通知您的活动,该报警器处于振铃状态并关闭它,然后,或者只是设置切换到上让用户关闭它。

我不明白是completeWakefulIntent(意向),这将释放你的唤醒锁的电话,但可能与样本的问题。

修改

铃声的需要,以阻止它的原始实例。正确的方法是创建一个服务,但是这是参与此演示了一下。我建议建立一个静态引用,并访问它的静态方法。

AlarmReceiver:

 私有静态铃声mRingtone = NULL;
...
    mRingtone = RingtoneManager.getRingtone(背景下,alarmUri);
    mRingtone.play();
...
}    公共静态无效stopRingtone(){
        mRingtone.stop();
    }

AlarmActivity:

  AlarmReceiver.stopRingtone();
        alarmManager.cancel(的PendingIntent);
        setAlarmText();
        Log.d(MyActivity,闹钟关);

I have made an alarm clock and there I have used a toggle switch to start my alarm. What I want is as soon as the user click on the button again, it should stop the alarm (ring). Toggling between ON and OFF wil start and stop respectively.

But right now, I am only able to start my alarm. In fact it never gets stopped. If I switch the button OFF the sound gets played even faster, that is very strange. How can I stop it? I have pasted the only part of code where I am assuming the problem occurs. If you need to look at my whole app's code I will post it. [NOTE: everything else works fine]

MainActivity:

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

    }

}

AlarmReceiver:

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

解决方案

In your toggle button off code you can add the lines:

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

Edit:

After looking at your code, this may not work in all cases. If your activity is killed off and the alarm is fired there will be no activity open to stop the ringtone. Even starting it via the notification, the toggle button will be in the wrong state. The ringtone should probably be stopped when the notification is touched.

When the alarm fires, it calls the AlarmReceiver, which calls the AlarmService. The AlarmService is what is creating the notification. It specifies the Intent that will be called when the notification is pressed. That is your Main activity, the AlarmActivity.

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

You can pass extras in the intent to notify your activity that the alarm is in the ringing state and to dismiss it then, or just set your toggle to on and let the user dismiss it.

What I don't see is a call to completeWakefulIntent(intent) which will release your wake lock, but that may be an issue with the sample.

Edit

The Ringtone needs the original instance in order to stop it. The correct way would be to create a service, but that's a bit involved for this demo. I'd suggest creating a static reference and a static method to access it.

AlarmReceiver:

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

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

AlarmActivity:

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

这篇关于如何停止通过切换按钮之间切换播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆