관리 메뉴

tapito.tistory.com

MediaRecorder 클래스 사용법 (소리 편) 본문

Application Programming Interface/Android

MediaRecorder 클래스 사용법 (소리 편)

Tapito 2015.02.09 09:53

MediaRecorder 클래스 사용법 (소리 편)

- by Tapito


Java Class 작성

package com.example.mediarecordertest;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity
{
	private MediaRecorder mediaRecorder = null;
	String outputDirectory = null;
	String outputFile = "record.mp4";
	
	private Button buttonRecord = null;
	private Button buttonStop = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		this.buttonRecord = (Button)this.findViewById(com.example.mediarecordertest.R.id.button1);
		this.buttonStop = (Button)this.findViewById(com.example.mediarecordertest.R.id.button2);

		this.buttonRecord.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				try
				{
					outputDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "testrec";
					mediaRecorder = new MediaRecorder();
					
					mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
					mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
					mediaRecorder.setOutputFile(outputDirectory + outputFile);
					
					mediaRecorder.prepare();
					mediaRecorder.start();
				}
				catch (IllegalStateException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		this.buttonStop.setOnClickListener(new View.OnClickListener()
		{	
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				mediaRecorder.stop();
				mediaRecorder.reset();
				mediaRecorder.release();
				
				mediaRecorder = null;
			}
		});
	}
}
저작자 표시 비영리 변경 금지
신고
0 Comments
댓글쓰기 폼