In one of my activity, there is a button to play music using mediaPlayer. Also it has an option to play the music in background or stop when the activity is finished.
I use this code for that.
#Override
protected void onStop() {
super.onStop();
SharedPreferences perfs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (!perfs.getBoolean("bg_play", true)) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
That works perfectly. Now the problem is when the user is back on the activity and press the play button, it starts a new mediaPlayer and play the new music. I want to stop the previous music running in background when new music is started.
Here is my code
private void myMediaPlayer() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
else {
mediaPlayer.stop();
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"file.mp3");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}
And the MediaPlayer declaration
MediaPlayer mediaPlayer=new MediaPlayer();
before onCreate
I'm a beginner to programing so I might be wrong. .. Wouldn't making the mefiaPlayer variable static work? But I think the problem is that when you go back to the activity, the activity is being rebuilt.
Related
i have many Imageviews to play sound when clicked and i managed to play it.
but the problem that when i want to click on another image which supposed to play another sound, it stops the sound but i can't resume if it was clicked on the same play button.
Here is the code I've managed to get it to work so far:
audio1 = (ImageView) popupView.findViewById(R.id.lesson1_audio_gramar_1);
assert audio1 != null;
audio1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mMediaPlayer == null) {
mMediaPlayer = MediaPlayer.create(Lesson1Activity.this, R.raw.lesson1);
}
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
audio1.setImageResource(R.drawable.ic_play_circle_filled_black_48dp);
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
mMediaPlayer.start();
audio1.setImageResource(R.drawable.ic_pause_circle_filled_black_48dp);
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
audio2 = (ImageView) popupView.findViewById(R.id.lesson1_audio_gramar_2);
assert audio2 != null;
audio2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mMediaPlayer == null) {
mMediaPlayer = MediaPlayer.create(Lesson1Activity.this, R.raw.lesson2);
}
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
audio2.setImageResource(R.drawable.ic_play_circle_filled_black_48dp);
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
mMediaPlayer.start();
audio2.setImageResource(R.drawable.ic_pause_circle_filled_black_48dp);
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
To play another track, follow this simple steps:
if (mediaPlayer != null) {
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset()
mediaPlayer.setDataSource(new music to play);
mediaPlayer.prepare();
}
}
Then call mediaPlayer.start() in the onPrepared() method
I have a media player which player mp3 from url. I want to show progress dialog when audio is loading from url. Here is my code.. I am new one so please don't mind if you cannot understand well my question. Thanks in advance
play function
public void playSong(int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setText("Buffering....");
tv.setSelected(true);
final ProgressDialog progressDialog = ProgressDialog.show(this,
"Loading Title", "Loading Message");
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss();
}
mp.start();
}
});
try {
mp.reset();
mp.setDataSource(naatpaths[naatindex]);
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setSelected(true);
mp.prepare();
// Set focus to the textview
tv.setText(naattitles[naatindex]);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
You must use prepareAsync(); and create another textview which text is buffering. while prepareAsync() visible buffering textview. and on prepared complete lister hide buffering textview. I hope that work
public void playSong(final int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);
bf = (TextView) this.findViewById(R.id.showbuffering);
try {
mp.reset();
mp.setDataSource(naatpaths[naatindex]);
tv.setVisibility(View.INVISIBLE);
bf.setVisibility(View.VISIBLE);
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
bf.setVisibility(View.INVISIBLE);
tv.setVisibility(View.VISIBLE);
tv.setSelected(true);
mp.start();
tv.setText(naattitles[naatindex]);
tv.setSelected(true);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(o);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
}
});
// Set focus to the textview
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
can someone help me with this. i'm doing a project in which user can watch video and i use surface view, and media player.. Now, i want to have a small screen that plays a video and continues playing on the big screen.. what i have in my code now, is i have two different surface view on the same activity when i click play button it plays video but when i click full screen it does'nt continue playing, its just play the video from the start.
public class MainActivity extends Activity implements OnClickListener ,
SurfaceHolder.Callback{
//For surface
MediaPlayer mediaPlayer, mediaPlayer_fullscreen;
SurfaceView surfaceView, surfaceView_fullscreen;
SurfaceHolder surfaceHolder, surfaceholder_fullscreen;
boolean pausing = false;
boolean pausing_2 = false;
boolean reset = false;
public static int media_length;
//FULL SCREEN
Button play, pause;
ImageButton mExit, resume;
public static int mediaPlayer_length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//For Surface
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
//FULLSCREEN
//For Surface
surfaceView_fullscreen =
(SurfaceView)findViewById(R.id.surfaceView2);
surfaceholder_fullscreen = surfaceView_fullscreen.getHolder();
surfaceholder_fullscreen.addCallback(this);
surfaceholder_fullscreen.setFixedSize(300, 300);
surfaceholder_fullscreen.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer_fullscreen = new MediaPlayer();
media_length = mediaPlayer.getCurrentPosition();
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.play:
playvideo();
break;
case R.id.view_fullscreen:
mediaPlayer.release();
linearlayout_head.setVisibility(View.GONE);
linearlayout_fullscreen.setVisibility(View.VISIBLE);
playvideo_fullscreen();
break;
}
public void playvideo() {
path_path = EditText_path.getText().toString();
pausing = false;
//reset = false;
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(getDataSource(path_path));
Log.d("LOGPLAY", path_path);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
//mediaPlayer.pause();
//media_length = mediaPlayer.getCurrentPosition();
}
public void playvideo_fullscreen() {
pausing_2 = false;
//reset = false;
if(mediaPlayer_fullscreen.isPlaying()){
mediaPlayer_fullscreen.reset();
//mediaPlayer_fullscreen.seekTo(media_length);
}
mediaPlayer_fullscreen.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer_fullscreen.setDisplay(surfaceholder_fullscreen);
try {
mediaPlayer_fullscreen.setDataSource(getDataSource(path_path));
Log.d("LOGPLAYFullscreen", path_path);
mediaPlayer_fullscreen.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//mediaPlayer_fullscreen.start();
mediaPlayer_fullscreen.seekTo(media_length);
}
can someone help me with this please. Thank you.
When you switch between two views media player will not resume where you are paused. So you must do it manual. You will get the current time of video using
MediaPlayer mp=new MediaPlayer();
int currentTime=mp.getCurrentPosition();
After switching the view just seek the video to the 'currentTime'.
mp.seekTo(currentTime);
I generate midi files on the go. I want to play these files continuously.
I initialise a mediaplayer and start song1.mid. Then I use the following code to play song2.mid
// set on completion listener music file
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
String filePath2 = null;
File file = null;
FileInputStream inputStream = null;
//set the filePath
try {
filePath2 = getCacheDir() + "/optimuse" + song + ".mid";
file = new File(filePath2);
if (file.exists()) {
inputStream = new FileInputStream(file);
if (inputStream.getFD().valid()) {
System.out.println("Valid!");
}
}
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
//set Mediaplayer's datasource
if (file.exists()) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//if the player is not running
if (!mediaPlayer.isPlaying()) {
//start the player
mediaPlayer.start();
Toast.makeText(MainActivity.this,
"mediaPlayer.start()", Toast.LENGTH_LONG)
.show();
}
}
});
}
});
The problem is that the mediaplayer stops after song2. But I want song3 to start. I can increment the global variable, ok. But the onCompletionListener does not seem to work when the second song finishes.
I guess I should initialise MediaPlayer mp to have an onCompletionListener too? Not sure what the best approach is.
Or should I do something like:
new class MediaPlayer implements OnCompletionListener(){
#Override
song++;
//code to start the mediaplayer
}
Thank you for putting me in the right direction. I am also a little bit concerned with efficiency, when I keep starting up new mediaplayers...
Basically, what I want to do is play song1.mid, song2.mid,... continuously, but the files are generated during the program.
EDIT
Thanks to the wonderful help of #Gan. I know have the following working code:
// set on completion listener music file
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
String filePath2 = null;
File file = null;
FileInputStream inputStream = null;
//set the filePath
try {
filePath2 = getCacheDir() + "/optimuse" + song + ".mid";
file = new File(filePath2);
if (file.exists()) {
inputStream = new FileInputStream(file);
if (inputStream.getFD().valid()) {
System.out.println("Valid!");
}
}
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
//set Mediaplayer's datasource
if (file.exists()) {
try {
mp.stop();
mp.reset();
mp.setDataSource(inputStream.getFD());
inputStream.close();
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//if the player is not running
if (!mp.isPlaying()) {
//start the player
mp.start();
Toast.makeText(MainActivity.this,
"mp.start()", Toast.LENGTH_LONG)
.show();
}
}
});
}
});
store the source locations in an array and in the onCompletionListener cycle through the source. something like this (use the same media player instance)
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
if(currentPosition<sourceArray.size())
{
mediaPlayer.reset();
/* load the new source */
mediaPlayer.setDataSource(sourceArray[position]);
/* Prepare the mediaplayer */
mediaPlayer.prepare();
/* start */
mediaPlayer.start();
}
else
{
/* release mediaplayer */
mediaPlayer.release();
}
}
I have 2 buttons that both play an mp3. When the first song is playing I want a click of the second song to stop the first song, and start the song that was clicked. also if the first button is pressed twice the second press resets the song.
basically I don't want the songs to play on top of each other when multiple buttons are pressed.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.placeyouknow:
String url = "http://www.katastro.com/audio/facts/01%20That%20Place%20You%20Know.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case R.id.fallen:
String url1 = "http://www.katastro.com/audio/fallen/01%20Fallen.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(url1);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Seems like you need to implement a state machine to manage your player (start-stop-restart) instead of creating a new player every time a button is pressed
Here is a sample sound helper class I use for simple apps. I use it to play at most one resource at a time within my app, but you could modify it to play audio from some url instead.
public class Sound
{
private static MediaPlayer mp = null;
/** Stop old sound and start new one */
public static void play(Context context, int resource)
{
stop(context);
mp = MediaPlayer.create(context, resource);
mp.setLooping(false);
mp.start();
}
/** Stop the music */
public static void stop(Context context)
{
if (mp != null)
{
mp.stop();
mp.release();
mp = null;
}
}
}
Whoooaaaaa, losing handles on MediaPlayers left and right.
First of all, for this particular activity, you should only put
mp = new MediaPlayer();
in the onCreate() method of your activity. Delete the two you call in the onClick() method and move it to the onCreate() method.
From there, all you have to do is include a call to mp.reset() right before you call mp.setDataSource() and it should work just like you want it to!
Cheers