如何检测按钮背景资源(drawable)android?

时间:2014-11-19 20:50:19

标签: android drawable ontouchlistener mediarecorder

我目前正在构建一个具有按钮的应用程序,一旦用户单击该按钮,将启动介质记录器并记录五秒钟然后停止。然后将该记录加载到媒体播放器中,然后用户可以通过再次按下按钮来播放该媒体播放器。到目前为止,这是我的代码:

    OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/tempRecord.3gpp";
    audioSample1 = (Button) findViewById(R.id.sample1);
    // button background drawable is grey_button
    final Drawable buttonBackground = audioSample1.getBackground();     
    audioSample1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (audioSample1.getBackground().equals(buttonBackground)) {
                Log.d("Button", "Start Record");
                try {
                    startRecord();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            else if (audioSample1.getBackground() != null) {
                Log.d("Button", "Start Playback");
                try {
                    startPlayback();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });

}

public void startRecord() throws Exception {
    if (recorder != null) {
        recorder.release();
    }
    File fileOut = new File(OUTPUT_FILE);
    if (fileOut != null) {
        fileOut.delete();
    }
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.setAudioSamplingRate(44100);
    recorder.setMaxDuration(5000);
    recorder.prepare();
    recorder.start();
    audioSample1.setBackgroundResource(R.drawable.red_button);
    recorder.setOnInfoListener(new OnInfoListener() {

        public void onInfo(MediaRecorder recorder, int timer, int extra) {
            if (timer == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                Log.v("Recorder", "Maximum Duration Reached");
                recorder.stop();
                audioSample1.setBackgroundResource(R.drawable.green_button);    
            }
        }
    });
}

public void startPlayback() throws Exception {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
    }
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(OUTPUT_FILE);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

}

基本上我遇到的问题是onClick方法检测按钮的状态,并根据背景可绘制它开始录制或开始播放。如果按钮处于其第一状态drawable.grey_button,则媒体记录器将开始记录并将背景可绘制更改为red_button,一旦完成记录,它将变为green_button以进行回放。此代码通常工作正常但如果用户在媒体记录器正在录制时按下按钮,则媒体播放器将无法创建媒体播放器。我知道问题是因为我正在调用audioSample1.getBackground() != null,但我无法找出任何其他方法来检测已更改的backgroundResources

请有人就如何检测每个backgroundResource提供一些建议吗?

此外,一旦媒体播放器加载了音频样本,我现在想要相同的按钮能够实现onTouchListener和手势检测器。这是可能的,你如何实现这样的方法?

谢谢

1 个答案:

答案 0 :(得分:1)

你接近真的很乏味,但这就是 getBackgroundDrawable(); 而是使用它getBackground() 返回按钮的可绘制集。

但您也可以使用该按钮的字符串,这可能很简单......

编辑1 此方法更好

  Button b = new Button(this); // now this is your button
    b.setText("Play"); //the button has a name as 'play' when its not clicked 
    // and in your case is drawable.grey_button
    b.setBackgroundResource(R.drawable.grey_button);

    b.setOnClickListener(new OnClickListener() {//setting your onclick

        @Override
        public void onClick(View arg0) {
            Button bb = (Button) arg0; // getting your clicked button
            if(bb.getText().toString().equals("play")){ // checking if the text on your bb is play
                //start your playback and change the background
                bb.setText("pause"); // setting the text to pause
            }else if(bb.getText().toString().equals("pause")){
                //pause playback and change your background
                bb.setText("play");
            }else{
                //stop play and reset mediaplayer and change your background.. 
                // this is not even captured
            }

        }
    });

这种方法更好..让我知道它是否有帮助