How to always check a condition in Android? - java

I want to develop an app to toggle the device's audio status. If the audio status is silent then my button's text should be "silent", if it's normal the text should be "normal"
Here is my only class:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1= (Button) findViewById(R.id.btn1);
final AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
btn1.setText("Normal");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
btn1.setText("Silent");
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
btn1.setText("Normal");
}
}
});
}
});
How can I always check this condition in Android ?

If you need to get the mode of current ringer even if it was changed outside from you application context then you should register a BroadcastReceiver for AudioManager.RINGER_MODE_CHANGED_ACTION.
This receiver will be called everytime someone changes the ringer mode.
Do this inside onCreate.
BroadcastReceiver receiver=new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
btn1.setText("Normal");
}
}
};
IntentFilter filter=new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(receiver,filter);

Just do this
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
btn1.setText("Normal");
}
}
});
what your are doing is
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
btn1.setText("Normal");
}
}
});
}
});

Related

setOnClickListener button doesn't work Android studio

I'm new in java and I have tried to make a food app in android studio. I have a bar in app and when I click on cart my app is crashing.
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter, adapter2;
private RecyclerView recyclerViewCategotyList, recyclerViewPopularList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewCategoty();
recyclerViewPopular();
bottomNavigation();
}
private void bottomNavigation() {
LinearLayout homeBtn=findViewById(R.id.homeBtn);
LinearLayout cartBtn=findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,CartActivity.class));
}
});
}
And I have this error: at com.example.food.Activity.MainActivity$2.onClick(MainActivity.java:48)
This is CartActivity
public class CartActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter;
private RecyclerView recyclerViewList;
private ManagementCart managementCart;
private TextView totalFeeTxt, taxTxt, deliveryTxt, totalTxt, emptyTxt;
private double tax;
private ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
managementCart = new ManagementCart(this);
initView();
initList();
bottomNavigation();
calculateCard();
}
private void bottomNavigation() {
LinearLayout homeBtn = findViewById(R.id.homeBtn);
LinearLayout cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}
I don't know what's wrong and why my setOnClickListener doesn't work.
Thank you.
I think your button R.id.homeBtn is not LinearLayout, can you show your xml file?
If your buttons are Button classes, try this
private void bottomNavigation() {
Button homeBtn = findViewById(R.id.homeBtn);
Button cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}

Want to show interstitial ads on every button clicked but ad show on one button

This is my code for main_activity.i want to implement ads on every button visible on screen and when clicked interstital ad should show. But it only shows ad on btn_button.Kindly guide me how to resolve this issue.
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private InterstitialAd mInterstitialAd2;
Button btn_button,btn_button2,btn_button3,btn_button4,btn_button5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}});
btn_button=(Button) findViewById(R.id.button);
btn_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i3=new Intent(MainActivity.this,Button.class);
startActivity(i3);
}
});
btn_button2=(Button) findViewById(R.id.button);
btn_button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i2=new Intent(MainActivity.this,Button.class);
startActivity(i2);
}
});
btn_button3=(Button) findViewById(R.id.button);
btn_button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Button.class);
startActivity(i);
}
});
btn_button4=(Button) findViewById(R.id.button);
btn_button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i4=new Intent(MainActivity.this,Button.class);
startActivity(i4);
}
});
btn_button5=(Button) findViewById(R.id.button);
btn_button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i5=new Intent(MainActivity.this,Button.class);
startActivity(i5);
}
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
btn_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
mInterstitialAd2 = new InterstitialAd(this);
mInterstitialAd2.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd2.loadAd(new AdRequest.Builder().build());
btn_button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd2.isLoaded()) {
mInterstitialAd2.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
}
}
You have same button id, that's why it is showing one only one button as #Anon mentioned.
But actually you don't need so many interstitial instances as you are only using one ad unit. Only one instance is sufficient to show ad on every button clicked.
BUT... your AdMob account will be disabled very soon if you try to show ad on every click.
Every button you are using refers to the same button id, so you have only one button working. =)
'btn_button5=(Button) findViewById(R.id.button);
btn_button4=(Button) findViewById(R.id.button);'

Pass a XML resource from one activity to another activity using java code in Android Studio?

