标签:
0、写在前面
1、实现个静音的功能
PM:『我这里有个需求,很简单很简单那种』RD:『哦,需要做三天』PM:『真的很简单很简单那种』RD:『哦,现在需要做六天了』
private void setMuteEnabled( boolean enabled){ AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, enabled); } |
2、『您好,我是京东快递,您有一个bug签收一下』
QA:『如果我先开启静音,然后退出我们的app再进来,尽管页面显示静音状态,但我无法取消静音啊』RD:『一定是你的用法有问题!』
boolean persistedMute = mute.getContext().getSharedPreferences( "volume" , Context.MODE_PRIVATE).getBoolean( "Volume.Mute" , false ); muteButton.setChecked(persistedMute); |
private void setMuteEnabled( boolean enabled){ AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, enabled); } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/** * Mute or unmute an audio stream. * <p> * The mute command is protected against client process death: if a process * with an active mute request on a stream dies, this stream will be unmuted * automatically. * <p> * The mute requests for a given stream are cumulative: the AudioManager * can receive several mute requests from one or more clients and the stream * will be unmuted only when the same number of unmute requests are received. * <p> * For a better user experience, applications MUST unmute a muted stream * in onPause() and mute is again in onResume() if appropriate. * <p> * This method should only be used by applications that replace the platform-wide * management of audio settings or the main telephony application. * <p>This method has no effect if the device implements a fixed volume policy * as indicated by {@link #isVolumeFixed()}. * * @param streamType The stream to be muted/unmuted. * @param state The required mute state: true for mute ON, false for mute OFF * * @see #isVolumeFixed() */ public void setStreamMute( int streamType, boolean state) { IAudioService service = getService(); try { service.setStreamMute(streamType, state, mICallBack); } catch (RemoteException e) { Log.e(TAG, "Dead object in setStreamMute" , e); } } |
The mute requests for a given stream are cumulative: the AudioManager can receive several mute requests from one or more clients and the stream will be unmuted only when the same number of unmute requests are received.
好像找到答案了。不对呀,我以你的人格担保,我只发了一次静音请求啊,怎么取消静音就这么费劲呢!
4、『这是我的名片』
1
2
3
4
5
6
7
8
|
public void setStreamMute( int streamType, boolean state) { IAudioService service = getService(); try { service.setStreamMute(streamType, state, mICallBack); } catch (RemoteException e) { Log.e(TAG, "Dead object in setStreamMute" , e); } } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
/** @see AudioManager#setStreamMute(int, boolean) */ public void setStreamMute( int streamType, boolean state, IBinder cb) { if (mUseFixedVolume) { return ; } if (isStreamAffectedByMute(streamType)) { if (mHdmiManager != null ) { synchronized (mHdmiManager) { if (streamType == AudioSystem.STREAM_MUSIC && mHdmiTvClient != null ) { synchronized (mHdmiTvClient) { if (mHdmiSystemAudioSupported) { mHdmiTvClient.setSystemAudioMute(state); } } } } } mStreamStates[streamType].mute(cb, state); } } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
public void mute( boolean state) { boolean updateVolume = false ; if (state) { if (mMuteCount == 0 ) { // Register for client death notification try { // mICallback can be 0 if muted by AudioService if (mICallback != null ) { mICallback.linkToDeath( this , 0 ); } VolumeStreamState. this .mDeathHandlers.add( this ); // If the stream is not yet muted by any client, set level to 0 if (!VolumeStreamState. this .isMuted()) { updateVolume = true ; } } catch (RemoteException e) { // Client has died! binderDied(); return ; } } else { Log.w(TAG, "stream: " +mStreamType+ " was already muted by this client" ); } mMuteCount++; } else { if (mMuteCount == 0 ) { Log.e(TAG, "unexpected unmute for stream: " +mStreamType); } else { mMuteCount--; if (mMuteCount == 0 ) { // Unregister from client death notification VolumeStreamState. this .mDeathHandlers.remove( this ); // mICallback can be 0 if muted by AudioService if (mICallback != null ) { mICallback.unlinkToDeath( this , 0 ); } if (!VolumeStreamState. this .isMuted()) { updateVolume = true ; } } } } if (updateVolume) { sendMsg(mAudioHandler, MSG_SET_ALL_VOLUMES, SENDMSG_QUEUE, 0 , 0 , VolumeStreamState. this , 0 ); } } |
01
02
03
04
05
06
07
08
09
10
|
private class VolumeDeathHandler implements IBinder.DeathRecipient { private IBinder mICallback; // To be notified of client‘s death private int mMuteCount; // Number of active mutes for this client VolumeDeathHandler(IBinder cb) { mICallback = cb; } …… } |
5、『其实,刚才不是我』
对呀,有名片啊,问题是我这是同一个app啊,同一个啊……问题出在哪里了呢。
1
|
private final IBinder mICallBack = new Binder(); |
操曰:『天下英雄,唯使君与操耳』玄德大惊曰:『操耳是哪个嘛?』
1
|
AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
1
2
3
4
5
|
@Override public Object getSystemService(String name) { ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name); return fetcher == null ? null : fetcher.getService( this ); } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public Object getService(ContextImpl ctx) { ArrayList<Object> cache = ctx.mServiceCache; Object service; synchronized (cache) { if (cache.size() == 0 ) { // Initialize the cache vector on first access. // At this point sNextPerContextServiceCacheIndex // is the number of potential services that are // cached per-Context. for ( int i = 0 ; i < sNextPerContextServiceCacheIndex; i++) { cache.add( null ); } } else { service = cache.get(mContextCacheIndex); if (service != null ) { return service; } } service = createService(ctx); cache.set(mContextCacheIndex, service); return service; } } |
1
2
3
4
|
registerService(AUDIO_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new AudioManager(ctx); }}); |
等会儿让我想会儿静静。它在这里new了一个AudioManager。它怎么能new了一个AudioManager呢。
1
|
AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
6、『这事儿还是交给同一个人办比较靠谱』
1
|
AudioManager mAudioManager = (AudioManager) getContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE); |
7、结语
标签:
原文地址:http://www.cnblogs.com/krislight1105/p/5203164.html