Check if music playing in android media player API

一世执手 提交于 2019-12-01 15:23:31

Try this:

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("fileSourceHere");
mediaPlayer.prepare();
mediaPlayer.start();

if(mediaPlayer.isPlaying())
{
    //stop or pause your media player mediaPlayer.stop(); or mediaPlayer.pause();
    mediaPlayer.pause();
}
else
{
    mediaPlayer.start();
}
Xar E Ahmer

To check if Music playing by any other app. Use

AudioManager.isMusicActive();

And if you want to know about your app music.

Add Listener to listen

mediaPlayer.setOnPreparedListener(this);

mediaPlayer.setOnCompletionListener(this);

mediaPlayer.setOnErrorListener(this);

you can add a boolean variable to check isPlaying;

boolean isPlaying= false; //false by default

and when you start mediaPlayer at the very moment set isPlaying=true and you are good to go.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!