I want to pass an XML resource from one activity to another activity using Java Code? I don't want to create separate different activities for different buttons.
ImageButton imageBttn = (ImageButton)findViewById(R.id.imageButton1);
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, info.class));
}
});
Pseudocode to explain what I'm trying to do:
If BUTTON_1 is clicked
-Pass swirl.png to info.class
If BUTTON_2 is clicked
-Pass golden.png to info.class
If BUTTON_3 is clicked
-Pass arcade.png
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, info.class)
intent.putExtra("image",R.drawable.ic_search_grey);
startActivity(intent);
}
});
On Mainactvitiy get the int drawable
int res = getIntent().getIntExtra("image", -1);
if(res > -1) {
Drawable drawable = getResources().getDrawable(res, null);
imgBtn.setImageDrawable(drawable);
}
InfoActivity.java
public class InfoActivity extends Activity {
private static final String EXTRA_IMAGE = "image";
public static void launch(Activity activity, #DrawableRes int imageResId) {
Intent intent = new Intent(activity, InfoActivity.class);
intent.putExtra(EXTRA_IMAGE, imageResId);
activity.startActivity(intent);
}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
int imageResId = getIntent().getIntExtra(EXTRA_IMAGE, -1);
if (imageResId == -1) {
throw new IllegalArgumentException(EXTRA_IMAGE);
// or set error/default image resource id
}
// ... something to do with imageResId
}
}
MainActivity.java
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.swirl);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.golden);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.arcade);
}
});
}

How to launch google calendar from the app I'm developing

I'm developing an APP for a schoolwork. When I launch my app, I configured 4 buttons, one of them is a calendar.
What I want is when I click on Calendar, this open the Google calendar; or if I don't have installed it, take you to Google Play.
The other buttons are activities in the same application, but these are solved.
The main activity:
public class MainActivity extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
entrarboton();
}
private void entrarboton() {
ImageButton accionentrar = (ImageButton) findViewById(R.id.imageButton0);
accionentrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Calendari.class));
}
});
ImageButton accionentrar2 = (ImageButton)findViewById(R.id.imageButton3);
accionentrar2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Notes.class));
}
});
ImageButton accionentrar3 = (ImageButton)findViewById(R.id.imageButton2);
accionentrar3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Apunts.class));
}
});
ImageButton accionentrar4 = (ImageButton)findViewById(R.id.imageButton4);
accionentrar4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Altres.class));
}
});
}
}

