I start mediaplayer like this:
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.background);
mp.start();
How can I stop in another activity? It continues to play in another activity. How can I use onDestroy in another activity?
Use the Separate class like below in your project.
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
public class AudioPlay {
public static MediaPlayer mediaPlayer;
private static SoundPool soundPool;
public static boolean isplayingAudio=false;
public static void playAudio(Context c,int id){
mediaPlayer = MediaPlayer.create(c,id);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
if(!mediaPlayer.isPlaying())
{
isplayingAudio=true;
mediaPlayer.start();
}
}
public static void stopAudio(){
isplayingAudio=false;
mediaPlayer.stop();
}
}
Playing the song
`AudioPlay.playAudio(mContext, R.raw.audiofile);` // play it from your preferred activity. and you can change raw file to your path also its depends upon your requirement.
then
stop the audio using this lines AudioPlay.stopAudio(); from any activity.
hope this helps.
In 1st activity override onPause
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.stop();
}
This is my Kotlin solution:
package com.programacionymas.myapp.services
import android.content.Context
import android.media.MediaPlayer
object AudioPlay {
var mediaPlayer: MediaPlayer? = null
var lastResource: Int? = null
fun playAudio(c: Context, id: Int, isLooping: Boolean = true) {
createMediaPlayer(c, id)
mediaPlayer?.let {
it.isLooping = isLooping
if (!it.isPlaying) {
it.start()
}
}
}
private fun createMediaPlayer(c: Context, id: Int) {
// in case it's already playing something
mediaPlayer?.stop()
mediaPlayer = MediaPlayer.create(c, id)
lastResource = id
}
// usually used inside the Activity's onResume method
fun continuePlaying(c: Context, specificResource: Int? = null) {
specificResource?.let {
if (lastResource != specificResource) {
createMediaPlayer(c, specificResource)
}
}
mediaPlayer?.let {
if (!it.isPlaying) {
it.start()
}
}
}
fun pauseAudio() {
mediaPlayer?.pause()
}
}
I started from itsrajesh4uguys's Java answer, and I applied these changes:
Remove the Boolean attribute, since it was not being used
Add an attribute to track the last loaded resource, this way I can continue playing it or replace with other resource
Call the stop function before creating a new instance, to avoid overlapping
Finally I use it this way:
In the onCreate method:
AudioPlay.playAudio(this, R.raw.background_music)
In the onResume method:
AudioPlay.continuePlaying(this, R.raw.background_music)
In my case I had to specify the resource, because some of my Activities start playing another music sounds.
you cannot call stop an activity but from the activity itself to
achive this you can send the media player in a service and bind to the service in the activities you want to access it
As you have started media player in first activity and wanted to stop in another activity, just call your second layout in first activity using layout inflater self instead of creating another activity.. and on second layout file just stop the media player by pressing a button
public class FirstAvtivity extends Activity
{
MediaPlayer mPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity_layoutfile);
Button b=(Button)findViewById(R.id.button1);
//start the media player like how you were starting in your activity
// then after clicking button you will be navigated to new layout , there
// you can stop media player
mPlayer.start();
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateLayout();
}
});
}
private void newUpdateLayout() {
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.second_disapr_scr, null));
finalDismiss=(Button)findViewById(R.id.final_dismiss);
finalDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"welcome to second
avtivity",Toast.LENGTH_SHORT).show();
mPlayer.stop();
finish();
}
});
}
}
Related
HELP PLEASE !!
I'm using media player in recycler view and the problem is when different item's play buttons are clicked they all play at the same time. How can I stop the previous audio and start the new audio ?
Here is the adapter code:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
final MediaPlayer mediaPlayer = new MediaPlayer();
public void onBindViewHolder(#NonNull MyAdapter.ViewHolder viewHolder, int position) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(item.get(position).getAudio());
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
viewHolder.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
viewHolder.play.setImageResource(R.drawable.play);
} else {
mediaPlayer.start();
viewHolder.play.setImageResource(R.drawable.pause);
}
I suppose one answer is using flag. but I don't know how to implement it.
There are multiple audios that play at the same time because there are multiple instances of the MediaPlayer object, because it's instantiated on every call to the onBindViewHolder(); i.e. each row of the RecyclerView will have a unique object.
To fix this, you need to use only a single object for all the audio, so transfer the
final MediaPlayer mediaPlayer = new MediaPlayer();
To be a direct field to the adapter class.
UPDATE:
Now change the logic of play button click
public void onBindViewHolder(#NonNull MyAdapter.ViewHolder viewHolder, int position) {
viewHolder.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
viewHolder.play.setImageResource(R.drawable.play);
}
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(item.get(position).getAudio());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
viewHolder.play.setImageResource(R.drawable.pause);
}
}
}
UPDATE 2:
Try to transfer the play button code to the ViewHolder instead of the onBindViewHolder, and instead of the position, use getAbsoluteAdapterPosition()
public static class ViewHolder extends RecyclerView.ViewHolder {
//.....
public ViewHolder(View itemView) {
super(itemView);
play = itemView.findViewById(...);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// ..... Add the mediaPlayer code
}
}
}
}
This is a problem of double instantiation of MediaPlayer() class. What you have to do is to make an object class like this:
import android.media.MediaPlayer
import android.net.Uri
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
object MusicObject {
private lateinit var mediaPlayer: MediaPlayer
private val executor: ExecutorService = Executors.newCachedThreadPool()
private var flag = 0
private var musicName = ""
private var musicID = 1
private var musicUri: Uri? = null
// Function to play music
fun playMusic(mp: MediaPlayer,name: String,ID: Int,Uri: Uri) {
if (flag > 0) {
pauseMusic()
}
flag++
musicName = name
musicID = ID
musicUri = Uri
mediaPlayer = mp
executor.execute {
mediaPlayer.start()
}
}
// Function to play currently playing music again
fun playMusicAgain() {
executor.execute {
mediaPlayer.start()
}
}
// Function to pause music
fun pauseMusic() {
mediaPlayer.pause()
}
// Function to stop music
fun stopMusic() {
if (mediaPlayer.isPlaying) {
mediaPlayer.stop()
}
}
fun getMediaPlayer(): MediaPlayer {
return mediaPlayer
}
}
And call this class to play and pause the music.
To play music:
MusicObject.playMusic(MediaPlayer.create(this, songUri), idOfMusic, songID, songUri)
To pause music:
MusicObject.pauseMusic()
I am trying to write a small android app for my son. The idea is to make ringtone start to play on button click and it should be stoped by clicking on the other button. Something like you click a button to start a calling and another button to answer to it.
What I manage to do is to make the first button to ring to work by the following code:
CallLukas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
});
However, the second button to stop the ringtone does not work as it suppose to. I use the following code:
Incoming.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
}
});
It stops the ringtone but in the same time makes the app to crash.
What would be the right way to stop the previously activated ringtone?
Thnak you...
This answer should help you.
I believe the problem is you're creating multiple MediaPlayer objects. Also try to use .reset() instead of .stop().
The solution for my issue was making the MediaPlayer as a service.
I have created a new Java class to extend the service class. Here is the code for it:
public class RingtoneService extends Service {
private MediaPlayer mp;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mp = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
mp.setLooping(true);
mp.start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
mp.stop();
}
}
Then I have updated my MainActivity class with the fallowing code:
CallLukas.setOnClickListener(this);
Incoming.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v == CallLukas){
startService(new Intent(this, RingtoneService.class));
}else if (v == Incoming) {
stopService(new Intent(this, RingtoneService.class));
}
}
The code basically sets the OnClickListener on my buttons and starts the service if one is clicked and stops the service if the other is clicked.
The final steps are to add the newly created service to the AndroidManifest file. It needs to be added inside the <application> tag.
<application
<service android:name=".RingtoneService"></service>
</application>
The completed instruction on how to implement this can be found here:
https://www.youtube.com/watch?v=p2ffzsCqrs8&t=315s
hei, can you help me?, i have 2 activity and i want to stop mediaPlayer in another activity, how to use the button in first activity to stop media player in the second activity,
my code in first activity
public class homenor extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homenor);
Button btmateri = (Button) findViewById(R.id.btmateri);
btmateri.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(homenor.this, home.class);
home.mediaPlayer.stop();
home.mediaPlayer2.stop();
startActivity(intent);
}
});
}
and this code in second activity
public class home extends AppCompatActivity {
int [] sound, soalkuis;
int soundke = 0;
int getNosoal = 0;
public static MediaPlayer mediaPlayer, mediaPlayer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
sound = new int[] {R.raw.definisiprisma1, R.raw.definisiprisma2, R.raw.definisiprisma3,R.raw.jaringprisma1,R.raw.luasprisma1, R.raw.luasprisma2
,R.raw.luasprisma3,R.raw.luasprisma4,R.raw.luasprisma5,R.raw.luasprisma6,R.raw.contohluasprisma1,R.raw.penyelesaianluasprisma1,R.raw.contohluasprisma2
,R.raw.penyelesaianluasprisma2,R.raw.contohluasprisma3, R.raw.penyelesaianluasprisma3, R.raw.volumeprisma1, R.raw.contohvolume1
, R.raw.penyelesaianvolumeprisma1, R.raw.contohvolume2, R.raw.penyelesaianvolumeprisma2, R.raw.contohvolume3, R.raw.penyelesaianvolumeprisma3
, R.raw.rangkumanprisma1, R.raw.rangkumanprisma2};
soalkuis = new int[] {R.raw.soalprisma1, R.raw.soalprisma2, R.raw.soalprisma3, R.raw.soalprisma4, R.raw.soalprisma5, R.raw.soalprisma6
, R.raw.soalprisma7, R.raw.soalprisma8, R.raw.soalprisma9, R.raw.soalprisma10};
mediaPlayer = new MediaPlayer();
mediaPlayer2 = new MediaPlayer();
mediaPlayer = MediaPlayer.create(home.this, sound[soundke]);
mediaPlayer2 = MediaPlayer.create(home.this, soalkuis[getNosoal]);
mediaPlayer2.setLooping(false);
mediaPlayer.setLooping(false);
mediaPlayer.start();
when i try that, i got error like thisjava.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference
how can i fix that? thank you
create class MyMediaPlayer,
public class MyMediaPlayer {
private static MyMediaPlayer Instance;
MediaPlayer mediaPlayer;
static MyMediaPlayer getMediaPlayerInstance() {
if (Instance == null) {
return Instance = new MyMediaPlayer();
}
return Instance;
}
public void playAudioFile(Context context, int sampleAudio) {
mediaPlayer = MediaPlayer.create(context, sampleAudio);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
public void stopAudioFile() {
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}}
Now, Call this method to play audio
MyMediaPlayer.getMediaPlayerInstance().playAudioFile(this, R.raw.sampleaudio)
To Stop Audio, Call this method
MyMediaPlayer.getMediaPlayerInstance().stopAudioFile()
Take the media player as public which you are already taking, then from the second activity, stop the media player like this
try {
mp.reset();
mp.prepareAsync();
mp.stop();
mp.release();
mp = null;
} catch (Exception e) {
e.printStackTrace();
}
my suggestion is to create a class like MediaPlayerManager or ...
create MediaPlayer instance inner this class and implement your interface with your method like pause, play, stop, ...
so, when need action MediaPlayer get instance this class and call suitable method
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);
}
}
I am beginner at Android App I have new app which call html files from assets file one of the task on this app is when user selected check box input to value "on" the mp3 play and when user goout from app as press back button or press home button the sound stopped and close the app. I did my interface between html and main activity to call java methods at html. When I added my code to stop the sound it does not play . I tried to add this line of code in main activity oncreate method .
mp= MediaPlayer.create(mContext,R.raw.sound);
the sound stopped when press back or home or turnoff buttons . but the sound work when i open the app but this what i doesnot need i need this is when user select the checkbox . so how can i do it .
#Override
protected void onPause()
{
super.onPause();
if(mp.isPlaying())
mp.pause(); //stop the sound
}
#Override
protected void onResume()
{
super.onResume();
mp.start();
}
public class WebAppInterface {
Context mContext;
private MediaPlayer mp;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void playsound(String value ) {
if (value.equals("on")) {
mp= MediaPlayer.create(mContext,R.raw.sound);
mp.setLooping(true);
mp.start();
}
else
{ mp.stop();}
}
}
I am assuming the issue is that the sound plays regardless whether you have selected the checkbox or not. A way to fix this would be to confirm whether the checkbox is checked or not in the onResume() method, instead of just starting regardless.
public class WebAppInterface extends Ramadan {
Context mContext;
private MediaPlayer mp;
private static boolean checked = false;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void playsound(String value , MediaPlayer mp ) {
if (value.equals("on")) {
checked = true;
mp= MediaPlayer.create(mContext,R.raw.sound);
mp.setLooping(true);
mp.start();
}
else
{
checked = false;
mp.stop();
}
}
}
in your main class:
#Override
protected void onPause()
{
super.onPause();
if(mp.isPlaying())
mp.pause(); //stop the sound
}
#Override
protected void onResume()
{
super.onResume();
if(WebAppInterface.checked)
{
mp.start();
}
}