OnClick Button Sound link with an ImageView - android

I'm trying to learn to program a simple app in (Android Studio), in this app I have 10 images which I programmed them with ImageSwitcher to go next or previous, Now I want to have a button which plays my 10 audios according to each image. Example when the Image I1 is shown and user presses PLAY button then Audio A1 plays, and the same for the rest I2=A2, I3=A3 and ...
Any help would be great and is appreciated.
with best regards

First you need to add your audio files in the raw folder. Then create an array with reference of image array put names of audio accordingly
public class MainActivity extends AppCompatActivity {
Context context = this;
MediaPlayer mp;
int audioList[] = [R.raw.audio1, R.raw.audio2........]
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button b = (Button) findViewById(R.id.Button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(context, audioList[currentIndex]);
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, audioList[currentIndex]);
} mp.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
}
}
Hope this works for you.
Happy Coding

Related

How to convert this file into an MP3 or is it already defined as MP3?

here's my code but there's no sound being played
MainActivity.java
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.file);
Button playButton = (Button) findViewById(R.id.play_id);
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.start();
}
});
Button pauseButton = (Button) findViewById(R.id.pause_id);
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.stop();
}
});
}
my code works but there's no sound being played. i don't know what's wrong
also the format of the mp3 is weird it shows YT with black background which i don't understand.
Thanks for the try but it was my mistake in this line playButton.setOnClickListener(new View.OnClickListener() { it should be pauseButton, i only forgot to change it. Thanks
change your sound file name, "file" is an reserved name!

MediaPlayer playback limit

I'm using a MediaPlayer to play my sound effects but after a certain point (28 playbacks to be exact) the MediaPlayer does not play anymore. This is how I create the MediaPlayer:
if(noteManager.rep.get((layer)).notes[j] == 0){
MediaPlayer mp;
switch (j){
case 0:
mp = MediaPlayer.create(InGame.this, R.raw.one);
break;
...
}}
Right after I call:
mp.start();
Also, this code gets executed a couple times a second. It all works fine until the 29th sound effect, after which it stops working (there's no sound). I'm using short wav files. Any thoughts?
Use MediaPlayer outside the click event
public class MainActivity extends AppCompatActivity {
private ImageView Reload;
private TextView Label;
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Reload = (ImageView)findViewById(R.id.iv_Reload);
Label = (TextView)findViewById(R.id.tv_appname);
mp = MediaPlayer.create(getApplicationContext(),R.raw.reloadsound);
Reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.iv_anim));
mp.start();
}
});
}

Run an audio file instantly when clicking on the application button (Android)

How to run an audio file instantly when clicking on the application button and not when button returns with Android Media Player
Any help is welcome because I did not find solution.
1.you add button
2.
public class Setting extends Activity implements OnClickListener{
private MediaPlayer Music;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_setting);
Music = MediaPlayer.create(this, R.raw.musicname);
findViewById(R.id.button).setOnClickListener(this);
}
public void onClick(View view) {
if (view.getId() == R.id.button) {
Music.start();
}
}
}
only Preview :D

Media Player start stop start

