Working with SoundPool Android
Hey Guys, Today we are going to see the usage of SoundPool in Android. So lets get started.
1) First of all create an Android Application Project.
2) Next go to "res/layout/activity_main.xml" and create a button there.
activity_main.xml
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
1) First of all create an Android Application Project.
2) Next go to "res/layout/activity_main.xml" and create a button there.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"3) Now go to "src/your_package_name/MainActivity.java" and the code is here.
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play"
android:id = "@+id/play"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
import android.app.Activity;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
SoundPool sp;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.play);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.play : playit();break;
}
}
private void playit() {
// TODO Auto-generated method stub
sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
final int i = sp.load(this, R.raw.music, 0);
sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
sp.play(i, 1,1,0,0,1.0f);
}
});
}
}
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
Comments
Post a Comment