---
title: "ShutdownThread - 关机音乐  （.ogg  ， SystemProperties）"
author: "ishelf"
date: "2011-04-21T23:09:46Z"
tags: []
categories: ["移动开发"]
slug: "shutdownthread-ogg-systemproperties"
draft: false
lastmod: "2011-04-21T23:11:21Z"
id: 1012085
url: "https://ishelf.iteye.com/blog/1012085"
---

关机时候有两种方式可以播放音乐，它们各有优劣，这里给出这两个方法并进行简单的分析:

 1.使用系统提供的命令进行播放，代码如下

 

SystemProperties.set("ctl.start", "haltsound");

```

```

     此方法类似于开机动画一样，可以通过adb  shell中的命令进行调用。命令序列如下adb shell

 

adb shell #setprop ctl.start bootanim // 这样就可以单独创建一个main surface 并显示

```

```

 

     使用SystemProperties调用关机音乐，可以通过音量键（媒体音量）进行控制，该方法能够独立于动画线程运行，这样缺点也就很明显了，如果调用的位置不对很有可能导致动画音乐不同步，因此要注意上节所将得run方法的执行顺序。

 

2.新建一个音乐类并进行播放，可如下实现

 

class ShutdownSound { private static String DEF\_PATH = "/system/media/bootsound/poweroff.ogg"; private String soundFile; private MediaPlayer player; BootSound(Context context) { String path = SystemProperties.get("cfg.sys.bootsound-poweroff"); if ((null == path) || (null != path && !((new File(path)).exists()))) { path = DEF\_PATH; } if ((new File(path)).exists()) { new AsyncPlayer(TAG).play(context, Uri.parse("file://"+ path), false, 5); } } }

```

```

 

    该种方法可以避免音量键的控制，这也是其优点之一（至少我是认为这样的），还有一个优点就是可以主动的控制播放过程。缺点是如果在一个线程内跑的话非常影响性能。

 

 

这里补充一点关于#setprop ctl.start bootanim  类似这样命令的使用方式，在android源码的system\\core\\rootdir\\文件夹中有这样一个配置文件：Init.rc，该文件定义了很多service的调用方式，例如bootanim的services，如下所示

service bootanim /system/bin/bootanimation user graphics group graphics disabled oneshot

```

```

 

    可以根据需要在系统中定义自己的services，并通过系统命令进行调用。