三个单独的单选按钮呼叫不同的声音

时间:2016-07-06 19:47:18

标签: android radio-button android-mediaplayer

我也遇到了这个带有复选框功能的错误。没有产生错误,但功能显示我的代码未被访问或未正确读取。

以下是我要解决的具体代码段:

  public void onStart(){
        super.onStart();
        final RadioButton softLoud = (RadioButton) findViewById(R.id.low_to_high_radioButton);
        final RadioButton loudSoft = (RadioButton) findViewById(R.id.high_to_low_radioButton);
        final RadioButton verbalCommand = (RadioButton) findViewById(R.id.step_voice_radioButton);
        final MediaPlayer mediaPlayer = new MediaPlayer();
        if (verbalCommand.isChecked()){
            verbalCommand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                    }
                    try {
                        mediaPlayer.reset();
                        AssetFileDescriptor assetFileDescriptor;
                        //Change mp3 file using voice recorder when ready
                        assetFileDescriptor = getAssets().openFd("Noise.mp3");
                        mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                                assetFileDescriptor.getStartOffset(),
                                assetFileDescriptor.getLength());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                            }
                          }
                      });
                    } else if(softLoud.isChecked()){
                        softLoud.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
                                toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);

                }
            });
        }
    }

我的想法是我有三个单选按钮(这里只显示两个)按钮调用一个自记录的.mp3文件,另一个创建一个增加音量的蜂鸣声模式(音量增加未显示因为我觉得这个问题需要先解决,我正在研究目前的延误。当应用程序运行时,只执行一个if语句,例如,如果我先点击蜂鸣声,它将始终发出蜂鸣声,但录音将不起作用,反之亦然。 此外,它应该在onStart,onCreate还是onResume?

以下是完整的活动:

public class AudioActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);
        //load preferred values
        loadPrefs();


        //Once audio is checked press ok button to return to feedback activity
        //initialize the radio buttons
        final RadioButton softLoud = (RadioButton) findViewById(R.id.low_to_high_radioButton);
        final RadioButton loudSoft = (RadioButton) findViewById(R.id.high_to_low_radioButton);
        final RadioButton verbalCommand = (RadioButton) findViewById(R.id.step_voice_radioButton);

        Button audioOkButton = (Button) findViewById(R.id.audio_ok);
        if (audioOkButton != null && (softLoud.isChecked() || loudSoft.isChecked() || verbalCommand.isChecked())) {
            audioOkButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(AudioActivity.this, FeedbackActivity.class);
                    startActivity(intent);
                }
            });
        } else if (audioOkButton != null && !(softLoud.isChecked() || loudSoft.isChecked() || verbalCommand.isChecked())) {
            audioOkButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showSimplePopUp();
                }
            });
        }
            //function call to have a response when button clicked
            CompoundButton.OnCheckedChangeListener checker = new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //save button preference
                    savePrefs("softLoudAudio", softLoud.isChecked());
                    savePrefs("loudSoftAudio", loudSoft.isChecked());
                    savePrefs("verbalCommandAudio", verbalCommand.isChecked());
                }
            };
            softLoud.setOnCheckedChangeListener(checker);
            loudSoft.setOnCheckedChangeListener(checker);
            verbalCommand.setOnCheckedChangeListener(checker);
        }


    private void loadPrefs() {
         RadioButton softLoud = (RadioButton) findViewById(R.id.low_to_high_radioButton);
         RadioButton loudSoft = (RadioButton) findViewById(R.id.high_to_low_radioButton);
         RadioButton verbalCommand = (RadioButton) findViewById(R.id.step_voice_radioButton);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

        //Creates a cache with default value of false
        boolean softLoudValue = sp.getBoolean("softLoudAudio", false);
        boolean loudSoftValue = sp.getBoolean("loudSoftAudio", false);
        boolean verbalCommandValue = sp.getBoolean("verbalCommandAudio", false);

        if(softLoudValue){
            softLoud.setChecked(true);
        }else if(loudSoftValue){
            loudSoft.setChecked(true);
        }else if(verbalCommandValue){
            verbalCommand.setChecked(true);
        }else{
            softLoud.setChecked(false);
            loudSoft.setChecked(false);
            verbalCommand.setChecked(false);
        }
    }

    private void savePrefs(String key, boolean value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    private void showSimplePopUp() {

        AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
        helpBuilder.setTitle(R.string.oops);
        helpBuilder.setMessage(R.string.error_message);
        helpBuilder.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // Do nothing but close the dialog
                    }
                });

        // Remember, create doesn't show the dialog
        AlertDialog helpDialog = helpBuilder.create();
        helpDialog.show();
    }
    public void onStart(){
        super.onStart();
        final RadioButton softLoud = (RadioButton) findViewById(R.id.low_to_high_radioButton);
        final RadioButton loudSoft = (RadioButton) findViewById(R.id.high_to_low_radioButton);
        final RadioButton verbalCommand = (RadioButton) findViewById(R.id.step_voice_radioButton);
        final MediaPlayer mediaPlayer = new MediaPlayer();
        if (verbalCommand.isChecked()){
            verbalCommand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                    }
                    try {
                        mediaPlayer.reset();
                        AssetFileDescriptor assetFileDescriptor;
                        //Change mp3 file using voice recorder when ready
                        assetFileDescriptor = getAssets().openFd("Noise.mp3");
                        mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                                assetFileDescriptor.getStartOffset(),
                                assetFileDescriptor.getLength());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                            }
                          }
                      });
                    } else if(softLoud.isChecked()){
                        softLoud.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
                                toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);

                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不确定为什么,但我只使用一个onClick方法修复了问题,而不是我发布的两个:

public void onStart() {
        super.onStart();
        final RadioButton softLoud = ((RadioButton) this.findViewById(R.id.low_to_high_radioButton));
        final RadioButton loudSoft = (RadioButton) this.findViewById(R.id.high_to_low_radioButton);
        final RadioButton verbalCommand = (RadioButton) this.findViewById(R.id.step_voice_radioButton);
        softLoud.setOnClickListener(this);
        verbalCommand.setOnClickListener(this);
        loudSoft.setOnClickListener(this);
    }

    public void onClick(View view) {
        final MediaPlayer mediaPlayer = new MediaPlayer();
        int id = view.getId();
        if (id == R.id.step_voice_radioButton) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            try {
                mediaPlayer.reset();
                AssetFileDescriptor assetFileDescriptor;
                //Change mp3 file using voice recorder when ready
                assetFileDescriptor = getAssets().openFd("Noise.mp3");
                mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                        assetFileDescriptor.getStartOffset(),
                        assetFileDescriptor.getLength());
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (id == R.id.low_to_high_radioButton) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 700);
        } else if(id == R.id.high_to_low_radioButton) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            ToneGenerator tones = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            tones.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 1000);
        }
    }