Bismillah..
Pada kasus berikut ini, yg menjadi bahan bahasan adalah bagaimana mebuat aplikasi record audio dengan efek audiovisualizer dengan input microphone. Akhirnya saya mengakalinya dengan cara yg boleh dibilang bodoh. Yaitu dengan menggunakan RelativeLayout yang dirubah-rubah tinggi atau ukuranya selama proses record berlangsung.
Saya memanfaatkan kelas RecorderTask yang mengextends dari kelas TimerTask. Kemudian dikelas inilah proses perubahan tinggi RelativeLayout selama proses record berlangsung dimana tinggi Relativelayout sesuai dengan nilai amplitudonya.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private class RecorderTask extends TimerTask{ private MediaRecorder recorder; public RecorderTask(MediaRecorder recorder){ this.recorder = recorder; } public void run(){ runOnUiThread(new Runnable() { @Override public void run() { yAmplitudo = recorder.getMaxAmplitude()/ 100; RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) relativeMeter.getLayoutParams(); param.height = yAmplitudo; relativeMeter.setLayoutParams(param); } }); } } |
Berikut kode selengkapnya. Silahkan Anda bisa mengembangkannya sendiri.
1. Layout xmlnya adalah seperti dibawah ini.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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=".RecordAudioActivity" > <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="185dp" android:textSize="12sp" android:text="Start" /> <Button android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/btnStart" android:layout_alignParentBottom="true" android:layout_marginBottom="185dp" android:textSize="12sp" android:enabled="false" android:text="Stop" /> <RelativeLayout android:layout_width="22dp" android:layout_height="120dp" android:padding="2dp" android:layout_above="@+id/btnStop" android:layout_centerHorizontal="true" android:background="#000000" android:layout_marginBottom="18dp"> <RelativeLayout android:id="@+id/relativeMeter" android:layout_width="fill_parent" android:layout_height="0dp" android:background="#19FF34" android:layout_alignParentBottom="true" > </RelativeLayout> </RelativeLayout> </RelativeLayout> |
2. Kemudian source lengkap nya.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
package com.hangga.amplitudometer; import java.io.IOException; import java.math.BigInteger; import java.security.SecureRandom; import java.util.Timer; import java.util.TimerTask; import android.media.MediaRecorder; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class RecordAudioActivity extends Activity { RelativeLayout relativeMeter; MediaRecorder recorder; Timer timer; TextView risultato; Button btnStart; Button btnStop; String filePath = null; int yAmplitudo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeMeter = (RelativeLayout) findViewById(R.id.relativeMeter); btnStart = (Button) findViewById(R.id.btnStart); btnStop = (Button) findViewById(R.id.btnStop); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //recorder.setOutputFile("/dev/null"); filePath = "/sdcard/"+getRandomString()+".m4a"; recorder.setOutputFile(filePath); try { recorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { recorder.start(); timer = new Timer(); timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 300); btnStart.setEnabled(false); btnStop.setEnabled(true); } }); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goStop(); } }); } private void goStop(){ recorder.stop(); recorder.release(); timer.cancel(); timer.purge(); btnStart.setEnabled(true); btnStop.setEnabled(false); Toast.makeText(getApplicationContext(), "Saved : "+filePath, Toast.LENGTH_SHORT).show(); finish(); } private class RecorderTask extends TimerTask{ private MediaRecorder recorder; public RecorderTask(MediaRecorder recorder){ this.recorder = recorder; } public void run(){ runOnUiThread(new Runnable() { @Override public void run() { yAmplitudo = recorder.getMaxAmplitude()/ 100; RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) relativeMeter.getLayoutParams(); param.height = yAmplitudo; relativeMeter.setLayoutParams(param); } }); } } public String getRandomString() { SecureRandom random = new SecureRandom(); String randomString = new BigInteger(130, random).toString(32); return(randomString); } } |