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();
}
}
});
}
}
Related
When I push the next button, I want to go next layout and stop still playing music.
If I push the play button and push the next button app is working.
But, if music doesn't play and I push to the next layout button, the app is crashing because my code try to stopped even though music isn't playing. What can i do ?
package com.gamedom.relaxingmusic;
import gif.decoder.GifRun;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
public class Music1 extends Activity {
public static MediaPlayer mp1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music1);
final ImageButton Start1 =(ImageButton) findViewById(R.id.btnStart1);
final ImageButton Stop1 =(ImageButton) findViewById(R.id.btnStop1);
ImageButton SOL1 =(ImageButton) findViewById(R.id.btnSol1);
ImageButton SAG1 =(ImageButton) findViewById(R.id.btnSag1);
Stop1.setVisibility(View.INVISIBLE);
Start1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1=MediaPlayer.create(Music1.this, R.raw.firefurnace);
mp1.start();
mp1.setLooping(true);
Start1.setVisibility(View.INVISIBLE);
Stop1.setVisibility(View.VISIBLE);
}
});
Stop1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.stop();
mp1.release();
Stop1.setVisibility(View.INVISIBLE);
Start1.setVisibility(View.VISIBLE);
}
});
//////NEXT LAYOUT BUTTON \\\\\\\\\
SAG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC2"));
}
});
////// BACK LAYOUt BUTTON \\\\\\\\\
SOL1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC4"));
}
});
}
Problem is here
SOL1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//It try to stoping the music but it's already stopped program is crashes
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC4"));
}
});
It looks like you initialize your media player object in an onClick. So, if that event is not fired, your mp1 variable will be null. I believe you are getting NullPointerException.
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.
I'd like to play a sound as I tap a button like hitting a drum, to have the sound playback as quick as I'm hitting the button. Right now I have to wait for the sound to finish before it will play again. Here's how I've got the sound set up.
MediaPlayer snareMP;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playdrums);
final MediaPlayer snareMP = MediaPlayer.create(this, R.raw.snare);
ImageView snareDrum = (ImageView) findViewById(R.id.snare);
snareDrum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
snareMP.reset();
snareMP.prepare();
snareMP.start();
}
});
Any help is greatly appreciated.
Thanks!
Try this:
public void onClick(View v) {
if (snareMP.isPlaying()){
snareMP.stop();
snareMP.reset();
}
snareMP.prepare();
snareMP.start();
}
I'm going to share a code that I've written a while back which I think does the exact same thing you're looking for; I hope it helps and I'd appreciate some rep points if it does :)
Here it is:
import com.myProject.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class Camera extends Activity {
private MediaPlayer snapMP;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
Button snap = (Button)findViewById(R.id.snapButton);
snap.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
freeSnapMP();
snapMP = MediaPlayer.create(Camera.this, R.raw.camera_snap2);
snapMP.start();
}
});
}
protected void onDestroy() {
super.onDestroy();
freeSnapMP();
}
private void freeSnapMP(){
if (snapMP != null){ snapMP.stop(); snapMP.release(); snapMP = null; }
}
}
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.