Sound buttons stopped working after some time - java

I have problem with my android app. It's soundbar application.
After run, everything works perfectly. After some time and clicking on buttons, I have error:
E/MediaPlayer: start called in state 0, mPlayer(0x7d176f4300)
E/MediaPlayer: error (-38, 0)
I run this on my Samsung S7, I would ask for understanding becouse I am beginner. I saw similar topics but I can't find answer which helps me. Here is my code:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer AaaCoJeszcze = MediaPlayer.create(this, R.raw.aaa_co_jeszcze);
Button But1 = this.findViewById(R.id.Button1);
But1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AaaCoJeszcze.start();
}
});
final MediaPlayer CoNieMoznaMP = MediaPlayer.create(this, R.raw.co_nie_mozna);
Button But2 = this.findViewById(R.id.Button2);
But2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoNieMoznaMP.start();
}
});
final MediaPlayer CoToJaWinnyMP = MediaPlayer.create(this, R.raw.co_to_ja_winny);
Button But3 = this.findViewById(R.id.Button3);
But3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoToJaWinnyMP.start();
}
});
final MediaPlayer debilMP = MediaPlayer.create(this, R.raw.debil);
Button But4 = this.findViewById(R.id.Button4);
But4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
debilMP.start();
}
});
final MediaPlayer iTenMP = MediaPlayer.create(this, R.raw.i_ten_o_co_chodzi);
Button But5 = this.findViewById(R.id.Button5);
But5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iTenMP.start();
}
});
final MediaPlayer JedziemyMP = MediaPlayer.create(this, R.raw.jedziemy_z_kurwami);
Button But6 = this.findViewById(R.id.Button6);
But6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JedziemyMP.start();
}
});
final MediaPlayer JegoMP = MediaPlayer.create(this, R.raw.jego_robota_moze_poczekac);
Button But7 = this.findViewById(R.id.Button7);
But7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JegoMP.start();
}
});
final MediaPlayer ZanimMP = MediaPlayer.create(this, R.raw.zanim_podeszlem);
Button But8 = this.findViewById(R.id.Button8);
But8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ZanimMP.start();
}
});
final MediaPlayer MozePanOdejscMP = MediaPlayer.create(this, R.raw.moze_pan_odejsc);
Button But9 = this.findViewById(R.id.Button9);
But9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozePanOdejscMP.start();
}
});
final MediaPlayer MozeZrobilPomylkeMP = MediaPlayer.create(this, R.raw.moze_zrobil_pomylke);
Button But10 = this.findViewById(R.id.Button10);
But10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeZrobilPomylkeMP.start();
}
});
final MediaPlayer MowiszTakTakMP = MediaPlayer.create(this, R.raw.mowisz_tak_tak);
Button But11 = this.findViewById(R.id.Button11);
But11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MowiszTakTakMP.start();
}
});
final MediaPlayer PaniMnieMP = MediaPlayer.create(this, R.raw.pani_mnie_rozprasza);
Button But12 = this.findViewById(R.id.Button12);
But12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PaniMnieMP.start();
}
});
final MediaPlayer PrawdopodobnieMP = MediaPlayer.create(this, R.raw.prawdopodobnie_polak);
Button But13 = this.findViewById(R.id.Button13);
But13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrawdopodobnieMP.start();
}
});
final MediaPlayer ProszeNiePrzeszkadzacMP = MediaPlayer.create(this, R.raw.prosze_nie_przeszkadzac_dobrze);
Button But14 = this.findViewById(R.id.Button14);
But14.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProszeNiePrzeszkadzacMP.start();
}
});
final MediaPlayer PrzepraszamPaniaMP = MediaPlayer.create(this, R.raw.przepraszam_pania_spoczko);
Button But15 = this.findViewById(R.id.Button15);
But15.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrzepraszamPaniaMP.start();
}
});
final MediaPlayer RozpraszaMP = MediaPlayer.create(this, R.raw.rozprasza_mnie);
Button But16 = this.findViewById(R.id.Button16);
But16.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozpraszaMP.start();
}
});
final MediaPlayer RozumiszMP = MediaPlayer.create(this, R.raw.rozumisz);
Button But17 = this.findViewById(R.id.Button17);
But17.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozumiszMP.start();
}
});
final MediaPlayer spoczkoMP = MediaPlayer.create(this, R.raw.spoczko);
Button But18 = this.findViewById(R.id.Button18);
But18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spoczkoMP.start();
}
});
final MediaPlayer SzefiemozeszMP = MediaPlayer.create(this, R.raw.szefie_mozesz_odejsc);
Button But19 = this.findViewById(R.id.Button19);
But19.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SzefiemozeszMP.start();
}
});
final MediaPlayer AleodejdzMP = MediaPlayer.create(this, R.raw.szefie_odejdz_ale_odejdz);
Button But20 = this.findViewById(R.id.Button20);
But20.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AleodejdzMP.start();
}
});
final MediaPlayer MozeMP = MediaPlayer.create(this, R.raw.moze_moze_moze);
Button But21 = this.findViewById(R.id.Button21);
But21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeMP.start();
}
});
final MediaPlayer RekoczynyMP = MediaPlayer.create(this, R.raw.zadnych_rekoczynow);
Button But22 = this.findViewById(R.id.Button22);
But22.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RekoczynyMP.start();
}
});
final MediaPlayer PowinnoDojscMP = MediaPlayer.create(this, R.raw.juz_powinno_dojsc);
Button But23 = this.findViewById(R.id.Button23);
But23.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PowinnoDojscMP.start();
}
});
}}

