我正在创建一个应用程序,当任何音乐播放器播放音乐时启动并运行一个过程,并在音乐暂停时(或在通话期间)暂停该过程;我以为我可以使用FileObserver查看音乐文件何时被访问(它需要能够分辨出正在播放哪首歌曲),但每当我让观察者运行并启动音乐播放器时,一堆文件似乎立即访问。我的问题是:
我不确定我的观察者是否有问题,所以这就是:
observer = new FileObserver(observedPath) {
@Override
public void onEvent(int event,final String file) {
event &= FileObserver.ALL_EVENTS;
switch(event) {
case FileObserver.DELETE_SELF:
break;
case FileObserver.OPEN:
case FileObserver.ACCESS:
if(changesRunning > 0)
{
stopCurrentChange.post(new Runnable() {
@Override
public void run() {
return;
}
});
}
mainThreadHandler.post(new Runnable() {
@Override
public void run()
{
Log.v(TAG,"The event was triggered.");
}
});
handler.post(new Runnable() {
@Override
public void run() {
changesRunning++;//global variable being used
GatherChangeVolume(username, deviceName, file);
changesRunning = 0;
}
});
break;
default:
break;
}
这是我的logcat输出:
08-10 17:30:19.874 3398-3398/badmexican333.volumizer17 V/TweakRunner: Service trying to watch /storage/sdcard1/Music/
08-10 17:30:34.309 3398-3398/badmexican333.volumizer17 V/TweakRunner: The event was triggered.
08-10 17:30:34.310 3398-3799/badmexican333.volumizer17 A/FileObserver: Unhandled exception in FileObserver badmexican333.volumizer17.TweakRunner$1@21c3ca00
java.lang.NullPointerException
at badmexican333.volumizer17.TweakRunner$1.onEvent(TweakRunner.java:123)
at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
at android.os.FileObserver$ObserverThread.observe(Native Method)
at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
08-10 17:30:34.314 3398-3799/badmexican333.volumizer17 A/FileObserver: Unhandled exception in FileObserver badmexican333.volumizer17.TweakRunner$1@21c3ca00
java.lang.NullPointerException
at badmexican333.volumizer17.TweakRunner$1.onEvent(TweakRunner.java:123)
at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
at android.os.FileObserver$ObserverThread.observe(Native Method)
at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
08-10 17:30:34.314 3398-3398/badmexican333.volumizer17 V/TweakRunner: The event was triggered.
08-10 17:30:34.318 3398-3398/badmexican333.volumizer17 V/TweakRunner: The event was triggered.
08-10 17:30:34.319 3398-3799/badmexican333.volumizer17 A/FileObserver: Unhandled exception in FileObserver badmexican333.volumizer17.TweakRunner$1@21c3ca00
java.lang.NullPointerException
at badmexican333.volumizer17.TweakRunner$1.onEvent(TweakRunner.java:123)
at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
at android.os.FileObserver$ObserverThread.observe(Native Method)
at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
08-10 17:30:34.425 3398-3398/badmexican333.volumizer17 V/TweakRunner: The event was triggered.
08-10 17:30:34.425 3398-3799/badmexican333.volumizer17 A/FileObserver: Unhandled exception in FileObserver badmexican333.volumizer17.TweakRunner$1@21c3ca00
java.lang.NullPointerException
at badmexican333.volumizer17.TweakRunner$1.onEvent(TweakRunner.java:123)
at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
at android.os.FileObserver$ObserverThread.observe(Native Method)
at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
08-10 17:30:34.480 3398-3799/badmexican333.volumizer17 A/FileObserver: Unhandled exception in FileObserver badmexican333.volumizer17.TweakRunner$1@21c3ca00
java.lang.NullPointerException
at badmexican333.volumizer17.TweakRunner$1.onEvent(TweakRunner.java:123)
at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
at android.os.FileObserver$ObserverThread.observe(Native Method)
at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
08-10 17:30:34.484 3398-3398/badmexican333.volumizer17 V/TweakRunner: The event was triggered.
08-10 17:30:34.571 3398-3398/badmexican333.volumizer17 V/TweakRunner: The event was triggered.
在停止之前,输出在此模式中持续一段时间。
我是Android开发的新手,非常感谢任何帮助。
答案 0 :(得分:2)
您可以使用AudioManager
API轻松检查当前是否播放音乐。
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.isMusicActive()) {
// music is playing
}