2016-07-06 65 views
0

我也有复选框功能的这个错误。没有错误产生,但功能表明我的代码片段未被达到或未被正确读取。三个单独的单选按钮调用不同的声音

这里是我要解决具体的片段:

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

       } 
      }); 
     } 
    } 
} 

回答

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); 
     } 
    } 
相关问题