Related

Media Player android

I'm struggling on designing a little audio player.
Just a list of 4 songs - each has it's own textView and when a user clicks on a textView a song starts playing but i have no idea how to make this. I though about making a onClick method for each textView but really dont like this idea.
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
/* MediaPlayer hell = MediaPlayer.create(this, R.raw.Hell);
MediaPlayer bells = MediaPlayer.create(this, R.raw.HellsBells);
MediaPlayer shoot = MediaPlayer.create(this, R.raw.Shoot);
MediaPlayer thunder = MediaPlayer.create(this, R.raw.Thunderstruck);*/
public void playMusic (View view) {
varMediaPlayer = MediaPlayer.create(this, R.raw.Hell);
varMediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the following variables:
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
will make your app crash as your assigning the variables when the MainActivity object is created.
move those to onCreate(Bundle) and assign a click listener to each of those TextViews
private static final int RAW_MUSIC_1 = R.raw.Hell;
private static final int RAW_MUSIC_2 = R.raw.HellsBells;
private static final int RAW_MUSIC_3 = R.raw.Shoot;
private static final int RAW_MUSIC_4 = R.raw.Thunderstruck;
private MediaPlayer varMediaPlayer;
private TextView song1, song2, song3, song4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign after setContentView(...) as findViewById scan the content view layout
song1 = (TextView) findViewById(R.id.song1);
song2 = (TextView) findViewById(R.id.song2);
song3 = (TextView) findViewById(R.id.song3);
song4 = (TextView) findViewById(R.id.song4);
// add click listeners
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_1);
}
};
song2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_2);
}
};
song3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_3);
}
};
song4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_4);
}
};
}
and change playMusic(View) method to playMusic(int) and add release and reset methods to release object from memory
private void playMusic(final int musicId) {
try {
if (mediaPlayer.isPlaying()) mediaPlayer.reset();
} catch (final Exception e) {}
try {
mediaPlayer.release();
} catch (final Exception e) {}
mediaPlayer = MediaPlayer.create(this, id);
mediaPlayer.start();
}
Replace your code with this one and try running this...
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
MediaPlayer mediaPlayer = new MediaPlayer();
public void playMusic (Integer id) {
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer = MediaPlayer.create(this , id);
mediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.hell);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.HellsBells);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Shoot);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Thunderstruck);
}
});
}
}

android mediaplayer pause not working

I have a problem with pausing a MediaPlayer. When I play it, and click the pause button it's working, but when it's playing longer than 10 seconds it's not pausing.
Here is my code:
private TextView tvState;
private MediaPlayer mediaPlayer = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_player);
ImageButton btnPlay = (ImageButton) findViewById(R.id.btnPlay);
ImageButton btnPause = (ImageButton) findViewById(R.id.btnPause);
TextView tvAuthor = (TextView) findViewById(R.id.tvAuthor);
TextView tvSong = (TextView) findViewById(R.id.tvSong);
tvState = (TextView) findViewById(R.id.tvState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String json = bundle.getString("songModel");
final SongModel songModel = new Gson().fromJson(json, SongModel.class);
tvAuthor.setText(songModel.getAuthor());
tvSong.setText(songModel.getName());
PrepareMediaPlayer(songModel);
}
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
if (mediaPlayer.isPlaying()) {
tvState.setText("Played");
}
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
if (!mediaPlayer.isPlaying()) {
tvState.setText("Paused");
}
}
});
}
public void PrepareMediaPlayer(SongModel song) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(song.getLink());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (!mp.isPlaying()) {
mp.start();
}
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
}
My OS: Android 4.0.4, API 15

my android app works very slow

