본문 바로가기

아옳옳의 코딩공부/아옳옳의 안드로이드스튜디오

2021-06-14안드로이드 스튜디오 (멀티미디어 오디오녹음 저장)

반응형

오디오 녹음하여 저장하기 

@많이 안쓰는 기능이기 때문에 공부한 코딩 내용만 참조하겠음 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnRecord, btnRecStop, btnPlay, btnStop;
    MediaRecorder mediaRecorder;
    MediaPlayer mediaPlayer;
    String filePath;
    boolean first = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(this , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO},0);

        btnRecord = findViewById(R.id.button);
        btnRecStop = findViewById(R.id.button2);
        btnPlay = findViewById(R.id.button3);
        btnStop = findViewById(R.id.button4);

        btnRecord.setOnClickListener(this);
        btnRecStop.setOnClickListener(this);
        btnPlay.setOnClickListener(this);
        btnStop.setOnClickListener(this);

        filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/recordData.mp3";

    }

    @Override
    public void onClick(View v) {

        if(v == btnRecord){
            if(mediaPlayer != null){
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder=null;
            }else {
                mediaRecorder = new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                mediaRecorder.setOutputFile(filePath);
                Toast.makeText(this,"녹음시작",Toast.LENGTH_SHORT).show();
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }else if (v==btnRecStop ){

            if(mediaRecorder == null){
                return;
            }else {
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder=null;
                Toast.makeText(this,"녹음중지",Toast.LENGTH_SHORT).show();
            }

        }else if (v ==btnPlay ){
          if(mediaPlayer !=null){
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer=null;
            }else {
                mediaPlayer = new MediaPlayer();
                try {
                    mediaPlayer.setDataSource(filePath);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    Toast.makeText(this,"재생시작",Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }else if(v==btnStop){
            if(mediaPlayer == null ){
                return;
            }else {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer=null;
                Toast.makeText(this,"재생중지",Toast.LENGTH_SHORT).show();
            }
        }
    }

말그대로 녹음기 기능이라 많이 사용 안할거 같다 ... 

 

유튜브 영상 

(매번 변경되다보니 필요할때 찾아서 사용하도록 하자 ) 

카메라로 사진찍어 저장하기 

public class MainActivity extends AppCompatActivity {

    File file; // 파일 저장을 위해 
    ImageView imageView; // 띄워줄 이미지뷰 
    Bitmap bitmap; // 비트맵으로 변경하기 위함 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //퍼미션 
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA},0);
        
        //이 이름으로 저장하겠다 .  curFile = new File(getFilesDir(), fileStamp); 이렇게 써줘야 한다 30버전 이상 
        file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"intentCamera.jpg");
       
        imageView = findViewById(R.id.imageView);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) {
                    Uri fileURI = FileProvider.getUriForFile(getApplicationContext(), "com.cyberkyj.cameraintent.fileprovider", file);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileURI);
                }else{
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                }
                if(intent.resolveActivity(getPackageManager())!=null){
                    startActivityForResult(intent,1000);
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1000){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            Resources r = Resources.getSystem();
            Configuration configuration = r.getConfiguration();
            if(configuration.orientation==ORIENTATION_LANDSCAPE){
                //가로모드
                imageView.setImageBitmap(bitmap);
            }else{
                //세로모드
                Matrix matrix = new Matrix();
                matrix.postRotate(90);
                Bitmap rotatebitmap = Bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(),matrix,true);
                bitmap = rotatebitmap;
                imageView.setImageBitmap(bitmap);
            }


        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if(newConfig.orientation==ORIENTATION_LANDSCAPE){
            imageView.setImageBitmap(bitmap);
        }else {
            imageView.setImageBitmap(bitmap);
        }
    }
}
반응형