2011-12-20 4 views
0

버튼이 있습니다. 해당 기능을 원한다면 버튼을 클릭하십시오. 소리를 녹음하기 시작합니다. 두 번째 클릭하면 세 번째 옵션이 표시됩니다 (소리 재생, 새로운 녹음). 나는이 작업을 도움 당신은 클래스 수준 변수를 설정하고 증발 것을 클릭 결정해야버튼 하나에 여러 기능이 있습니다.

k=(Button)findViewById(R.id.button1); 
    k.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

답변

3

을 구현하는 방법) 사운드를 삭제합니다. 이 접근법의 어려움은 언제 값을 재설정할지 정의하는 것입니다.

private int _clicks = 0; 

k = (Button)findViewById(R.id.button1); 
k.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     int count = ++_clicks; 

     if(count == 1) 
      //do whatever 
     if(count == 2) 
      //do whatever 
     if(count == 3) 
      //do whatever 
    } 
}); 

더 나은 방법은 여러 개의 단추에 여러 개의 처리기를 지정하고 단추에 필요한 작업을 수행하는 것입니다. 이렇게하면 1 : 1 관계를 정의 할 수 있으며 코드를 훨씬 더 쉽게 관리 할 수 ​​있습니다.

편집 : 소리를 녹음하려면 웹상에 plenty of examples이 있습니다.

public class AudioRecorder { 

    final MediaRecorder recorder = new MediaRecorder(); 
    final String path; 

    /** 
    * Creates a new audio recording at the given path (relative to root of SD card). 
    */ 
    public AudioRecorder(String path) { 
    this.path = sanitizePath(path); 
    } 

    private String sanitizePath(String path) { 
    if (!path.startsWith("/")) { 
     path = "/" + path; 
    } 
    if (!path.contains(".")) { 
     path += ".3gp"; 
    } 
    return Environment.getExternalStorageDirectory().getAbsolutePath() + path; 
    } 

    /** 
    * Starts a new recording. 
    */ 
    public void start() throws IOException { 
    String state = android.os.Environment.getExternalStorageState(); 
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     throw new IOException("SD Card is not mounted. It is " + state + "."); 
    } 

    // make sure the directory we plan to store the recording in exists 
    File directory = new File(path).getParentFile(); 
    if (!directory.exists() && !directory.mkdirs()) { 
     throw new IOException("Path to file could not be created."); 
    } 

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(path); 
    recorder.prepare(); 
    recorder.start(); 
    } 

    /** 
    * Stops a recording that has been previously started. 
    */ 
    public void stop() throws IOException { 
    recorder.stop(); 
    recorder.release(); 
    } 

} 
+0

U 내가 어떻게 말할 수 첫 번째 클릭으로 사운드 녹음 – Ramz

+0

@ user1103284 편집 추가, 녹음 소리에 대한 참조 –

+0

재생할 때 감사합니다 – Ramz

1

또 다른 접근법은 3 개의 서로 다른 onClickListeners를 갖는 것입니다. 당신의에서 onCreate에서

:이 코드를 한 번 봐 걸릴

... 
button.setOnClickListener(playListener); 
... 

play(), stop() 및 활동에 어디 showOption()에 대한 방법을 만들

을, 이것은 :

OnClickListener playListener = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      play(); 
      v.setOnClickListener(stopListener); 
     } 
    }; 

    OnClickListener stopListener = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      stop(); 
      v.setOnClickListener(showOptionListener); 
     } 
    }; 

    OnClickListener showOptionListener = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showOption(); 
     } 
    }; 
관련 문제