Anda di halaman 1dari 5

Audio Manager

1) Buat project baru dan beri nama Audio Manager


2) Masukkan gambar Audio ke mipmap

3) Masukkan ImageView

4) Buat tampilan layout seperti gambar


5) Ubah id dan nama tombol
Nama Id
RECORD btn_rec
STOP btn_stp
PALY btn_ply

6) Tambahkan permission pada AndroidManivest.xml


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.STORAGE" />

7) Masukkan program ke MainActivity.java


package com.example.blue_lion.audio_manager;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

Button Play, Stop, Record;


private MediaRecorder RecorderAudio;
private String outputFile=null;
String folder= "TMJ";
int nomorfile=0;

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

Play =(Button) findViewById(R.id.btn_ply);


Stop = (Button) findViewById(R.id.btn_stp);
Record=(Button) findViewById(R.id.btn_rec);

Stop.setEnabled(false);
Play.setEnabled(false);

File f1=new
File(Environment.getExternalStorageDirectory().getAbsolutePath()
+"/"+folder);
if (!f1.exists()){
f1.mkdir();
}else {

Record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nomorfile++;

outputFile=Environment.getExternalStorageDirectory().getAbsolutePath()
+"/"+folder+"/recording"+nomorfile+".3gp";

RecorderAudio = new MediaRecorder();

RecorderAudio.setAudioSource(MediaRecorder.AudioSource.MIC);

RecorderAudio.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

RecorderAudio.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
RecorderAudio.setOutputFile(outputFile);
try {
RecorderAudio.prepare();
RecorderAudio.start();
}
catch (IllegalStateException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
Record.setEnabled(false);
Stop.setEnabled(true);
Play.setEnabled(true);

Toast.makeText(getApplicationContext(),"Recording
State",Toast.LENGTH_SHORT).show();
}
});

Stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RecorderAudio.stop();
RecorderAudio.release();
RecorderAudio=null;

Stop.setEnabled(false);
Play.setEnabled(true);
Record.setEnabled(true);
Toast.makeText(getApplicationContext(),"Audio Record
Succsesfull",Toast.LENGTH_SHORT).show();
}
});
Play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MediaPlayer mp = new MediaPlayer();

try {
mp.setDataSource(outputFile);
}catch (IOException e){
e.printStackTrace();
}
try {
mp.prepare();
}catch (IOException e){
e.printStackTrace();
}
mp.start();
Toast.makeText(getApplicationContext(),"Memutar
Audio",Toast.LENGTH_SHORT).show();
}
});
}
}

8) Scren Shoot Program