Background audio for activity - java

I want to add my recorded audio in background of the activity in which I am explaining an example. Problem is that audio do not stop after ending the activity and it keeps on playing. I want audio only to be played when my activity is at front. Kindly put the service class in following code
public class exp extends Activity {
ImageView imview1,imview2;
final MediaPlayer mediaPlayer=new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
mediaPlayer.create(exp.this,R.raw.babydoll);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
mediaPlayer.start();
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100,100);
}
}, 2000);
}
protected void onPause(){
super.onPause();
mediaPlayer.pause();
}
}

First. you must use a Service for playing music, but if you do not want use a service. the way to instaciate a Media player is mediaPlayer = MediaPlayer.create(this,R.raw.facebook_tone);.
this code when the activity pass to background the music is paused, when the activity pass to foreground the music continue playing.
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the MediaPlayer object
mediaPlayer = MediaPlayer.create(this,R.raw.facebook_tone);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mediaPlayer.start();
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100,100);
}
}, 2000);
}
#Override
protected void onPause() {
super.onPause();
//pause the music
mediaPlayer.pause();
}
#Override
protected void onResume() {
super.onResume();
//validate if the mediaplayer is not null and not playing
if(mediaPlayer != null && !mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
}

Related

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();

How to use same button after setoncompletitionlistener?

After I set oncompletetionlistener for MediaPlayer.
Using the same button again after the mediaplayer is release, it crashes the app. How do I call a event to play audio after the media player is release?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1 = findViewById(R.id.b1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.button1);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.release();
}
});
}
});
}}
After you release the media player you have to reintialiaze the MediaPlayer object.
if (mp1 == null)
mp1 = new Media player();

Background audio for one specific activity

I want to add my recorded audio in background of the activity in which I am explaining an example. Problem is that audio do not stop after ending the activity and it keeps on playing. I want audio only to be played when my activity is at front. Kindly put the service class in following code
public class exp extends Activity {
ImageView imview1,imview2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
}
}, 2000);
}
}
In onResume start your audio
#Override
protected void onResume() {
super.onResume();
//start your audio track here
}
Stop the same in onPause
#Override
protected void onPause() {
//Sop it over here
super.onPause();
}
Create the player instances in onCreate
public class exp extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
//player starting code goes here.
}
protected void onPause() {
//stop the music player here.
}
}
Try to stop your audio in onPause:
public class exp extends Activity {
ImageView imview1,imview2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
}
}, 2000);
}
#Override
public void onPause(){
super.onPause();
//Kill your audio
}
}
I think your solution is something like that. Try it and comment
Good look!

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