I'm trying to cancel an animation with a very simply way just to see whether if .cancel() function doing what it is intended to do.
But it seems it just does not work.
Here is the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.my_text_view);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_animation);
textView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animation.cancel();
}
}, 2000);
}
}
The animation is running forever. It just doesn't stop after 2 seconds.
If I call it from the view:
textView.getAnimation().cancel();
It doesn't work either.
What do you think what makes this? I need a proper way to stop an animation.
I have 3 activity, when I move from 1st act.to 2nd act. one music will start, and when I move from 2nd act. to 3rd act., 2nd activity's music should stop and 3rd activity's music should start. As per my coding, 2nd activity's music is stopping, but 3rd activity's music does not start. my code is given here-under: please help me.
//1st activity code start
public class MainActivity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this,R.raw.bakaratwozeroone);
b = (Button) findViewById(R.id.butmove);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
}
}
//1st activity code end
//2nd activity code start
public class Main2Activity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this, R.raw.mumfive);
b = (Button) findViewById(R.id.butmove2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(Main2Activity.this, Main3Activity.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
if(mp !=null);
mp.release();
mp = null;
}
}
//2nd activity code end
No need to keep MediaPlayer instance on every activity, Use BoundService to run media player and make communication with Service and activities.
For reference,use this link
https://github.com/SimpleMobileTools/Simple-Music-Player
I want to immediately start playing a sound when the activity is first launched and I want the MediaPlayerControl object to reflect this change. On the code given below the controls will start with play-mode activated instead of pause-mode (since the file is already playing).
public class Guide extends AppCompatActivity implements MediaPlayerControl, MediaPlayer.OnPreparedListener {
private MediaPlayer m_audio_player;
private MediaController m_audio_controller;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
set_up_audio();
}
private void set_up_audio(){
m_audio_player = MediaPlayer.create(this, *SOMESOUNDFILE*);
m_audio_player.setOnPreparedListener(this);
m_audio_controller = new MediaController(this);
}
public void onPrepared(MediaPlayer mediaPlayer) {
m_audio_controller.setMediaPlayer(this);
m_audio_controller.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
m_audio_controller.setEnabled(true);
m_audio_controller.show();
m_audio_player.start();
// how do I update m_audio_controller's state here?
}
});
}
}
how can I do this?
I've found the solution. If I call show after first having started the player, it does work.
m_audio_player.start();
m_audio_controller.show();
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 new to Android programming and i have a problem that i can not solve without your help...Please, give me a hand... ;-)
When i start playing my audio file, i can not stop it. Instead, I am quitting the application.
I play audio file with this code:
public class Word1Audio extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word1video);
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.lige);
mediaPlayer.start();
}
}
HOWEVER, when i try to stop it when the back button is pressed by this piece of code
#Override
public void onBackPressed() {
// do something on back.
return;
}
my applications closes down and audio continues to play...
Do you have an idea why the application closes down? And how can i just stop the music and go back to the previous page...??
Thank you for your time and help... :-)
you could try
MediaPlayer mediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word1video);
mediaPlayer = MediaPlayer.create(this, R.raw.lige);
mediaPlayer.start();
}
#Override
public void onBackPressed() {
if( mediaPlayer.isPlaying() ) {
mediaPlayer.stop();
}
super.onBackPressed();
}