您的位置:首页 > 移动开发 > Android开发

Android Media Scanner Process

2011-03-28 09:40 337 查看
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 J***A and C++ layer. J***A layer is the starter. To scan the whole directory, MediaScanner does the following steps.

1. J***A layer initialize

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

2. J***A 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 J***A 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("W***", FILE_TYPE_W***, "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. J***A layer scanFile

a) J***A 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 J***A layer handleStringTag. J***A layer will record the name/value information.

c) J***A layer endFile

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

5. J***A 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: