gpt4 book ai didi

android - android如何判断何时播放/暂停音乐

转载 作者:行者123 更新时间:2023-12-03 00:14:34 25 4
gpt4 key购买 nike

我正在制作一个可在任何音乐播放器播放音乐时启动并运行过程的应用程序,并在音乐暂停时(或在通话期间)暂停该过程;我以为我可以使用FileObserver来查看何时访问音乐文件(它需要能够判断正在播放哪首歌曲),但是每当我运行观察者并启动音乐播放器时,似乎就会出现很多文件立即访问。我的问题是:

  • 是否有更好的方法(或任何方法)来判断何时从任何播放器播放音乐?
  • 如果启动另一首歌曲,如何停止运行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开发的新手,任何帮助将不胜感激。

    最佳答案

    您可以使用AudioManager API轻松检查当前是否正在播放音乐。

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.isMusicActive()) {
    // music is playing
    }

    另请: AudioManager.isMusicActive()

    关于android - android如何判断何时播放/暂停音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884698/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com