Android 5.0 以上开机音乐实现

在Android 4.4上开机音乐,要实现开机音乐。通过在BootAnimation 中加入以下代码便可

void BootAnimation::playMusic()
{
    sp<MediaPlayer> mp = new MediaPlayer();

    if ((0 == access(BOOTMUSIC_FILE, F_OK)) && mp != NULL) {
        mp->setDataSource(BOOTMUSIC_FILE, NULL);
        mp->setVolume(0.5,0.5);
        mp->prepare();
        mp->start();
    }
}

但是在5.1 和 6.0 上以上步骤就行不通了。由于BootAnimation是由surfaceflinger 启动,在5.0以上系统中,由于BootAnimation 启动的时候,Mediasever 还没有完全启动,所以上面代码不能播放音乐。至于为什么在4.4 上Mediasever 已经启动,而5.1 以上系统没有启动呢? 因为在5.1 系统服务比4.4 多了一个电池统计服务,启动较慢导致Mediasever 没有启动成功。

那怎么样才能播放开机音乐呢?其实在5.1 以上的系统android 在BootAnimation中预留了AudioPlayer的服务。它可以直接操作pcm 播放wav 格式的音乐。但是前提是你要自己配置audio_conf 文件。下面是笔者自己的配置文件。这个文件的配置要和硬件保持一致,具体配置可以参考hardware 中关于音频硬件的配置,笔者的硬件是RT3261

card=0
device=0
period_size=1024
period_count=4
mixer "I2S Switch"=1
mixer "ASRC Switch"=Disable
mixer "Speaker Playback Switch"=0 0
mixer "OUT Playback Switch"=0 0
mixer "Modem Input Switch"=0 0
mixer "DAC L2 Mux"=IF2
mixer "DAC R2 Mux"=IF2
mixer "Mono dacr Mux"=TxDC_R
mixer "DACR Select"=IF2_DAC
mixer "Mono DAC MIXL DAC L2 Switch"=1
mixer "Mono DAC MIXR DAC R2 Switch"=1
mixer "DAC MIXL INF1 Switch"=1
mixer "DAC MIXR INF1 Switch"=1
mixer "Stereo DAC MIXL DAC L1 Switch"=1
mixer "Stereo DAC MIXR DAC R1 Switch"=1
mixer "SPK MIXL DAC L2 Switch"=1
mixer "SPK MIXR DAC R2 Switch"=1    
mixer "Speaker Playback Volume"=32 31
mixer "Mono DAC Playback Volume"=175 173
mixer "DAC1 Playback Volume"=175 173
mixer "Class D SPK Ratio Control"=10  
mixer "RECMIXL BST2 Switch"=0
mixer "Stereo ADC MIXL ADC1 Switch"=0
mixer "DAC MIXL Stereo ADC Switch"=0
mixer "Mono DAC MIXL DAC L1 Switch"=0
mixer "OUT MIXL DAC L2 Switch"=0
mixer "SPK MIXL OUT MIXL Switch"=0
mixer "OUT MIXR DAC L2 Switch"=0
mixer "SPK MIXR OUT MIXR Switch"=0
mixer "Stereo DAC MIXR DAC R2 Switch"=0
mixer "LOUT MIX DAC R1 Switch"=0
mixer "Stereo DAC MIXL DAC L2 Switch"=0
mixer "LOUT MIX DAC L1 Switch"=0
mixer "OUT MIXR DAC R2 Switch"=0
mixer "HPO MIX HPVOL Switch"=0
mixer  "OUT MIXL DAC L1 Switch"=0
mixer  "OUT MIXL DAC L1 Switch"=0
mixer  "OUT MIXL REC MIXL Switch"=0
mixer  "OUT MIXL REC MIXL Switch"=0
mixer  "DAC MIXR Stereo ADC Switch"=0
mixer "Speaker Playback Switch"=1 1
mixer  "I2S Switch"=1

制作好audio_conf.txt文件后,将其放到开机动画的目录中,打包之后放入相应目录,即可播放开机音乐。

你可能感兴趣的:(Android 5.0 以上开机音乐实现)