Explanation:
In This Tutorial I am going to create a simple Bullet sound if you click on the screen any where u will listen the bullet fired sound and if you long click on the screen the then a Machine Gun Sound.
OutPut Of the Tutorial:
Create Android Application:
- File >> New >> Android
Application
- Enter Project name: SoundStuffApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish
package com.ambilpursunil.newapp;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
public class SoundStuff extends Activity implements OnClickListener,
OnLongClickListener
{
SoundPool sp;
int explosion = 0;
MediaPlayer mp;
// new explosion = 0 bcz when we play the
sp.play explosion will take some
// time to load at the time we are getting erro
to over come this we first
// initialized explosion to 0
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View
v = new View(this);
v.setOnClickListener(this);
v.setOnLongClickListener(this);
setContentView(v);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
explosion = sp.load(this, R.raw.gunshot, 1);
mp = MediaPlayer.create(this, R.raw.beep);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (explosion != 0)
sp.play(explosion, 1, 1, 0, 0, 1);
/*
* 1)once we run this application,we get the
screen here we needt to
* click on the screen to get been sound 2)
Difference between SoundPool
* and MediaPlayer is soundPool is small piece
of sound which make
* effective sound where as mediaplayer
is used to play sound for long
* time
*/
}
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
mp.start();
return false;
}
}
Note: No need of activity_main.xml for this application
instead of that we create a view object and placed it in setContextView(v).
2.Make the change the of manifest.xml file as the below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ambilpursunil.newapp.SoundStuff"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
NOTE:
Create a raw folder and place sounds like bullet.mp3 or other .mp3 files
in raw folder
How to
create raw folder just right click on the res folder and choose folder and name
it as raw and place what ever you want but .mp3 only
4.Right
click on the project and Run As-->Android Application
OutPut
Explanation:
Not
Much to explain about this project output just click on the screen you
will listen bullet sound and if you long click then machine gun sound.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... Tabs and shop watch in Android:
No comments:
Post a Comment