首页 > 其他分享 >Android Audio

Android Audio

时间:2023-08-28 17:22:45浏览次数:66  
标签:index return attr int AudioSystem const Android Audio

1.最常接触到的audioservice

frameworks\base\services\core\java\com\android\server\audio\AudioService.java

初始化音量的代码

// Initialize volume
        // Priority 1 - Android Property
        // Priority 2 - Audio Policy Service
        // Priority 3 - Default Value
        if (AudioProductStrategy.getAudioProductStrategies().size() > 0) {
            int numStreamTypes = AudioSystem.getNumStreamTypes();

            for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) {
                AudioAttributes attr =
                        AudioProductStrategy.getAudioAttributesForStrategyWithLegacyStreamType(
                                streamType);
                int maxVolume = AudioSystem.getMaxVolumeIndexForAttributes(attr);
                if (maxVolume != -1) {
                    MAX_STREAM_VOLUME[streamType] = maxVolume;
                }
                int minVolume = AudioSystem.getMinVolumeIndexForAttributes(attr);
                if (minVolume != -1) {
                    MIN_STREAM_VOLUME[streamType] = minVolume;
                }
            }
            if (mUseVolumeGroupAliases) {
                // Set all default to uninitialized.
                for (int stream = 0; stream < AudioSystem.DEFAULT_STREAM_VOLUME.length; stream++) {
                    AudioSystem.DEFAULT_STREAM_VOLUME[stream] = UNSET_INDEX;
                }
            }
        }

2.进入到audiosystem中获取最大音量getMaxVolumeIndexForAttributes

frameworks\base\media\java\android\media\AudioSystem.java

/**
     * @hide
     * get the maximum volume index for the given {@link AudioAttributes}.
     * @param attributes the {@link AudioAttributes} to be considered
     * @return maximum volume index for the given {@link AudioAttributes}.
     */
    public static native int getMaxVolumeIndexForAttributes(@NonNull AudioAttributes attributes);

方法为native方法,具体实现在frameworks\base\core\jni\android_media_AudioSystem.cpp

static jint
android_media_AudioSystem_getMaxVolumeIndexForAttributes(JNIEnv *env,
                                                         jobject thiz,
                                                         jobject jaa)
{
    // read the AudioAttributes values
    JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
    jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
        return jStatus;
    }
    int index;
    if (AudioSystem::getMaxVolumeIndexForAttributes(*(paa.get()), index)
            != NO_ERROR) {
        index = -1;
    }
    return (jint) index;
}

3.调用audiosystem的getMaxVolumeIndexForAttributes方法

frameworks\av\media\libaudioclient\AudioSystem.cpp

status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) {
    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    if (aps == 0) return PERMISSION_DENIED;

    media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_attributes_t_AudioAttributesInternal(attr));
    int32_t indexAidl;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl)));
    index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl));
    return OK;
}

4.进入IAudioPolicyService中进行获取

// establish binder interface to AudioPolicy service
const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
    sp<IAudioPolicyService> ap;
    sp<AudioPolicyServiceClient> apc;
    {
        Mutex::Autolock _l(gLockAPS);
        if (gAudioPolicyService == 0) {
            sp<IServiceManager> sm = defaultServiceManager();
            sp<IBinder> binder;
            do {
                binder = sm->getService(String16("media.audio_policy"));
                if (binder != 0)
                    break;
                ALOGW("AudioPolicyService not published, waiting...");
                usleep(500000); // 0.5 s
            } while (true);
            if (gAudioPolicyServiceClient == NULL) {
                gAudioPolicyServiceClient = new AudioPolicyServiceClient();
            }
            binder->linkToDeath(gAudioPolicyServiceClient);
            gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
            LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
            apc = gAudioPolicyServiceClient;
            // Make sure callbacks can be received by gAudioPolicyServiceClient
            ProcessState::self()->startThreadPool();
        }
        ap = gAudioPolicyService;
    }
    if (apc != 0) {
        int64_t token = IPCThreadState::self()->clearCallingIdentity();
        ap->registerClient(apc);
        ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
        ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
        IPCThreadState::self()->restoreCallingIdentity(token);
    }

    return ap;
}

最终进入到AudioPolicyManager进行获取

frameworks\av\services\audiopolicy\managerdefault\AudioPolicyManager.cpp

status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr,
                                                            int &index)
{
    index = getVolumeCurves(attr).getVolumeIndexMax();
    return NO_ERROR;
}

frameworks\av\services\audiopolicy\managerdefault\AudioPolicyManager.h

