音量总是一样的? [英] Volume always the same?
问题描述
我有以下代码,并且我想在按住音量按钮和当前音量之前获得前一个音量的差异。但是,当我调试时,我发现以前的和当前的音量总是相同的:
这是我的代码:
package curlybrace.ruchir.ivebeenstuckfortwodays;
导入android.content.Context;
导入android.database.ContentObserver;
导入android.media.AudioManager;
导入android.os.Handler;
/ **
*由ruchir于2/5/2016创建。
* /
public class volumeCheck扩展ContentObserver {
int previousVolume;
上下文上下文;
public volumeCheck(Context c,Handler handler){
super(handler); //创建一个新的处理程序
context = c; //前面定义的变量上下文被设置为等于服务上下文c。
AudioManager audio =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE); //检索一个AudioManager来处理音量,铃声模式和音频路由的管理。
previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC); //我们在调用`onChange`之前获得的音量
}
$ b @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); //在我的服务中的方法
$ b}
}
我一直试图弄清楚这个问题2天了,但我不知道为什么这些值是相同的。请帮助。
编辑:
在我的服务的 onCreate中有以下代码
:
mSettingsContentObserver = new volumeCheck(this,new Handler());
getApplicationContext()。getContentResolver()。registerContentObserver(android.provider.Settings.System.CONTENT_URI,true,mSettingsContentObserver);
谢谢
您可能还想在更改后更新您的 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); //我的服务中的方法
previousVolume = currentVolume;
}
I have the following code, and I want to get the difference of the previous volume before holding the volume button and the current volume. But, when I was debugging, I found that the previous and current volume is always the same:
Here is my code:
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
}
}
I have been trying to figure this out for 2 days now, but I don't know why the values are the same. Please help.
Edit:
I have the following code in my service's onCreate
:
mSettingsContentObserver = new volumeCheck(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
Thanks
You probably aren't changing the media volume on your device. To verify go to Settings->Sound & Notification and try changing the Media volume found there.
You probably also want to update update your previousVolume
after a change too:
@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;
}
这篇关于音量总是一样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!