I'm just learning here. I'm trying to make a soundboard with around forty sounds, but I'm having some trouble how to get it to work using a maphash. Can anyone save me?
--------------soundboard-------------------------
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import java.util.HashMap;
import java.util.Map;
public class main extends Activity {
MediaPlayer mp=null;
\\\if I put put "MediaPlayer mp;" here it only plays one sound\\\
ImageButton Button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
mp = MediaPlayer.create(this, entry.getValue());
\\\if I put "final MediaPlayer mp = MediaPlayer.create(this, entry.getValue());" here I cant stop MediaPlayer with onpause and onstop overrides.\\\
ImageButton button = (ImageButton) findViewById(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
}
#Override
protected void onStop() {
super.onStop();
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
#Override
public void onDestroy(){
super.onDestroy();
mp.release();
}
}
As I suggested in your previous question, don't create all those mediaplayer instances, for two reasons:
You lose all instances and have only the last one. always.
The onCreate() become very long method for no reason.
instead, remove mp = MediaPlayer.create(this, entry.getValue()); from your for loop, and move it to inside of your listener, something like that (not tested...):
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int sound = map.get(v.getId());
mp = MediaPlayer.create(main.this, sound);
mp.start();
}
});
So you would create the mediaplayer instance only when needed.
BTW, main is not a good name for a class.
Related
I am trying to make a simple app to help me learn how to code for Android. The apps principle is simply this: When you press down on a button it starts playing a sound file. When you release the button, the audio stops. Pressing down on the button works, but releasing it does not. Thanks in advance! Here is my java code:
package com.example.siriu.presstoplay;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.MotionEvent;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.pressbutton);
button.setOnTouchListener(handleTouch);
mp = MediaPlayer.create(this, R.raw.test);
}
private View.OnTouchListener handleTouch = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("button", "down");
play();
break;
case MotionEvent.ACTION_UP:
Log.d("button", "up");
pause();
break;
}
return true;
}
};
public void play (){
final MediaPlayer mp = MediaPlayer.create(this, R.raw.test);
mp.start();
Log.d("MediaPlayer", "started");
}
public void pause (){
mp.stop();
Log.d("MediaPlayer", "paused");
}
}
Android MediaPlayer will not stop or pause
Because in your play () method you are starting local declared MediaPlayer of play () method
and in your pause () you are trying to stop MediaPlayer that you have declared global
in this situation your global declared MediaPlayer mp; hasn't start yet
Try this
Remove final MediaPlayer mp = MediaPlayer.create(this, R.raw.test); from Your play() Method
public void play (){
mp.start();
Log.d("MediaPlayer", "started");
}
I am a very beginner in Android, I am trying to make a MediaPlayer works but I have some errors.
1-If I click on play again , start playing twice at the same time.
2-If click on puse, doesn't happen anything.
3-My intention is to do a reproduction list with play, pause and stop.
Thanks in advance.
package com.example.android.allmusic;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.media.MediaPlayer;
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (!mp.isPlaying()) {mp.start();}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (mp.isPlaying()) { mp.pause(); }
}
});
}
}
Try declaring your MediaPlayer as a member variable, it should be enough to set it on your onCreate, but just declare it as an attribute of your class to test.
Since you are creating a new reference to the MediaPlayer every time you click on the ImageView or the TextView, you don't the old reference to pause it or to know if it has already been started.
Code:
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
//MediaPlayer as a member variable
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mp.isPlaying()) {
mp.start();
}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mp.isPlaying()) {
mp.pause();
}
}
});
}
}
When i click a imgniatp at the display, audio play very well, but if i click imgbukap too, before audio of imgniatp finish, imgbukap also play audio. so both audio play together. How to play one sound at a time?
package sodik.com.ramadhan;
import sodik.com.ramadhan.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.TextView;
public class Puasa3Activity extends Activity implements OnClickListener{
MediaPlayer mp;
ImageButton imgniatp1, imgbukap2;
String p1, p2, p3, p4, p5, p6, p7, p8, p9;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.puasa3);
TextView p1tk = (TextView) findViewById(R.id.p1tk);
TextView p2tk = (TextView) findViewById(R.id.p2tk);
imgniatp1=(ImageButton) findViewById(R.id.imgniatp);
imgniatp1.setOnClickListener(this);
imgbukap2=(ImageButton) findViewById(R.id.imgbukap);
imgbukap2.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.imgniatp:
mp= MediaPlayer.create(this, R.raw.bukapuasa);
mp.setVolume(1,1);
mp.start();
Toast.makeText(this, " Memainkan Buka Puasa", Toast.LENGTH_SHORT).show();
break;
case R.id.imgbukap:
mp = MediaPlayer.create(this, R.raw.niatpuasa);
mp.setVolume(1,1);
mp.start();
Toast.makeText(this, " Mainkan Niat Puasa ", Toast.LENGTH_SHORT).show();
break;
}
}
`
You could check to see if the MediaPlayer is already playing something, and start the clip in an onCompletionListener if it is
if(mp != null && mp.isPlaying()) {
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp = MediaPlayer.create(this,R.raw.bukapuasa);
mp.start();
}
});
} else {
mp = MediaPlayer.create(this,R.raw.bukapuasa);
mp.start();
}
package project.kalmas;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class one extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
MediaPlayer mp= MediaPlayer.create(this,R.raw.one);
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
When i click button it play sound then again i click button to stop it wont stop and play sound again which results in double sound playing at one time.Please help
You are creating a new MediaPlayer with every click, instead of keeping a reference to the first one. The MediaPlayer that is playing the sound is different than the MediaPlayer that you are calling isPlaying() on. You need to turn the mp variable into a field so you can keep a reference to it.
I imagine something like this would work:
public class one extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mp = MediaPlayer.create(this,R.raw.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
I'm trying to create an app that has many pages, every page has button.
When I click on the button it takes me to next page.
What i want to do is when I press on the button it plays the sound that i have included in "raw folder?
At this moment when I manipulated the code and I press on the button to change the page it close the app. Can anyone please help. Thanks
here is my code:
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button button;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp;
#Override
public void onClick(View view) {
mp.seekTo(0);
mp.start();
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.test);
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
}
#Override
public void onPause() {
super.onPause();
// Release the MediaPlayer if going into background
if(mp != null) mp.release();
} }
If this is how your code is laid out:
button.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp;
#Override
public void onClick(View view) {
mp.seekTo(0);
mp.start();
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.test);
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
Then the reason is because you're trying to use the MediaPlayer before actually initializing it. Try this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
final MediaPlayer mp = MediaPlayer.create(context, R.raw.test);
mp.start();
}
catch(Exception e) { e.printStackTrace(); } // Eat it
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
If that doesn't work, well, something is definitely wrong somewhere else.