//获取实例 AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); //获取/设置系统音量 audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM); audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE , AudioManager.FLAG_SHOW_UI); //获取/设置音乐音量 audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE , AudioManager.FLAG_SHOW_UI); //其他的类似 //获取/设置铃声的模式 int ringMode = audioManager.getRingerMode(); //普通模式 audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); //静音模式 audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); //其他的类似 //设置声音流静音/不静音 //音乐静音 audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true); //铃声不静音 audioManager.setStreamMute(AudioManager.STREAM_RING, false); //其他的类似
由于知识水平有限,我觉得下面的代码自己分析不好,建议看这篇博文:http://blog.csdn.net/qinjuning/article/details/6938436
/* 为Action == “MEDIA_BUTTON”注册广播接收者 用来广播“耳机插入”的事件 eventReceiver一般接受的参数为这样一个ComponentName对象 如: AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); ComponentName component = new ComponentName(context.getApplicationContext(),MediaControlReceiver.class); audioManager.registerMediaButtonEventReceiver(component); */ public void registerMediaButtonEventReceiver(ComponentName eventReceiver) { if (eventReceiver == null) { return; } if (!eventReceiver.getPackageName().equals(mContext.getPackageName())) { Log.e(TAG, "registerMediaButtonEventReceiver() error: " + "receiver and context package names don't match"); return; } // construct a PendingIntent for the media button and register it Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); // the associated intent will be handled by the component being registered mediaButtonIntent.setComponent(eventReceiver); PendingIntent pi = PendingIntent.getBroadcast(mContext, 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); registerMediaButtonIntent(pi, eventReceiver); } /** 为MediaButton注册一个Intent 可以发现,这边也是通过aidl的方式进行的调用 IAudioService的aidl:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/IAudioService.aidl */ public void registerMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) { if ((pi == null) || (eventReceiver == null)) { Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter"); return; } IAudioService service = getService(); try { // pi != null service.registerMediaButtonIntent(pi, eventReceiver); } catch (RemoteException e) { Log.e(TAG, "Dead object in registerMediaButtonIntent"+e); } } /** * Unregister the receiver of MEDIA_BUTTON intents. * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver} * that was registered with {@link #registerMediaButtonEventReceiver(ComponentName)}. */ public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) { if (eventReceiver == null) { return; } // construct a PendingIntent for the media button and unregister it Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); // the associated intent will be handled by the component being registered mediaButtonIntent.setComponent(eventReceiver); PendingIntent pi = PendingIntent.getBroadcast(mContext, 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); unregisterMediaButtonIntent(pi, eventReceiver); } /** * @hide */ public void unregisterMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) { IAudioService service = getService(); try { service.unregisterMediaButtonIntent(pi, eventReceiver); } catch (RemoteException e) { Log.e(TAG, "Dead object in unregisterMediaButtonIntent"+e); } } /** * Registers the remote control client for providing information to display on the remote * controls. * @param rcClient The remote control client from which remote controls will receive * information to display. * @see RemoteControlClient */ public void registerRemoteControlClient(RemoteControlClient rcClient) { if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { return; } IAudioService service = getService(); try { service.registerRemoteControlClient(rcClient.getRcMediaIntent(), /* mediaIntent */ rcClient.getIRemoteControlClient(), /* rcClient */ // used to match media button event receiver and audio focus mContext.getPackageName()); /* packageName */ } catch (RemoteException e) { Log.e(TAG, "Dead object in registerRemoteControlClient"+e); } } /** * Unregisters the remote control client that was providing information to display on the * remote controls. * @param rcClient The remote control client to unregister. * @see #registerRemoteControlClient(RemoteControlClient) */ public void unregisterRemoteControlClient(RemoteControlClient rcClient) { if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { return; } IAudioService service = getService(); try { service.unregisterRemoteControlClient(rcClient.getRcMediaIntent(), /* mediaIntent */ rcClient.getIRemoteControlClient()); /* rcClient */ } catch (RemoteException e) { Log.e(TAG, "Dead object in unregisterRemoteControlClient"+e); } } /** * @hide * Registers a remote control display that will be sent information by remote control clients. * @param rcd */ public void registerRemoteControlDisplay(IRemoteControlDisplay rcd) { if (rcd == null) { return; } IAudioService service = getService(); try { service.registerRemoteControlDisplay(rcd); } catch (RemoteException e) { Log.e(TAG, "Dead object in registerRemoteControlDisplay " + e); } } /** * @hide * Unregisters a remote control display that was sent information by remote control clients. * @param rcd */ public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { if (rcd == null) { return; } IAudioService service = getService(); try { service.unregisterRemoteControlDisplay(rcd); } catch (RemoteException e) { Log.e(TAG, "Dead object in unregisterRemoteControlDisplay " + e); } }