Play Pause single button - java

I am trying to implement play/pause in a single button however when i try to press it again it just stars the song again overlapping the sound playing. i have search ways and tried this method and it seems it is not working for me. thank you
private OnClickListener handler1 = new OnClickListener(){
public void onClick(View v){
MediaPlayer mpE = MediaPlayer.create(GuitarTuner.this, R.raw.test2 );
if (mpE.isPlaying()) {
mpE.pause();
} else {
mpE.start();
}
}
};"

You are creating the the media player every time. Instead try moving
MediaPlayer mpE = MediaPlayer.create(GuitarTuner.this, R.raw.test2 );
to outside of the handler.

MediaPlayer mpE = MediaPlayer.create(GuitarTuner.this, R.raw.test2 );
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mpE.isPlaying()) {
mpE.pause();
play.setBackgroundResource(R.drawable.play);
} else {
mpE.start();
play.setBackgroundResource(R.drawable.pause);
}
}
});

Related

Not Allowing Multiple Button Presses

I'm building a camera app with Camera2 API, relatively new to Android development. Everything is working just working out the bugs. But I have a switch camera button, going from the front camera to the back or vise-versa. If the user continuosly presses the button, the app will crash. Trying to set it up in a way that it finishes everything it needs to do before the button can be used again.
I have the button set to enabled, but after press, it disables the button until everyting finishes, then renables, but that doesn't seem to work:
//The button to switch the camera to front and back camera.
mChangeCamera = (ImageButton) findViewById(R.id.change_camera);
mChangeCamera.setEnabled(true);
mChangeCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mChangeCamera.setEnabled(false);
closeCamera();
// stopBackgroundthread();
if (mTextureView.isAvailable()) {
setUpCamera(mTextureView.getWidth(), mTextureView.getHeight());
transformImage(mTextureView.getWidth(), mTextureView.getHeight());
connectCamera();
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
mChangeCamera.setEnabled(true);
}
});
there has to be a simple way to do this, but not finding anyting from searchs. Anyone know how I can set it up not to crash when the user smashes the button?
Alight, so I ended up figuring out how to do this. You can use a handler with a post delay like so:
mChangeCamera = (ImageButton) findViewById(R.id.change_camera);
mChangeCamera.setEnabled(true);
mChangeCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mChangeCamera.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mChangeCamera.setEnabled(true);
}
},1000);
closeCamera();
// stopBackgroundthread();
if (mTextureView.isAvailable()) {
setUpCamera(mTextureView.getWidth(), mTextureView.getHeight());
transformImage(mTextureView.getWidth(), mTextureView.getHeight());
connectCamera();
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
});
i did this by calculate time between multiple button presses...
create global variable in your class first
private long mLastClickTime = 0;
and a final int for duration between clicks
public static final int CLICK_TIME = 400;
then paste this code in on click of button
if (SystemClock.elapsedRealtime() - mLastClickTime < CLICK_TIME) {
return; //button pressed repeatedly so do nothing
}
mLastClickTime = SystemClock.elapsedRealtime();
// button is not pressed repeatedly so add your desired action
and of course you can increase the CLICK_TIME value if error still occurs

How to disable momentarily onClick event of ImageView and Button in Android Studio

I have 2 elements:
Button answer;
Imageview play;
When 'answer' is clicked - the app records the user.
When 'play' is clicked - the app plays a clue to the user.
So when one of the above is clicked, the other should be disabled and not allow an onclick event.
Problem is - I've tried both setClickable() and setEnabled() but they don't do what I expected them to do.
Also, I've tried using a boolean flag, but it also didn't do the trick.
What am I doing wrong?
Here is the attempt with the boolean flag:
answer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsAudioResourcesFree)
{
mIsAudioResourcesFree = false;
//RECORDS AND STOPS RECORDING
mIsAudioResourcesFree = true;
}
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsAudioResourcesFree)
{
mIsAudioResourcesFree = false;
//PLAYS SOME SHORT AUDIO
mIsAudioResourcesFree = true;
}
}
});

How can I not have to wait for the MediaPlayer to finish in Android Studio?

