Ada dua cara untuk membuat Record Audio. Menggunakan Intent Bawaan Android. atau mau Coding sendiri. Nah berikut ini jika kita menggunakan intent bawaan Android.
1. Buat Activity Baru MainActivity sajalah namanya.
2. Buatlah layout xml nya, kira2 seperti ini:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<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=".MainActivity" > <Button android:id="@+id/record" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Record" /> </RelativeLayout> |
3. Kita hanya perlu memanggil intent RECORD_SOUND_ACTION bawaan Android.
0 1 |
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivityForResult(intent, RECORD_REQUEST); |
Oh ya, jangan lupa buat variabel konstanta
0 |
final static int RECORD_REQUEST = 1; |
4. Kemudian di tangkap melalui onActivityResult.
0 1 2 3 4 5 6 7 8 |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == RECORD_REQUEST){ if (resultCode == RESULT_OK){ savedUri = data.getData(); Toast.makeText(MainActivity.this,"Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show(); } } } |
5. Kalau mau liha source lengkapnya.
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 |
package com.hangga.audiorecordtest; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { final static int RECORD_REQUEST = 1; Uri savedUri; Button btnRecord; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnRecord = (Button)findViewById(R.id.record); btnRecord.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivityForResult(intent, RECORD_REQUEST); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == RECORD_REQUEST){ if (resultCode == RESULT_OK){ savedUri = data.getData(); Toast.makeText(MainActivity.this,"Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show(); } } } } |