gpt4 book ai didi

java - 音量总是一样?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:19:50 25 4
gpt4 key购买 nike

我有以下代码,我想在按住音量按钮之前获得上一个音量与当前音量之间的差异。但是,我在调试的时候发现,之前的音量和当前的音量总是一样的:

enter image description here

这是我的代码:

package curlybrace.ruchir.ivebeenstuckfortwodays;

import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.os.Handler;


/**
* Created by ruchir on 2/5/2016.
*/
public class volumeCheck extends ContentObserver {
int previousVolume;
Context context;

public volumeCheck(Context c, Handler handler) {
super(handler); //Creates a new handler
context=c; //variable context, defined earlier, is set equal to c, context of service.

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //retrieve an AudioManager for handling management of volume, ringer modes and audio routing.
previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC); //The volume that we get before the `onChange` is called
}

@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

processVolumeChange(previousVolume, currentVolume);
}

public void processVolumeChange(int previousVolume, int currentVolume) {

MyService mService = new MyService();
mService.volumeCheck(previousVolume - currentVolume); //Method in my service


}
}

我已经尝试解决这个问题 2 天了,但我不知道为什么这些值是相同的。请帮忙。

编辑:

我的服务的 onCreate 中有以下代码:

     mSettingsContentObserver = new volumeCheck(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);

谢谢

最佳答案

您可能没有更改设备上的媒体音量。要验证,请转至设置 -> 声音和通知并尝试更改在那里找到的媒体音量。

enter image description here

您可能还想在更改后更新您的 previousVolume:

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

processVolumeChange(currentVolume);
}

public void processVolumeChange(int currentVolume) {
MyService mService = new MyService();
mService.volumeCheck(previousVolume - currentVolume); //Method in my service
previousVolume = currentVolume;
}

关于java - 音量总是一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35256364/

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