538
No
핵심
//녹음시작
public void onButton5Clicked(View v){
Log.i(TAG, "녹음 버튼 클릭 됨");
try{
if(recorder!=null){
recorder.stop();
recorder.release();
recorder=null;
}
recorder =new MediaRecorder();
//순서가 틀리면 오류
//음성이 마이크로 들어가기 때문에 마이크 지정
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//어떤 포맷으로 파일을 저장 할 것인가
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//단말에서 지정된 default
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}catch (Exception e){
e.printStackTrace();
}
toastShow("녹음을 시작합니다.");
}
//녹음중지
public void onButton6Clicked(View v){
Log.i(TAG, "녹음중지 클릭 됨");
if(recorder!=null){
recorder.stop();
recorder.release();
recorder=null;
toastShow("녹음을 중지합니다.");
}
}
class MainActivity
package kr.co.braverokmc.audioplayer;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
MediaPlayer player;
String url ="https://sites.google.com/site/ubiaccessmobile/sample_audio.amr";
String path="/sdcard/recorded.mp4";
Toast toast;
int position; // 일시정지시 현재 위치 저장
MediaRecorder recorder; //녹음
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//시작
public void onButton1Clicked(View v){
Log.i(TAG, "시작 버튼 클릭 됨");
try {
killPlayer();
player =new MediaPlayer();
//player.setDataSource(url);
player.setDataSource(path);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
toastShow("재생을 시작합니다.");
}
private void killPlayer(){
if(player!=null){
player.release(); //리소스를 전부 해제
player=null;
}
}
//일시정지
public void onButton2Clicked(View v){
Log.i(TAG, "일시정지 클릭 됨");
if(player!=null && player.isPlaying()){
position=player.getCurrentPosition();
player.pause();
toastShow("재생을 일시정지합니다.");
}
}
//재시작
public void onButton3Clicked(View v){
Log.i(TAG, "재시작 클릭 됨");
if(player!=null && !player.isPlaying()){
player.start();
player.seekTo(position);// 정지 위치 찾아가기
toastShow("재생을 재시작합니다.");
}
}
//중지
public void onButton4Clicked(View v){
Log.i(TAG, "중지 버튼 클릭 됨");
if(player!=null && player.isPlaying()){
player.stop();
}
toastShow("재생을 중지합니다.");
}
//녹음시작
public void onButton5Clicked(View v){
Log.i(TAG, "녹음 버튼 클릭 됨");
try{
if(recorder!=null){
recorder.stop();
recorder.release();
recorder=null;
}
recorder =new MediaRecorder();
//순서가 틀리면 오류
//음성이 마이크로 들어가기 때문에 마이크 지정
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//어떤 포맷으로 파일을 저장 할 것인가
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//단말에서 지정된 default
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}catch (Exception e){
e.printStackTrace();
}
toastShow("녹음을 시작합니다.");
}
//녹음중지
public void onButton6Clicked(View v){
Log.i(TAG, "녹음중지 클릭 됨");
if(recorder!=null){
recorder.stop();
recorder.release();
recorder=null;
toastShow("녹음을 중지합니다.");
}
}
private void toastShow(String text){
if(toast==null){
toast=Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
}else {
toast.setText(text);
}
toast.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
killPlayer();
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.audioplayer.MainActivity">
<Button
android:text="시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="116dp"
android:layout_marginStart="116dp"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onButton1Clicked"
/>
<Button
android:text="일시정지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1"
android:onClick="onButton2Clicked"
/>
<Button
android:text="재시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:onClick="onButton3Clicked"
/>
<Button
android:text="중지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3"
android:layout_marginTop="49dp"
android:id="@+id/button4"
android:onClick="onButton4Clicked"
/>
<Button
android:text="녹음시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:onClick="onButton5Clicked"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"
android:layout_marginTop="46dp" />
<Button
android:text="녹음중지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button5"
android:layout_alignEnd="@+id/button5"
android:id="@+id/button"
android:onClick="onButton6Clicked"
/>
</RelativeLayout>
권한설정
AndroidMainfest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
댓글 ( 4)
댓글 남기기