안드로이드 음성녹음 예제에서 오류가 생깁니다.


            recorder.setAudioEncoder(MediaRecorder.AudioSource.MIC);

이 부분에서 에러가 발생합니다.. 왜이럴까요..??

public class MainActivity extends Activity {


static final String RECORD_FILE="/sdcard/record.mp4";

MediaPlayer player;
MediaRecorder recorder;

int playbackPos=0;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button record = (Button)findViewById(R.id.recordBtn);
    Button recordStop = (Button)findViewById(R.id.recordStopBtn);
    final Button play = (Button)findViewById(R.id.playBtn);
    Button playStop = (Button)findViewById(R.id.playStopBtn);

    getPermissionToRECORD();

    record.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(recorder!=null){
                recorder.stop();
                recorder.release();
                recorder=null;
            }
            recorder = new MediaRecorder();
            recorder.setAudioEncoder(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            recorder.setOutputFile(RECORD_FILE);
            try{
                Toast.makeText(MainActivity.this, "Starting Recording", Toast.LENGTH_SHORT).show();
                recorder.prepare();
                recorder.start();
            } catch (IOException e) {
                Log.e("AudioRecorder","Exception : "+e);
            }
        }
    });
    recordStop.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(recorder==null)
                return;
            recorder.stop();
            recorder.release();
            recorder=null;

            Toast.makeText(MainActivity.this, "Stopped Recording..", Toast.LENGTH_SHORT).show();
        }
    });

    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                playAudio(RECORD_FILE);

                Toast.makeText(MainActivity.this, "Playing RecFile..", Toast.LENGTH_SHORT).show();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });

    playStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(player!=null){
                playbackPos=player.getCurrentPosition();
                player.pause();
                Toast.makeText(MainActivity.this, "Stopped Playing..", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void playAudio(String url) throws Exception{
    killPlayer();

    player=new MediaPlayer();
    player.setDataSource(url);
    player.prepare();
    player.start();
}

protected void onDestroy(){
    super.onDestroy();
    killPlayer();
}

private void killPlayer(){
    if(player!=null){
        try{
            player.release();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

protected void onPause(){
    if(recorder!=null){
        recorder.release();
        recorder=null;
    }
    if(player!=null){
        player.release();
        player=null;
    }

    super.onPause();
}


private static final int RECORD_AUDIO_PERMISSIONS_REQUEST = 1;
private static final int WRITE_EXTERNAL_STORAGE_PERMISSIONS_REQUEST=2;

@TargetApi(Build.VERSION_CODES.M)
public void getPermissionToRECORD() {

    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
    // checking the build version since Context.checkSelfPermission(...) is only available
    // in Marshmallow
    // 2) Always check for permission (even if permission has already been granted)
    // since the user can revoke permissions at any time through Settings
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {

        // The permission is NOT already granted.
        // Check if the user has been asked about this permission already and denied
        // it. If so, we want to give more explanation about why the permission is needed.
        if (shouldShowRequestPermissionRationale(
                Manifest.permission.RECORD_AUDIO)) {
            // Show our own UI to explain to the user why we need to read the contacts
            // before actually requesting the permission and showing the default UI
        }

        // Fire off an async request to actually get the permission
        // This will show the standard permission request dialog UI
        requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO_PERMISSIONS_REQUEST);
    }

    else if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission. WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // The permission is NOT already granted.
        // Check if the user has been asked about this permission already and denied
        // it. If so, we want to give more explanation about why the permission is needed.
        if (shouldShowRequestPermissionRationale(
                Manifest.permission. WRITE_EXTERNAL_STORAGE)) {
            // Show our own UI to explain to the user why we need to read the contacts
            // before actually requesting the permission and showing the default UI
        }

        // Fire off an async request to actually get the permission
        // This will show the standard permission request dialog UI
        requestPermissions(new String[]{Manifest.permission. WRITE_EXTERNAL_STORAGE},  WRITE_EXTERNAL_STORAGE_PERMISSIONS_REQUEST);
    }
}

// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    // Make sure it's our original CAMERA request
    if (requestCode == RECORD_AUDIO_PERMISSIONS_REQUEST) {
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(), "RECORD_AUDIO permission granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "RECORD_AUDIO permission denied", Toast.LENGTH_SHORT).show();
        }
    }
    else if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSIONS_REQUEST) {
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(), "WRITE_EXTERNAL_STORAGE permission granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "WRITE_EXTERNAL_STORAGE permission denied", Toast.LENGTH_SHORT).show();
        }
    }
    else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

}


조회수 198


1 답변


로그캣으로 출력되는 에러 로그를 올려주시면 문제를 파악하는데 도움이 될 것 같습니다. 음성 녹음과 관련된 아래 글도 참고하세요.


로그인이 필요한 기능입니다.

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 작성한 답변에 다른 개발자들이 댓글을 작성하거나 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.
► 로그인
► 계정만들기
Close