I'm currently using a MediaPlayer to play a 1 second sound file. However, I can't replay it right afterwards; I have to wait a while, which I don't want to do.
I want to be able to play the sound file right after I click the View.
Here's my code:
final TextView lowBongo = (TextView)this.findViewById(R.id.low_bongo);
final MediaPlayer lowBongoMP = MediaPlayer.create(this, R.raw.low_bongo_sound);
lowBongo.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
lowBongoMP.start();
}
});
final TextView lowBongo = (TextView)this.findViewById(R.id.low_bongo);
final MediaPlayer lowBongoMP = MediaPlayer.create(this, R.raw.low_bongo_sound);
lowBongo.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
lowBongoMP.seekTo(0); //Start Song From 0 second
lowBongoMP.start();
}
});
Stop whenever the song is about to play
final TextView lowBongo = (TextView)this.findViewById(R.id.low_bongo);
final MediaPlayer lowBongoMP = MediaPlayer.create(this, R.raw.low_bongo_sound);
lowBongo.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
lowBongoMP.stop(); //stop current song
lowBongoMP.start();
}
});

Android MediaPlayer start called in State 1. Error -38,0

Good day! As usual - I am starting to learn Android programming and have faced unexpected difficulty while trying to create basic MediaPlayer app. Audio file is stored in res/raw. It is accessed with create(). I have read a few manuals on how to build a media player app and was convinced that using prepare() and prepareAsync() was not necessary in case if file is stored in res/raw folder. So here is my initial code
private MediaPlayer playerM = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button playButton = (Button) findViewById(R.id.playButton);
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.sleep_away);
}
});
Button stopButton = (Button) findViewById(R.id.pauseButton);
stopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopMusic();
}
});
}
private void playMusic(int rid) {
playerM.create(this, rid);
playerM.start();
}
private void stopMusic() {
playerM.stop();
}
}
Since that I have made numerous changes, but nothing helped. According to dev.android manual on MediaPlayer the code above should work. However it results in the following errors:
E/MediaPlayer: start called in state 1, mPlayer(0x0)
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)
Probably I am just making some blunt mistake. Any help would be much appreciated.
Solved by myself
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
Uri myUri = Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + R.raw.sleep_away);
System.out.println(myUri);
playerM.setDataSource(v.getContext(), myUri);
playerM.prepare();
playerM.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer playerM){
playerM.start();
}
});
}catch(IOException e){
e.printStackTrace();
}
}
});
I know I should add other methods to change State, but the main purpose was to get it to play file.

Array Image and Audio change by clicking on "Next" and "Previous" button in Android

