Rabu, 08 Mei 2013

Membuat Aplikasi Android Pengusir Nyamuk dan Tikus dengan Suara Frekuensi Tinggi

Mungkin aplikasi ini terdengar sangat konyol, tapi mungkin begitulah adanya :D Ide dasarnya sebenarnya dari Aplikasi Pengusir Nyamuk dan Tikus yang pernah penulis dapatkan, diinstall di laptop / PC dan suaranya memang cukup mengganggu ^_^' Dari ide tersebut penulis bisa membuat Piano Virtual Menggunakan Frekuensi Tanpa File Sound. Dan akhirnya penulis perkenalkan di bawah ini. Aplikasi Pengusir Nyamuk dan Tikus yang sangat-sangat belum sempurna ;p

Langkah pertama buat Project Android baru, ketikkan kode program berikut di MainActivity.java :


package com.amijaya.androidmosquitorat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.widget.Button;
import android.widget.Toast;

// http://cariprogram.blogspot.com
// nuramijaya@gmail.com

public class MainActivity extends Activity implements OnClickListener {

private static final int duration = 1;
private static final int sampleRate = 8000;
private static final int numSamples = duration * sampleRate;

private final double f16kHz = 16000.000;
private final double f20kHz = 20000.000;
private final double f30kHz = 30000.000;
private final double f40kHz = 40000.000;
private final double f50kHz = 50000.000;
private final double f60kHz = 60000.000;
private final double f65kHz = 65000.000;

private Button button16kHz;
private Button button20kHz;
private Button button30kHz;
private Button button40kHz;
private Button button50kHz;
private Button button60kHz;
private Button button65kHz;

private byte[] generateSound(double frequency) {
double[] soundSample = new double[numSamples];
for (int i = 0; i < numSamples; i++) {
soundSample[i] = Math.sin(2 * Math.PI * i / (sampleRate/frequency));
}

byte[] sound = new byte[2 * numSamples];
int idx = 0;
for (double dVal: soundSample) {
short val = (short) (dVal * 32767);
sound[idx++] = (byte) (val & 0x00ff);
sound[idx++] = (byte) ((val & 0xff00) >>> 8);
}

return sound;
}

private void playSound(byte[] sound) { 
final byte[] play = sound;
//Concurrency for music playing
(new Thread(new Runnable() {
public void run() {
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_DEFAULT, AudioFormat.ENCODING_PCM_16BIT, numSamples, AudioTrack.MODE_STATIC);
audioTrack.write(play, 0, numSamples);
audioTrack.play();
}
})).start();
}


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button16kHz = (Button) findViewById(R.id.button16kHz);
        button20kHz = (Button) findViewById(R.id.button20kHz);
        button30kHz = (Button) findViewById(R.id.button30kHz);
        button40kHz = (Button) findViewById(R.id.button40kHz);
        button50kHz = (Button) findViewById(R.id.button50kHz);
        button60kHz = (Button) findViewById(R.id.button60kHz);
        button65kHz = (Button) findViewById(R.id.button65kHz);
        
        button16kHz.setOnClickListener(this);
        button20kHz.setOnClickListener(this);
        button30kHz.setOnClickListener(this);
        button40kHz.setOnClickListener(this);
        button50kHz.setOnClickListener(this);
        button60kHz.setOnClickListener(this);
        button65kHz.setOnClickListener(this);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

@Override
public void onClick(View v) {
//TODO need create a object method that changes the pitch
if (v == button16kHz) {
playSound(generateSound(f16kHz));
Toast.makeText(getApplicationContext(), "16.000 kHz", Toast.LENGTH_LONG).show();
}
if (v == button20kHz) {
playSound(generateSound(f20kHz));
Toast.makeText(getApplicationContext(), "20.000 kHz", Toast.LENGTH_LONG).show();
}
if (v == button30kHz) {
playSound(generateSound(f30kHz));
Toast.makeText(getApplicationContext(), "30.000 kHz", Toast.LENGTH_LONG).show();
}
if (v == button40kHz) {
playSound(generateSound(f40kHz));
Toast.makeText(getApplicationContext(), "40.000 kHz", Toast.LENGTH_LONG).show();
}
if (v == button50kHz) {
playSound(generateSound(f50kHz));
Toast.makeText(getApplicationContext(), "50.000 kHz", Toast.LENGTH_LONG).show();
}
if (v == button60kHz) {
playSound(generateSound(f60kHz));
Toast.makeText(getApplicationContext(), "60.000 kHz", Toast.LENGTH_LONG).show();
}
if (v == button65kHz) {
playSound(generateSound(f65kHz));
Toast.makeText(getApplicationContext(), "65.000 kHz", Toast.LENGTH_LONG).show();
}

}

}


Setelah itu buat tampilan layout activity_main.xml sebagai berikut :


<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Pengusir Nyamuk dan Tikus"
    />
<Button android:text="16000 Hz" android:id="@+id/button16kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="20000 Hz" android:id="@+id/button20kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="30000 Hz" android:id="@+id/button30kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="40000 Hz" android:id="@+id/button40kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="50000 Hz" android:id="@+id/button50kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="60000 Hz" android:id="@+id/button60kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="65000 Hz" android:id="@+id/button65kHz" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</ScrollView>


Untuk AndroidManifest.xml tidak ada perubahan :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amijaya.androidmosquitorat"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 


Hasilnya :


Dan sebagai catatan saja, di Android 2.3 Gingerbread Galaxy Mini yang keluar hanya Beberapa Frekuensi, tidak semuanya. Kenapa hal itu bisa terjadi? Entahlah, mungkin anda bisa menjawabnya ?? :)

Project selengkapnya silakan didownload di sini. Jika ada kesulitan download ini cara downloadnya.

Semoga menyenangkan ^_^


1 komentar:

  1. Gan, ane bikin pake eclipse..Pas coding di MainActivity.java nya...Kok ada error yaa di setContentView(R.layout.main); ?

    dan pas coding di Activity_Main nya error di <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ?

    Tolong kasih solusi nya yaa gan, Terima Kasih

    BalasHapus