Hello i'm creating an app to scan buisness cards, it has lot of views but one works very slow it has 19 buttons. Is it possible that slow working of my app is caused by this number of buttons? I've tried to put listeners outside of onCreate but it didn't help. This is my java file conected to this view:
public class Szczegoly extends Activity {
long contactID;
Bitmap contactFoto;
int imagebig=0;
int w;
Contact contact;
EditText tv1;
EditText tv2;
EditText tv3;
EditText tv4;
EditText tv5;
ImageView i;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b10;
Context to;
Button accept1;
Button accept2;
Button accept3;
Button accept4;
Button accept5;
Button cancel1;
Button cancel2;
Button cancel3;
Button cancel4;
Button cancel5;
Context context;
OnClickListener accept1listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==1)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept2listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==2)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept3listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==3)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept4listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==4)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept5listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==5)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener cancel1listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel2listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel3listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel4listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel5listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener b1lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
finish();
}
}
};
OnClickListener b2lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
contact.delete();
finish();
}
}
};
OnClickListener b3lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
}
}
};
OnClickListener b4lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
Uri call = Uri.parse("tel:"+contact.Phone1);
Intent intent = new Intent(Intent.ACTION_CALL, call);
startActivity(intent);
}
}
};
OnClickListener b5lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
}
}
};
OnClickListener b6lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
if(tv1.isEnabled())
{
tv1.setSelected(false);
tv1.setEnabled(false);
}
else
{
cancel1.setVisibility(Button.VISIBLE);
accept1.setVisibility(Button.VISIBLE);
b6.setVisibility(Button.INVISIBLE);
tv1.setEnabled(true);
tv1.setSelected(true);
tv1.setFocusableInTouchMode(true);
tv1.requestFocus();
tv1.setSelection(tv1.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 1;
}
}
};
OnClickListener b7lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
if(tv2.isEnabled())
tv2.setEnabled(false);
else
{
cancel2.setVisibility(Button.VISIBLE);
accept2.setVisibility(Button.VISIBLE);
b7.setVisibility(Button.INVISIBLE);
tv2.setEnabled(true);
tv2.setSelected(true);
tv2.setFocusableInTouchMode(true);
tv2.requestFocus();
tv2.setSelection(tv2.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 2;
}
}
};
OnClickListener b8lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr == 0)
{
if(tv3.isEnabled())
tv3.setEnabled(false);
else
{
cancel3.setVisibility(Button.VISIBLE);
accept3.setVisibility(Button.VISIBLE);
b8.setVisibility(Button.INVISIBLE);
tv3.setEnabled(true);
tv3.setEnabled(true);
tv3.setSelected(true);
tv3.setFocusableInTouchMode(true);
tv3.requestFocus();
tv3.setSelection(tv3.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr=3;
}
}
};
OnClickListener b9lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0 )
{
if(tv4.isEnabled())
tv4.setEnabled(false);
else
{
cancel4.setVisibility(Button.VISIBLE);
accept4.setVisibility(Button.VISIBLE);
b9.setVisibility(Button.INVISIBLE);
tv4.setEnabled(true);
tv4.setEnabled(true);
tv4.setSelected(true);
tv4.setFocusableInTouchMode(true);
tv4.requestFocus();
tv4.setSelection(tv4.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 4;
}
}
};
OnClickListener b10lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr==0)
{
if(tv5.isEnabled())
tv5.setEnabled(false);
else
{
cancel5.setVisibility(Button.VISIBLE);
accept5.setVisibility(Button.VISIBLE);
b10.setVisibility(Button.INVISIBLE);
tv5.setEnabled(true);
tv5.setEnabled(true);
tv5.setSelected(true);
tv5.setFocusableInTouchMode(true);
tv5.requestFocus();
tv5.setSelection(tv5.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null)
{
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 5;
}
}
};
int edit_nr=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_szczegoly);
Log.d("dupa","dupa1");
context = this;
Intent intent = getIntent();
contactID = intent.getLongExtra("contactID", 0);
contact = new Contact(contactID, this);
tv1 = (EditText) findViewById(R.id.editText1);
tv2 = (EditText) findViewById(R.id.editText2);
tv3 = (EditText) findViewById(R.id.editText3);
tv4 = (EditText) findViewById(R.id.editText4);
tv5 = (EditText) findViewById(R.id.editText5);
i = (ImageView) findViewById(R.id.imageView1);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
b10 = (Button) findViewById(R.id.button10);
tv1.setText(contact.FirstName);
tv2.setText(contact.LastName);
tv3.setText(contact.Phone1);
tv4.setText(contact.Phone2);
tv5.setText(contact.EmaiL);
accept1 = (Button) findViewById(R.id.accept1);
accept2 = (Button) findViewById(R.id.accept2);
accept3 = (Button) findViewById(R.id.accept3);
accept4 = (Button) findViewById(R.id.accept4);
accept5 = (Button) findViewById(R.id.accept5);
cancel1 =(Button) findViewById(R.id.cancel1);
cancel2 =(Button) findViewById(R.id.cancel2);
cancel3 =(Button) findViewById(R.id.cancel3);
cancel4 =(Button) findViewById(R.id.cancel4);
cancel5 =(Button) findViewById(R.id.cancel5);
}
public void closeEdit()
{
tv1.setSelected(false);
tv1.setEnabled(false);
tv2.setSelected(false);
tv2.setEnabled(false);
tv3.setSelected(false);
tv3.setEnabled(false);
tv4.setSelected(false);
tv4.setEnabled(false);
tv5.setSelected(false);
tv5.setEnabled(false);
cancel1.setVisibility(Button.INVISIBLE);
accept1.setVisibility(Button.INVISIBLE);
b6.setVisibility(Button.VISIBLE);
cancel2.setVisibility(Button.INVISIBLE);
accept2.setVisibility(Button.INVISIBLE);
b7.setVisibility(Button.VISIBLE);
cancel3.setVisibility(Button.INVISIBLE);
accept3.setVisibility(Button.INVISIBLE);
b8.setVisibility(Button.VISIBLE);
cancel4.setVisibility(Button.INVISIBLE);
accept4.setVisibility(Button.INVISIBLE);
b9.setVisibility(Button.VISIBLE);
cancel5.setVisibility(Button.INVISIBLE);
accept5.setVisibility(Button.INVISIBLE);
b10.setVisibility(Button.VISIBLE);
edit_nr=0;
}
public void onResume() {
super.onResume();
Log.d("dupa","dupa");
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(size);
to = this;
w = size.x;
contactFoto = Bitmap.createScaledBitmap(contact.photo, w, (int) (w*contact.photo.getHeight()/contact.photo.getWidth()), true);
i.setImageBitmap(contactFoto);
accept1.setOnClickListener(accept1listener);
accept2.setOnClickListener(accept2listener);
accept3.setOnClickListener(accept3listener);
accept4.setOnClickListener(accept4listener);
accept5.setOnClickListener(accept5listener);
cancel1.setOnClickListener(cancel1listener);
cancel2.setOnClickListener(cancel2listener);
cancel3.setOnClickListener(cancel3listener);
cancel4.setOnClickListener(cancel4listener);
cancel5.setOnClickListener(cancel5listener);
b1.setOnClickListener(b1lisner);
b2.setOnClickListener(b2lisner);
b3.setOnClickListener(b3lisner);
b4.setOnClickListener(b4lisner);
b5.setOnClickListener(b5lisner);
b6.setOnClickListener(b6lisner);
b7.setOnClickListener(b7lisner);
b8.setOnClickListener(b8lisner);
b9.setOnClickListener(b9lisner);
b10.setOnClickListener(b10lisner);
}
}
The number of buttons by itself is not a likely cause of slowness. However, from the code it seems as if many actions from listeners (ie contact.savedata_nr) might be running on the main/UI thread which could cause slowness, depending on what happens when these methods are called. As mentioned in the comment, creating Bitmap might be better task to be carried out in the background as well.
To fix this, you should review your code and see what can be run in the background thread instead. Operations like saving can be done on the background thread, and then any updates to the GUI after these should be carried out on UI thread. If the background operation takes a long time, it's a good idea to up a progress indicator to show the user that something is happening and that the screen isn't just stuck.
You can read about Android threading here.
The basic example would be:
new Thread(new Runnable()
{
public void run()
{
//put background code here
contact.save(...)
//after background task is finished, update the UI
runOnUiThread(new Runnable()
{
#Override
public void run()
{
//put UI code here
textview.setText(...);
}
});
}
}).start();

Error setonclickListener undefined for the type button

Hello i'm getting the error:
the method setonclickListener(new onclickListener(){}) is undefined
for the type button
Here's my code:
public class Sliding extends Activity {
/** Called when the activity is first created. */
private LinearLayout MenuList;
private Button btnToggleMenuList;
private int screenWidth;
private boolean isExpanded;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding);
MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
btnToggleMenuList = (Button) findViewById(R.id.button1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
if (isExpanded) {
isExpanded = false;
MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}else {
isExpanded = true;
MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}
}
});
}
}
can anyone tells me why?
Replace this
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
By
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Also make sure you have the below import statement
import android.view.View.OnClickListener;
Change it to
View.OnClickListener()
Write this:
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rather than:
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
Try this:
btnToggleMenuList.setonclickListener(new onclickListener() {
#Override
public void onclick(View v) {

How to stop already playing sound and immediately start another

When a sound that the user previously selected is playing, how can I stop that sound and immediately play the new selected sound? The code I have so far only stops the new sound, but the user has to select the sound again in order to play it.
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.secondsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
I am making a soundboard with over 30 sounds, if that helps at all. What (slight I assume) changes would I need to make to my code? Any help would be greatly appreciated!
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
public Boolean playing=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
}
}

Categories