I am making simple on Android and it has 99 Images and 99 Audio Sounds and I created separate array for both of them. I want when I click on next button then "R.drawable.p1" image appear and produce "R.raw.a1" sound and again when I click on next button then "R.drawable.p2" image appear and produce "R.raw.a2 sound". In this case my Images Array is working perfectly as I want but problem is that my audio is not working correct, my next button audio is working well but when I come back to previous button then it produce firstly next button sound and then come to the previous. I want like image1=sound1, image2=sound2, image3=sound3 but problem is that I clicking on Previous button its look like this image2=sound3, image1=sound2.
Secondly, one more problem is that when one loop is completed then it muted all the sounds, why?
If you know any method, any if-else OR Switch-case logic then plz reply me, any help will be appreciated.
This is my code:
public class MainActivity extends Activity {
private ImageView hImageViewPic;
private Button iButton, gButton;
MediaPlayer ourSong;
private int currentImage = 0;
public int currentAudio = 0;
int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11,
R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15,
R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19,
R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23,
R.drawable.p24, R.drawable.p25, R.drawable.p26, R.drawable.p27,
R.drawable.p28, R.drawable.p29, R.drawable.p30, R.drawable.p31,
R.drawable.p32, R.drawable.p33, R.drawable.p34, R.drawable.p35,
R.drawable.p36, R.drawable.p37, R.drawable.p38, R.drawable.p39,
R.drawable.p40, R.drawable.p41, R.drawable.p42, R.drawable.p43,
R.drawable.p44, R.drawable.p45, R.drawable.p46, R.drawable.p47,
R.drawable.p48, R.drawable.p49, R.drawable.p50, R.drawable.p51,
R.drawable.p52, R.drawable.p53, R.drawable.p54, R.drawable.p55,
R.drawable.p56, R.drawable.p57, R.drawable.p58, R.drawable.p59,
R.drawable.p60, R.drawable.p61, R.drawable.p62, R.drawable.p63,
R.drawable.p64, R.drawable.p65, R.drawable.p66, R.drawable.p67,
R.drawable.p68, R.drawable.p69, R.drawable.p70, R.drawable.p71,
R.drawable.p72, R.drawable.p73, R.drawable.p74, R.drawable.p75,
R.drawable.p76, R.drawable.p77, R.drawable.p78, R.drawable.p79,
R.drawable.p80, R.drawable.p81, R.drawable.p82, R.drawable.p83,
R.drawable.p84, R.drawable.p85, R.drawable.p86, R.drawable.p87,
R.drawable.p88, R.drawable.p89, R.drawable.p90, R.drawable.p91,
R.drawable.p92, R.drawable.p93, R.drawable.p94, R.drawable.p95,
R.drawable.p96, R.drawable.p97, R.drawable.p98, R.drawable.p99 };
int[] audios = { R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4, R.raw.a5,
R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10, R.raw.a11,
R.raw.a12, R.raw.a13, R.raw.a14, R.raw.a15, R.raw.a16, R.raw.a17,
R.raw.a18, R.raw.a19, R.raw.a20, R.raw.a21, R.raw.a22, R.raw.a23,
R.raw.a24, R.raw.a25, R.raw.a26, R.raw.a27, R.raw.a28, R.raw.a29,
R.raw.a30, R.raw.a31, R.raw.a32, R.raw.a33, R.raw.a34, R.raw.a35,
R.raw.a36, R.raw.a37, R.raw.a38, R.raw.a39, R.raw.a40, R.raw.a41,
R.raw.a42, R.raw.a43, R.raw.a44, R.raw.a45, R.raw.a46, R.raw.a47,
R.raw.a48, R.raw.a49, R.raw.a50, R.raw.a51, R.raw.a52, R.raw.a53,
R.raw.a54, R.raw.a55, R.raw.a56, R.raw.a57, R.raw.a58, R.raw.a59,
R.raw.a60, R.raw.a61, R.raw.a62, R.raw.a63, R.raw.a64, R.raw.a65,
R.raw.a66, R.raw.a67, R.raw.a68, R.raw.a69, R.raw.a70, R.raw.a71,
R.raw.a72, R.raw.a73, R.raw.a74, R.raw.a75, R.raw.a76, R.raw.a77,
R.raw.a78, R.raw.a79, R.raw.a80, R.raw.a81, R.raw.a82, R.raw.a83,
R.raw.a84, R.raw.a85, R.raw.a86, R.raw.a87, R.raw.a88, R.raw.a89,
R.raw.a90, R.raw.a91, R.raw.a92, R.raw.a93, R.raw.a94, R.raw.a95,
R.raw.a96, R.raw.a97, R.raw.a98 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hImageViewPic = (ImageView) findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bIleri);
gButton = (Button) findViewById(R.id.bGeri);
// Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
try {
// Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
ourSong = MediaPlayer.create(MainActivity.this,
audios[currentAudio]);
currentAudio++;
ourSong.start();
} catch (Exception e) {
}
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
try {
// Decrease Counter to move to previous Image
currentImage--;
currentImage = (currentImage + images.length) % images.length;
hImageViewPic.setImageResource(images[currentImage]);
MediaPlayer.create(MainActivity.this, audios[currentAudio]);
currentAudio--;
ourSong.start();
} catch (Exception e) {
}
}
};
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Simply set this on your button click:
int current_img = 0;
int current_aud = 0;
btn_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
current_img ++;
if(current_img == pets.length){
current_img = 0;
}
itemimage.setImageResource(pets[current_img]);
if(mp != null && mp.isPlaying()){
mp.stop();
}
current_aud++;
if(current_aud == petsAudio.length){
current_aud = 0;
}
mp.reset();
mp = MediaPlayer.create(ViewPagerActivity.this, petsAudio[current_aud]);
current_aud --;
mp.start();
}
});
You must add this method in your code
public void cleanup() {
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
mp = null;
}
}
and call this method in both button listener before
MediaPlayer.create(MainActivity.this, audios[currentAudio]);

Categories