检查音乐是否在android media player API中播放 [英] Check if music playing in android media player API

查看:665
本文介绍了检查音乐是否在android media player API中播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的这段代码在android中播放音频文件

I'm using this code below to play an audio file in android

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

我在该程序上有一个按钮.单击该按钮时,它将检查音乐是否正在播放.如果播放音乐,它将停止播放.如何检查音乐是否在播放?我尝试了下面的代码,但没有用

I have a button on that program. When click on that button, it'll check if music playing. If music playing, it'll stop that. How can I check if music playing? I tried the code below but it didn't work

if(mediaPlayer.isPlaying() == true){
 mediaPlayer.pause();
}else{
 mediaPlayer.start();
}

推荐答案

检查音乐是否正在由其他任何应用程序播放.使用

To check if Music playing by any other app. Use

AudioManager.isMusicActive();

如果您想了解自己的应用音乐.

And if you want to know about your app music.

添加监听器以监听

mediaPlayer.setOnPreparedListener(this);

mediaPlayer.setOnCompletionListener(this);

mediaPlayer.setOnErrorListener(this);

您可以添加一个布尔变量来检查isPlaying;

you can add a boolean variable to check isPlaying;

boolean isPlaying= false; //false by default

,此时您将start mediaPlayer设置为isPlaying=true,就可以了.

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

这篇关于检查音乐是否在android media player API中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