I am in learning process of the Android app development, and trying to create one songs app.
Problem:
I have two activities, A:songslist & B:MediaPlayer
B plays remote song via mp.prepareAsync(); and starts player when its ready.
I want to keep running the song even though I move to other activities or if I open any other app.
But problem comes, when song is running and I go back to select new song, then oncreate activity B it starts streaming and playing new song, since old song is still running.
My Code:
public void Play() {
if(mp.isPlaying())
{
releaseMediaPlayer();
Log.d("MediaPlayer", "Player is already running release it first");
}
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
URL = BASE_URL + album_files[songIndex];
mp.setDataSource(URL);
mp.prepareAsync();
btnPlay.setImageResource(R.drawable.btn_pause);
songTitleLabel.setText("Loading track, please wait....");
}
private void releaseMediaPlayer() {
if (mp != null) {
if(mp.isPlaying()) {
mp.stop();
}
Log.d("MediaPlayer", "Player is released");
mp.release();
mp = null;
}
}
You should have a service for playing music beside playlists and mediaplayer activities. service plays the music even if you exit program and you can have a widget to control playing music.
Try this. Using the answer you can make a background service and also with out background service.
Good luck
Related
I am developing a music player on android and the app has two activities (MainActivity and PlayActivity). In the MainActivity I have a listview with a list of songs and in the PlayActivity I have a button to listen and pause the music. The problem is that when go back to MainActivity and I select a new song from the listview, the first one keeps playing on background while the second one also starts playing. How can I stop the first song when I select a new one?
(I don't want to stop mediaplayer onBackPressed, I just want to stop the music when another song it's selected from listview and play a fresh song in PlayActivity)
EDIT: I'm using AsyncTask to start mediaplayer on PlayActivity: mp.prepareAsync();
You could try to stop MediaPlayer when onBackPressed()
MediaPlayer mp ; // mp is your MediaPlayer
#Override
public void onBackPressed() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
super.onBackPressed();
}
private void playMusic() {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
MediaPlayer copyMp = new MediaPlayer();
try {
copyMp.setDataSource("/path");
mp = copyMp;
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
From here
MediaPlayer is not thread-safe. Creation of and all access to player
instances should be on the same thread.
That means you have to maintain one media player instance throughout your whole application(As per your requirement). I don't know how you implemented the media player(Service/AsyncTask etc.). When you select a new song, you need to access that instance and replace the existing song with the selected one.
i create an app, and in one layout (just call it layout A), i play the media player, then when i went to another layout (just call it Layout B), i want the sound from the Layout A is continue playing in Layout B, and when i went back to the Layout A, i also want the media player is still continuing the music that was played before.
In Layout A, i set this code in onCreate:
player = MediaPlayer.create(this, R.raw.sound);
if(!isMuted())
{
player.setLooping(true);
player.start();
}
...
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(A.this, B.class);
stop=1;
startActivity(intent);
}
});
And this code:
#Override
protected void onPause()
{
super.onPause();
if (stop!=1)
{
//stop=1 if i go to another layout, for example when i want to go to Layout B
//if the device is automatically locked, i want the media player is paused and it resumed when i unlock the device, so i use stop!=1 to know whether the sound should be paused or not
player.pause();
length = player.getCurrentPosition();
}
}
#Override
protected void onResume()
{
super.onResume();
if(!isMuted())
{
//isMuted is method to know whether the sound is muted or not, if it isn't muted, then the sound is resumed
player.seekTo(length);
player.setLooping(true);
player.start();
}
}
In layout B, i used this code in onCreate:
player = MediaPlayer.create(this, R.raw.sound);
....
And this code:
protected void onPause()
{
super.onPause();
player.pause();
length = player.getCurrentPosition();
}
#Override
protected void onResume()
{
super.onResume();
if(!isMuted())
{
if(player.isPlaying())
{
}
else
{
player.seekTo(length);
player.setLooping(true);
player.start();
}
}
}
But this is the problem.
When i went to Layout B, the Media Player from Layout A and Media Player from Layout B is played in the same time, so the sound is played simultaneously at one time.
When i went back to the Layout A, the Media Player in Layout B is stopped and the Media Player in Layout A is stopped and played from the beginning again, it didn't continue the Media Player that was played before.
When the device is locked, the media player is still played although i have used the indicator whether is should be paused or not. Any correction to the code?
I would recommend that you use a Service to play and pause/stop your music. It is not recommended to use Activity to handle music that way. You can start a Service and then play music in it. Services run in background and don't get destroyed automatically too often as compared to Activity. Here's a sample app code that does similar to what you need media player sound continue in all activities
In Activity MediaPlayer can play far.
You should use Service to play MediaPlayer and control from anywhere in application.
I used this tutorial.
https://thenewcircle.com/s/post/60/servicesdemo_using_android_service
I am trying to play two sound items, one after the other
MediaPlayer mp = null;
protected void produceErrorSound(int index) {
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, index);
mp.start();
}
public void correctAnswerAndNext(){
produceErrorSound(R.raw.right1) ;
produceErrorSound(R.raw.right1) ;
}
but only second sound is produced.
is there any alternative approach?
I can't see any wait mechanism in your code.
You can use an onCompletionListener to receive a callback when your first MediaPlayer has finished playback. At that point you can start the second MediaPlayer.
An alternative way in Jellybean (and later versions) is to use the audio chaining functionality (i.e. setNextMediaPlayer) to automatically start another MediaPlayer as soon as the current one has finished playing. Something like this (I've omitted the calls to setDataSource etc for brevity):
mediaPlayer1.prepare();
mediaPlayer2.prepare();
mediaPlayer1.start();
mediaPlayer1.setNextMediaPlayer(mediaPlayer2);
I am creating an android application where I'm playing a music file from my phone. I am implementing this using services. So there is a problem in my service. When I start the service the music from my app starts playing. When I press Home Key and Went to YouTube application and played any video, The service is still running in the background paying the music file. How can I stop my service from playing the music file, when some other music file is started.
Thanks in advance.
Poll the list of running services on start of a new activity and, if it requires use of the speaker, pause yours and let it finish?
Before starting a new service, stop the running service.
When stopping the running service, make sure you stop the player too.
When the Activity or Service is starting, check whether the music is playing or not. If playing, stop it else return null as below:
I think u have the below lines in your onCreate Method of your Activity:
MediaPlayer mp= MediaPlayer.create(this, R.raw.<Your Music File>);
mp.setLooping(true);
Add the below lines of code in onPause, onStop methods in your Activity:
if(mp.isPlaying())
mp.stop();
else
return;
Create two method like this.
For audio play.
private void playAudioFile()
{
try
{
mp=new MediaPlayer();
mp.reset();
mp.setDataSource(audiofileName));
mp.prepare();
mp.start();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
and this for killMedia player.
private void killMediaPlayer()
{
if(mp != null)
{
try
{
mp.release();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
use this done it's work for me.
and call like this
killMediaPlayer();
playaudio();
It now plays on first press, stops on second press or press of another sound button. If I let a sound play through I can restart it or play another sound on a single press.
My goal is for the second press of the same button to stop it, but the press of a new button to just start the new sound instead of first press of new button stopping the old sound, second press starting the new sound.
I can see why it does what it does now but am not sure how to make it work the way I want.
public void playSound(int input){
if (mp!=null && mp.isPlaying()) {
mp.stop();
mp.reset();
} else{
if (mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Soundboard.this, input);
mp.start();
}
}
This block of code:
if (mp!=null){
mp.reset();
mp.release();
}
will never be executed. mp can only be null at this point, as it is in the else block of an if (mp != null) test. This suggests that there is a flaw in your thinking regarding the use of this method.
If a sound has played through and you press the button, then this code will execute:
mp.release();
mp = null;
Since mp was not null, the else block doesn't execute and no new sound is played. When the button is pressed a second time, mp is now null and the else block gets executed, creating a new MediaPlayer and playing the sound.