Music stops after 20 seconds (android soundboard) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have made my own android soundboard.
Now i have only 1 problem
I have a song Intro , but it is about 38 seconds.
When i am trying to play intro i only hear for about 20 seconds.
can someone help me ?
SoundBoard.java
package com.soundboard;
import com.soundboard.SoundManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
mSoundManager.addSound(4, R.raw.sound4);
mSoundManager.addSound(5, R.raw.sound5);
mSoundManager.addSound(6, R.raw.sound6);
mSoundManager.addSound(7, R.raw.sound7);
mSoundManager.addSound(8, R.raw.sound8);
mSoundManager.addSound(9, R.raw.sound9);
mSoundManager.addSound(10, R.raw.sound10);
mSoundManager.addSound(11, R.raw.sound11);
mSoundManager.addSound(12, R.raw.sound12);
mSoundManager.addSound(13, R.raw.sound13);
mSoundManager.addSound(14, R.raw.sound14);
mSoundManager.addSound(15, R.raw.sound15);
mSoundManager.addSound(16, R.raw.sound16);
mSoundManager.addSound(17, R.raw.sound17);
mSoundManager.addSound(18, R.raw.sound18);
mSoundManager.addSound(19, R.raw.sound19);
mSoundManager.addSound(20, R.raw.sound20);
mSoundManager.addSound(21, R.raw.sound21);
mSoundManager.addSound(22, R.raw.sound22);
mSoundManager.addSound(23, R.raw.sound23);
mSoundManager.addSound(24, R.raw.sound24);
mSoundManager.addSound(25, R.raw.sound25);
mSoundManager.addSound(26, R.raw.sound26);
mSoundManager.addSound(27, R.raw.sound27);
mSoundManager.addSound(28, R.raw.sound28);
mSoundManager.addSound(29, R.raw.sound29);
mSoundManager.addSound(30, R.raw.sound30);
mSoundManager.addSound(31, R.raw.sound31);
mSoundManager.addSound(32, R.raw.sound32);
mSoundManager.addSound(33, R.raw.sound33);
mSoundManager.addSound(34, R.raw.sound34);
mSoundManager.addSound(35, R.raw.sound35);
mSoundManager.addSound(36, R.raw.sound36);
Button SoundButton1 = (Button)findViewById(R.id.sound1);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button SoundButton2 = (Button)findViewById(R.id.sound2);
SoundButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
Button SoundButton3 = (Button)findViewById(R.id.sound3);
SoundButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(3);
}
});
Button SoundButton4 = (Button)findViewById(R.id.sound4);
SoundButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(4);
}
});
Button SoundButton5 = (Button)findViewById(R.id.sound5);
SoundButton5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(5);
}
});
Button SoundButton6 = (Button)findViewById(R.id.sound6);
SoundButton6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(6);
}
});
Button SoundButton7 = (Button)findViewById(R.id.sound7);
SoundButton7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(7);
}
});
Button SoundButton8 = (Button)findViewById(R.id.sound8);
SoundButton8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(8);
}
});
Button SoundButton9 = (Button)findViewById(R.id.sound9);
SoundButton9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(9);
}
});
Button SoundButton10 = (Button)findViewById(R.id.sound10);
SoundButton10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(10);
}
});
Button SoundButton11 = (Button)findViewById(R.id.sound11);
SoundButton11.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(11);
}
});
Button SoundButton12 = (Button)findViewById(R.id.sound12);
SoundButton12.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(12);
}
});
Button SoundButton13 = (Button)findViewById(R.id.sound13);
SoundButton13.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(13);
}
});
Button SoundButton14 = (Button)findViewById(R.id.sound14);
SoundButton14.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(14);
}
});
Button SoundButton15 = (Button)findViewById(R.id.sound15);
SoundButton15.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(15);
}
});
Button SoundButton16 = (Button)findViewById(R.id.sound16);
SoundButton16.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(16);
}
});
Button SoundButton17 = (Button)findViewById(R.id.sound17);
SoundButton17.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(17);
}
});
Button SoundButton18 = (Button)findViewById(R.id.sound18);
SoundButton18.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(18);
}
});
Button SoundButton19 = (Button)findViewById(R.id.sound19);
SoundButton19.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(19);
}
});
Button SoundButton20 = (Button)findViewById(R.id.sound20);
SoundButton20.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(20);
}
});
Button SoundButton21 = (Button)findViewById(R.id.sound21);
SoundButton21.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(21);
}
});
Button SoundButton22 = (Button)findViewById(R.id.sound22);
SoundButton22.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(22);
}
});
Button SoundButton23 = (Button)findViewById(R.id.sound23);
SoundButton23.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(23);
}
});
Button SoundButton24 = (Button)findViewById(R.id.sound24);
SoundButton24.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(24);
}
});
Button SoundButton25 = (Button)findViewById(R.id.sound25);
SoundButton25.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(25);
}
});
Button SoundButton26 = (Button)findViewById(R.id.sound26);
SoundButton26.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(26);
}
});
Button SoundButton27 = (Button)findViewById(R.id.sound27);
SoundButton27.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(27);
}
});
Button SoundButton28 = (Button)findViewById(R.id.sound28);
SoundButton28.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(28);
}
});
Button SoundButton29 = (Button)findViewById(R.id.sound29);
SoundButton29.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(29);
}
});
Button SoundButton30 = (Button)findViewById(R.id.sound30);
SoundButton30.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(30);
}
});
Button SoundButton31 = (Button)findViewById(R.id.sound31);
SoundButton31.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(31);
}
});
Button SoundButton32 = (Button)findViewById(R.id.sound32);
SoundButton32.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(32);
}
});
Button SoundButton33 = (Button)findViewById(R.id.sound33);
SoundButton33.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(33);
}
});
Button SoundButton34 = (Button)findViewById(R.id.sound34);
SoundButton34.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(34);
}
});
Button SoundButton35 = (Button)findViewById(R.id.sound35);
SoundButton35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(35);
}
});
Button SoundButton36 = (Button)findViewById(R.id.sound36);
SoundButton36.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(36);
}
});
}
}
SoundManager.java
package com.soundboard;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
}
Thank you
Is there any specific reason why you're using Soundpool instead of MediaPlayer?
MediaPlayer is much more elegant for this type of media playback.
If you're doing that on the main thread and not in a service (recommended for music playback of that length) or a background thread you could be locking up the event thread for too long. Just a thought.

Categories

Resources