How to play mp in other java class than MainActivity? - java

I have two java classes: MainActivity and Explosion. I want to use this code in the Explosion java class. (this, R.raw.explosionsound) doesn't work and (Explosion.this, R.raw.explosionsound) also doesn't work.
How do I fix this?
MediaPlayer mp = null;
mp = MediaPlayer.create(this, R.raw.explosionsound);
if (mp != null) {
mp.stop();
mp.release();
}
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});

If Explosion is not an activity then you must supply a Context to instantiate the MediaPlayer object.
Explosion explosion = new Explosion(context);
explosion.play();
class Explosion{
Context context;
public Explosion(Context context){
this.context = context;
}
public void play(){
MediaPlayer mp = MediaPlayer.create(context, R.raw.explosionsound);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
mp.start();
}
}
Also as it was allready pointed out you can't release the MediaPlayer object and then call start();

Related

No Sound Coming From Android Emulator | Android Studio

MediaPlayer player;
// Playing Sound
public void playSound(View v){
player = MediaPlayer.create(this, R.raw.song);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
Log.e("start","player çalıştı");
}
});
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.release();
Log.e("stop", "player durdu");
}
});
}
My code is here and with this code i do not get any sound from physical and virtual device.

How to terminate music using back button of phone and created back button?

I was having some bugs with playing a music in my app. It wont stop playing even if I exit the app. It will only stop if I hit pause or terminate the app. Here's my code for music. What should I do code to stop it.
public void play_brain(View g){
final MediaPlayer brainMP = new MediaPlayer();
try {
brainMP.setDataSource("https://firebasestorage.googleapis.com/v0/b/capstone-katugna-001.appspot.com/o/meditation%20music%201%20minute%20peaceful%20music%201%20minute%20mindfulness%20meditation%201%20minute%20relax%20music%201.mp3?alt=media&token=f34fe7c7-54f1-4645-88cf-5e7a21f401d8");
brainMP.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
brainMP.prepare();
} catch (IOException e) {
e.printStackTrace();
}
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
brainMP.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.pause();
}
});
brainMP.pause();
}
});
}
You can try using this code:
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private Button buttonStartPause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Initialize your MediaPlayer object here
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
}
buttonStartPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
// here you can set the play icon to your button
} else {
mediaplayer.start();
// here you can set the pause icon to your button
}
}
});
// To capture back button's pression you can override onBackPressed() in your Activity:
#Override
public void onBackPressed() {
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
}
super.onBackPressed();
}
}

Stop audio playing on backpress

I have an activity when i click on play button and it plays mp3 sound. When I click back the audio still plays in the background and can also run alongside other audio sounds which is annoying. I have tried to implement other methods used on here but to no avail.
Code:
public class InstructionsManualActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instructions_manual);
Button one = (Button) this.findViewById(R.id.instructionsManual_but);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.instructions_manual);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
}
I have tried this code but app crashes on back press
#Override
public void onBackPressed() {
super.onBackPressed();
if (mp.isPlaying()) {
mp.stop(); // or mp.pause();
mp.release();
First of all declare the MediaPlayer object Globally
MediaPlayer mp = null; //Declaring globally so can access anywhere in activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instructions_manual);
Button one = (Button) this.findViewById(R.id.instructionsManual_but);
mp = MediaPlayer.create(this, R.raw.instructions_manual);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
When you are pressing back button in any activity onBackPressed()
will be called , there you can stop you media player if playing.
#Override
public void onBackPressed() {
super.onBackPressed();
//Here you can stop your MediaPlayer
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
}
}
Try this it's working for me
if (mp != null) {
mp.stop();
mp.release();
mp = null;
finish();
}
Your code is fine just check it before stop for null player
super.onBackPressed();
if (mp!=null && mp.isPlaying()) {
mp.stop(); // or mp.pause();
mp.release();

Audio player with listview

i have audio files when i'm play another file then previous file play continuously.
so if you have solution please let me know.
here is my main.java
public class MusicAndroidActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonPlay = (Button) findViewById(R.id.play);
music = (ListView)findViewById(R.id.music);
// music.setAdapter(new ArrayAdapter<String>(this,, str));
music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
switch (position){
case 0:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.hosannatamil);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
case 1:
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(),R.raw.one_less);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
case 2:
MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(),R.raw.words);
mp2.start();
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
}
}
});
}
You need to manually call MediaPlayer.stop() if you want to stop the playback.
If you want to start the same MediaPlayer again you have to call .prepare() before that.
For more information read the reference page and look at the state diagram there.
if(mediaPlayer.isPlaying())
{
//stop or pause your media player mediaPlayer.stop(); or mediaPlayer.pause();
mediaPlayer.pause();
}
else
{
mediaPlayer.start();
}
Put this Condition inside switch statements before mediaPlayer.start() also create a global reference of Media Player instead of creating three references like MediaPlayer mediaPlayer instead of MediaPlayer mp,mp1,mp2 etc,because you can create media Player object in the same reference as one condition must be true all the time,also put a default condition in the switch statement!

how can I use MediaPlayer outside my mainActivity in android (how to call it from mainActivity)

IM trying to create a simple app and for it I have to have a mediaPlayer not in the mainActivity. how can I do this?(in what kind of class does it need to be and how to write the method and instance it in main).
Im quite new to android and have nooo idea how to do this(its probably very simple...)
any help would be great. thanks ahead!
You can create such a class for usage in every Activity:
public class MediaPlayerUtil {
public static void playSound(Context context, int soundFileResId) {
MediaPlayer mp = MediaPlayer.create(context, soundFileResId);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
mp=null;
}
});
mp.start();
}
}
Then inside any Activity just call MediaPlayerUtil.playSound(this, R.raw.your_sound_file) where this will be a reference to your activity and by R.raw.your_sound_file you reference file in /res/raw directory of your project.
This is an example of a simple app like to ask just have to create the folder raw, where the audio was located.
mysong replace the file with the name of your audio
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button play, stop;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button)findViewById(R.id. play);
stop = (Button)findViewById(R.id.stop);
play.setOnClickListener(this);
stop.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.play:
play();
break;
case R.id.stop:
stop();
break;
}
}
private void play (){
destruir();
mp = MediaPlayer.create(this, R.raw.mysong);
mp.start();
}
private void stop(){
mp.stop();
}
private void destruir(){
if (mp !=null)
mp.release();
}
}

Categories