IVolumeCurves &getVolumeCurves(const audio_attributes_t &attr)
        {
            auto *curves = mEngine->getVolumeCurvesForAttributes(attr);
            ALOG_ASSERT(curves != nullptr, "No curves for attributes %s", toString(attr).c_str());
            return *curves;
        }

mEngine是EngineInstance 类型

EngineInstance mEngine;

frameworks\av\services\audiopolicy\engineconfigurable\src\EngineInstance.cpp

具体实现在frameworks\av\services\audiopolicy\engine\common\src\EngineBase.cpp

VolumeCurves *EngineBase::getVolumeCurvesForAttributes(const audio_attributes_t &attr) const
{
    volume_group_t volGr = mProductStrategies.getVolumeGroupForAttributes(attr);
    const auto &iter = mVolumeGroups.find(volGr);
    LOG_ALWAYS_FATAL_IF(iter == std::end(mVolumeGroups), "No volume groups for %s", toString(attr).c_str());
    return mVolumeGroups.at(volGr)->getVolumeCurves();
}

frameworks\av\services\audiopolicy\engine\common\include\VolumeCurve.h

int getVolumeIndexMax() const { return mIndexMax; }

 

   

标签:index,return,attr,int,AudioSystem,const,Android,Audio
From: https://www.cnblogs.com/wanglongjiang/p/17662890.html

相关文章

  • cocos2dx 如何编译android 打包
    先要配置NDK,然后启动CMD命令进入到自己的游戏根目录,我的是starGame,所以如上所示:......
  • 迅为RK3588开发板Android12 设置系统默认不锁屏
    修改frameworks/base/packages/SettingsProvider/res/values/defaults.xml文件,修改为如下所示:-<boolname="def_lockscreen_disabled">false</bool>+<boolname="def_lockscreen_disabled">true</bool>修改完,保存修改,重新编译android源码。......
  • android多模块 安卓模块是什么意思
    模块化在进入组件化之前,我们先说一下模块化。一个功能分为一个模块,例如登录模块,支付模块,广告模块。传统的开发模式中一个模块就是一个Module(也有不同模块放在不同包里面的情况)。模块在功能上对代码进行了划分,但是在开发上任然存在问题。例如 当需要debug某一个模块的时候此......
  • 直播商城源码,android xml中设置水平虚线及竖直虚线
    直播商城源码,androidxml中设置水平虚线及竖直虚线水平虚线:line_stroke <?xmlversion="1.0"encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"  android:shape="line">  <stroke    and......
  • 成品直播源码推荐,android自定义显示图片+文字
    成品直播源码推荐,android自定义显示图片+文字 /** *@authorMartin-harry *@date2021/8/10 *@address *@Desc自定义toast */publicclassToastUtil{  /**   *显示文本+图片   *@paramcontext   *@parammessage   */  publicsta......
  • Android平台RTMP|RTSP直播播放器功能进阶探讨
    我们需要怎样的直播播放器?很多开发者在跟我聊天的时候,经常问我,为什么一个RTMP或RTSP播放器,你们需要设计那么多的接口,真的有必要吗?带着这样的疑惑,我们今天聊聊Android平台RTMP、RTSP播放器常规功能,如软硬解码设置、实时音量调节、实时快照、实时录像、视频view翻转和旋转、画面填充......
  • jdk1.8 AudioSystem 无法关闭流的问题
    问题首先说明JDK版本,EclipseTemurin1.8.0_382,写音频处理时遇到一个文件流无法关闭的问题。具体是javax.sound.sampled.AudioSystem#getAudioInputStream(java.io.File)写在try-with-resources里,在try-with-resources结束的代码块外删除文件显示文件被占用,最后在stackov......
  • 【面试小抄】2023最全Android面试综合题集,冲刺金九银十
    前言还有不到一个星期的时间就是金九银十了,前几个月一直在刷面试题,好为金九银十做准备,因此也在大量的面试题中总结了很多知识,今天特地将总结的内容分享给有需要的朋友,让他们把握好最后的冲刺时间。当然我并不提倡死背面试题,所以里面的内容除了面试题和答案,还有充分的讲解,希望大家能......
  • 现在开发需要兼容Android 和 iOS 手机的大型App,优选什么框架?
    前言现在最主流的跨平台方案应该也就是Flutter了,再就是RN,不过RN已经在慢慢退出历史舞台。中小企业目前最优先选择的还是Flutter,Flutter现在的生态也逐渐很完善。不过大型App,我还是推荐混编,一些不重要的模块使用Flutter,重要的模块还是使用原生的好。除了Fluttet的方案,还有一种方案可......
  • Android并发编程高级面试题汇总(含详细解析 十)
    Android并发编程高级面试题汇总最全最细面试题讲解持续更新中......