Chinaunix首页 | 论坛 | 博客
  • 博客访问: 549692
  • 博文数量: 185
  • 博客积分: 4031
  • 博客等级: 上校
  • 技术积分: 1591
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-27 19:45
文章分类

全部博文(185)

文章存档

2011年(14)

2010年(63)

2009年(108)

我的朋友

分类:

2010-12-04 13:08:56

Android Media Scanner Process

[First written by Steve Guo, please keep the mark if forwarding.]

Here is the overall picture.

MediaScannerReceiver is started when any ACTION_BOOT_COMPLETED, ACTION_MEDIA_MOUNTED or ACTION_MEDIA_SCANNER_SCAN_FILE intent is sent out. Because it may cost a lot of time to parse meta-data information of media files, MediaScannerReceiver will start MediaScannerService. MediaScannerService calls a utility class MediaScanner to do the real work. MediaScannerReceiver maintains two kinds of scan directories: One is internal volume which points to $(ANDROID_ROOT)/media. Another is external volume which points to $(EXTERNAL_STORAGE).

The scan and parse work lies in both JAVA and C++ layer. JAVA layer is the starter. To scan the whole directory, MediaScanner does the following steps.

1. JAVA layer initialize

In this step, it will open different database according to whether the dir is internal or external volume.

2. JAVA layer prescan

It first clears file and play list cache entries, then generates new file and play list cache entries according to query result from MediaProvider.

3. C++ layer processDirectory

It enumerates all files and sub-dirs in a specific dir(If a sub-dir contains a .nomedia hidden file, it won’t be enumerated.). For each enumerated file, it judges whether the file is supported according to file extension. If the file extension is supported, then C++ layer will call back to JAVA layer scanFile. The file extension which will be scanned is listed in MediaFile.java. Here is the list.

/* Audio */

addFileType("MP3", FILE_TYPE_MP3, "audio/mpeg");

addFileType("M4A", FILE_TYPE_M4A, "audio/mp4");

addFileType("WAV", FILE_TYPE_WAV, "audio/x-wav");

addFileType("AMR", FILE_TYPE_AMR, "audio/amr");

addFileType("AWB", FILE_TYPE_AWB, "audio/amr-wb");

addFileType("WMA", FILE_TYPE_WMA, "audio/x-ms-wma");

addFileType("OGG", FILE_TYPE_OGG, "application/ogg");

addFileType("MID", FILE_TYPE_MID, "audio/midi");

addFileType("XMF", FILE_TYPE_MID, "audio/midi");

addFileType("RTTTL", FILE_TYPE_MID, "audio/midi");

addFileType("SMF", FILE_TYPE_SMF, "audio/sp-midi");

addFileType("IMY", FILE_TYPE_IMY, "audio/imelody");

/* Video */

addFileType("MP4", FILE_TYPE_MP4, "video/mp4");

addFileType("M4V", FILE_TYPE_M4V, "video/mp4");

addFileType("3GP", FILE_TYPE_3GPP, "video/3gpp");

addFileType("3GPP", FILE_TYPE_3GPP, "video/3gpp");

addFileType("3G2", FILE_TYPE_3GPP2, "video/3gpp2");

addFileType("3GPP2", FILE_TYPE_3GPP2, "video/3gpp2");

addFileType("WMV", FILE_TYPE_WMV, "video/x-ms-wmv");

/* Image */

addFileType("JPG", FILE_TYPE_JPEG, "image/jpeg");

addFileType("JPEG", FILE_TYPE_JPEG, "image/jpeg");

addFileType("GIF", FILE_TYPE_GIF, "image/gif");

addFileType("PNG", FILE_TYPE_PNG, "image/png");

addFileType("BMP", FILE_TYPE_BMP, "image/x-ms-bmp");

addFileType("WBMP", FILE_TYPE_WBMP, "image/vnd.wap.wbmp");

/* Audio Play List */

addFileType("M3U", FILE_TYPE_M3U, "audio/x-mpegurl");

addFileType("PLS", FILE_TYPE_PLS, "audio/x-scpls");

addFileType("WPL", FILE_TYPE_WPL, "application/vnd.ms-wpl");

4. JAVA layer scanFile

a) JAVA layer beginFile

First it ignores some special files for MacOS and Windows Media Player. Then it looks whether the file has been in the cache entry, if so, it will check whether the file’s last modification time is changed. Finally it returns the result whether the file needs to be processed further. If no need, the following two steps won’t be executed.

b) C++ layer scanFile

Not all the files will be delivered to let C++ layer parse meta-data. Only the following file types will be parsed. Pay attention here, image files are not handled here.

if (mFileType == MediaFile.FILE_TYPE_MP3 ||

mFileType == MediaFile.FILE_TYPE_MP4 ||

mFileType == MediaFile.FILE_TYPE_M4A ||

mFileType == MediaFile.FILE_TYPE_3GPP ||

mFileType == MediaFile.FILE_TYPE_3GPP2 ||

mFileType == MediaFile.FILE_TYPE_OGG ||

mFileType == MediaFile.FILE_TYPE_MID ||

mFileType == MediaFile.FILE_TYPE_WMA) {

For each parsed meta-data information, C++ layer will call back to JAVA layer handleStringTag. JAVA layer will record the name/value information.

c) JAVA layer endFile

Finally JAVA layer updates the corresponding database table provided by MeidaProvider according to the values parsed at the previous step.

5. JAVA layer postScan

Until now, all the files have been scanned, it finally checks the file and play list cache entry to see whether all items still existed in the file system. If have any empty entry, then delete it from database. So that it can keep some kinds of consistence between database and file system.

Other application can know when the scan operation starts and ends through receiving ACTION_MEDIA_SCANNER_STARTED and ACTION_MEDIA_SCANNER_FINISHED intents sent from MediaScannerService.

阅读(821) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~