I am making a new android sound application. I made a clickable button to play sound when I click on it. But I also want it to stop playing sound when I click for the second time. That part works fine now here is the problem, when I click again on button to play sound again, it doesn't play it, Media player is completely stopped. I was looking on forums but I can't seem to find an answer that could help me.
Here is my Activity:
MediaPlayer mpButtonClick1;
MediaPlayer mpButtonClick2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prvi);
final MediaPlayer mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
final MediaPlayer mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
Button dugme = (Button) findViewById(R.id.dugme);
dugme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
mpButtonClick1.reset();
}
else {
mpButtonClick1.start();
}
}
});
When I try to write mpButtonClick1.prepare(); I get error Unhandled Exception Type IOE exception
Try to use pause instead of stop.
Reason: if you pause the MediaPlayer, then you can resume it later. However, if you use stop, almost any other method won't work and you will have to prepare the MediaPlayer again (or create a new one).
More info: here and here
PS: don't forget to release the memory when you finish using the resources.
Try this:
You should use only one mediaplayer object
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
Change your class with below code:
remove reset();.
init well all components:
MediaPlayer mpButtonClick1;
MediaPlayer mpButtonClick2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prvi);
mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
Button dugme = (Button) findViewById(R.id.dugme);
dugme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
}
else {
mpButtonClick1.start();
}
}
});
You're calling mpButtonClick1.reset() after mpButtonClick1.stop() - don't do that:
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
mpButtonClick1.reset(); //<--------- calling reset(), remove this line
}
The docs for reset() say:
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
Remove mpButtonClick1.reset() and it should work.
Keep in mind that MediaPlayer works as a state machine, which means that if you call methods in the wrong order, you'll get problems. Please read about MediaPlayer here and here.
Hey please use following
for stop -> media player
mp.seekTo(0);
mp.pause();
again for start just call
mp.start();
In my experience when I need to play multiple times and I may need to stop one play to start another play, (like in the case of multiple buttons), I just create another player, making sure that I release the resources for the previous one. To stop just use
mediaPlayer.stop();
But for play use something like this (adapt the logging to your specific needs) to create/recreate your player:
private boolean createMediaPlayer()
{
if (mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setVolume(1f, 1f);
try
{
mediaPlayer.setAudioStreamType(Interop.PRIMARY_STREAM);
mediaPlayer.setDataSource(m_soundFile);
mediaPlayer.prepare();
return true;
// Interop.logDebug(TAG + "-loadAudio: SUCCESS" + m_soundFile);
} catch (Exception e)
{
Interop.logError(TAG + "-LoadAudio for Clic Sound: audioPlayer prepare failed for current file: " + m_soundFile);
Interop.logError(TAG + "-Exception: " , e);
return false;
}
}
and than use
if (createMediaPlayer())
mediaPlayer.start();
this will ensure proper release of the resources used by the media player.
A simple solution is to Use pause instead of stop and the seek to the beginning of the song.
I know that this question is quite old but recently while learning Android, I also got stuck at this point and found a very simple solution which I'd like to share with everyone.
Instead of trying to stop or reset the media, you can just seek back to the starting position.
mediaPlayer.seekTo(0);
For reference, I am also posting my code below:
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
public void play(View view) {
mp.start();
}
public void pause(View view) {
mp.pause();
}
public void stop(View view) {
// this seeks to the beginning of the file
mp.seekTo(0);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.sample_audio);
}
}

Android MediaPlayer Stop and Play

I'm creating Android application contains 2 buttons, on click on each button play a mp3 file.
The problem is when I play button1 it plays sound1, when I click button2 it plays sound2.
I check on each button the other player if it's working and I stop it and play the clicked one
But If I click on same button twice it's keep first audio playing in the background and play another one again
I tried to check isPlaying() and to stop it, but it doesn't work!
I want If I click on button1 it play sound1 and if clicked on it again it stop it and play it again from beginning.
My code:
package com.hamoosh.playaudio;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PlayaudioActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b= (Button) findViewById(R.id.button1);
Button b2= (Button) findViewById(R.id.button2);
final TextView t= (TextView) findViewById(R.id.textView1);
final MediaPlayer mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
final MediaPlayer mp1 = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.stop();
}
mp1.start();
}
});
}
}
Hope if there any better code that can use multiple buttons as an array or something to not check each button and player every time.
You should use only one mediaplayer object
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
To stop the Media Player without the risk of an Illegal State Exception, you must do
try {
mp.reset();
mp.prepare();
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
rather than just
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
According to the MediaPlayer life cycle, which you can view in the Android API guide, I think that you have to call reset() instead of stop(), and after that prepare again the media player (use only one) to play the sound from the beginning. Take also into account that the sound may have finished. So I would also recommend to implement setOnCompletionListener() to make sure that if you try to play again the sound it doesn't fail.
I may have not got your answer correct, but you can try this:
public void MusicController(View view) throws IOException{
switch (view.getId()){
case R.id.play: mplayer.start();break;
case R.id.pause: mplayer.pause(); break;
case R.id.stop:
if(mplayer.isPlaying()) {
mplayer.stop();
mplayer.prepare();
}
break;
}// where mplayer is defined in onCreate method}
as there is just one thread handling all, so stop() makes it die so we have to again prepare it If your intent is to start it again when your press start button(it throws IO Exception)
Or for better understanding of MediaPlayer you can refer to Android Media Player
just in case someone comes to this question, I have the easier version.
public static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
Button b2 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, R.raw.game);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
// mp.start();
}
});
}
In Kotlin
1- Upper Class add a variable:
var mpo: MediaPlayer? = null
2- In onCreate or a Function
mpo = MediaPlayer.create(this, R.raw.m7)
mpo!!.start()
3- Add Stop under a control, onClick or any order:
if (mpo != null) {
mpo!!.stop()
mpo!!.release()
mpo = null
}
Note that for each player, need a separate stop!

Categories

Resources