AudioService 和AudioManager 以及使用举例.

/**
 * The implementation of the volume manager service.
 * <p>
 * This implementation focuses on delivering a responsive UI. Most methods are
 * asynchronous to external calls. For example, the task of setting a volume
 * will update our internal state, but in a separate thread will set the system
 * volume and later persist to the database. Similarly, setting the ringer mode
 * will update the state and broadcast a change and in a separate thread later
 * persist the ringer mode.
 *
 * @hide

 */

frameworks/base/media/java/android/media/AudioService .java


public class AudioService extends IAudioService.Stub {


}

AudioService .java 提供了audio service 的服务实现, IAudioService.aidl 定义了服务的方法.


  private static IAudioService sService;

    /**
     * @hide
     */
    public AudioManager(Context context) {
        mContext = context;
        mMediaPlayers = new ArrayList<MediaPlayerInfo>(1); 
        mUseMasterVolume = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_useMasterVolume);
        mUseVolumeKeySounds = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_useVolumeKeySounds);
        mUseFixedVolume = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_useFixedVolume);
        sAudioPortEventHandler.init();
    }

    private static IAudioService getService()
    {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
        sService = IAudioService.Stub.asInterface(b);
        return sService;
    }
 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
        sService = IAudioService.Stub.asInterface(b)


AudioManager 封装了AudioService的服务,并通过getservice 获取到AudioService的 Service 实例. 


实现了服务和服务的manager, java 应用层的使用方法 ,找到了个例子说明.

导入提供服务的java包

import android.media.AudioManager; 

定义私有对象. 

private AudioManager mAudioManager;


        获取服务并转换成AudioManger 类型.

       mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

 获取原始音量.

        mOriginalMasterVol = mAudioManager.getMasterVolume();


        mShutterVol =  SystemProperties.getInt("persist.camera.snapshot.volume", -1);

        if (mShutterVol >= 0 && mShutterVol <= 100 )

            mAudioManager.setMasterVolume(mShutterVol,0);  调用服务提供的方法设置音量.  



这样就实现了audio的 Service manger 的调用流程.  当然这只是Service 的大概说明. 

总的框架掌握了. 细节慢慢分析吧. 



       




你可能感兴趣的:(AudioService 和AudioManager 以及使用举例.)