Android MediaScannerスキャンプロセス

5878 ワード

Android MediaScannerスキャン呼び出しのタイミングチャート:
Created with Raphaël 2.1.0 MediaScanner Receiver.java MediaScanner Receiver.java MediaScanner Service.java MediaScanner Service.java Media Scanner.java Media Scanner.java android_ media_Media Scanner.cpp android_ media_Media Scanner.cpp Stagefri ghtMedia Scanner.cpp Stagefri ghtMedia Scanner.cpp Media Scanner.cpp Media Scanner.cpp Stagefright Metadata Retriever.cpp Stagefright Metadata Retriever.cpp 1,received mounted broadcast 2,scan 3,create MediaScanner 4,create MyMediaScan nerClient MyMedia ScannerClient 5,native_init 6,native_setup 7,create Stage fright MediaScanner 8,scanDirectories 9,initialize 10,prescan 11,process Directory 12,create MyMedia ScannerClient MyMedia ScannerClient 13,MyMedia Scanner Client(native) reflection of MyMedia Scanner Client(java) 14,processDirectory 15,doPro cess Directory 16,doPro cessDirec toryEntry 17,MyMediaScanner Client->scanFile 18,MyMedia Scanner Client->scanFile 19,doScanFile 20,beginFile Audio or Video follow 21,processFile 22,processFile 23,new Media Metadata Retriever 24,setData Source 25,extract Metadata 26,MyMediaScanner Client->setMimeType 27,extract Metadata 28,MyMediaScanner Client->addStringTag 29,postscan Scan finished
Android MediaScannerコードの具体的な分析:
引用:
 * The way the scan currently works is:
 * - The Java MediaScannerService creates a MediaScanner (this class), and calls
 *   MediaScanner.scanDirectories on it.
 * - scanDirectories() calls the native processDirectory() for each of the specified directories.
 * - the processDirectory() JNI method wraps the provided mediascanner client in a native
 *   'MyMediaScannerClient' class, then calls processDirectory() on the native MediaScanner
 *   object (which got created when the Java MediaScanner was created).
 * - native MediaScanner.processDirectory() calls
 *   doProcessDirectory(), which recurses over the folder, and calls
 *   native MyMediaScannerClient.scanFile() for every file whose extension matches.
 * - native MyMediaScannerClient.scanFile() calls back on Java MediaScannerClient.scanFile,
 *   which calls doScanFile, which after some setup calls back down to native code, calling
 *   MediaScanner.processFile().
 * - MediaScanner.processFile() calls one of several methods, depending on the type of the
 *   file: parseMP3, parseMP4, parseMidi, parseOgg or parseWMA.
 * - each of these methods gets metadata key/value pairs from the file, and repeatedly
 *   calls native MyMediaScannerClient.handleStringTag, which calls back up to its Java
 *   counterparts in this file.
 * - Java handleStringTag() gathers the key/value pairs that it's interested in.
 * - once processFile returns and we're back in Java code in doScanFile(), it calls
 *   Java MyMediaScannerClient.endFile(), which takes all the data that's been
 *   gathered and inserts an entry in to the database.

上からmedia scannerプロセスの設計ロジックがわかりますが、次の点に注意してください.
1,mount放送受信
MediaScannerReceiverが外部機器からの放送を受信した後、mediaScannerServiceで外部をスキャンするたびにSDカードディレクトリからスキャンし、Uディスクに再帰する.スキャン速度を速めると、Uディスクディレクトリまたは固定ディレクトリのみをスキャンすることが考えられる.
2,プリスキャンprescan
この関数の役割は、スキャン前にデータベース内の情報を抽出して保存すること(メディアファイルのパス、Metadata、所属テーブルのURIを含む)であり、これは主にスキャン後にデータベースをよりよく更新するためである.
3,processDirectoryメディアスキャン
この関数は最も時間がかかります.主に2つに分かれています.ディレクトリであれば再帰的に呼び出し、ファイルであれば
a、beginFileを呼び出します.この関数はデータベースの前のキャッシュに保存するために使用され、現在のファイルが変更されたかどうかを判断するために使用されます.変更された場合、スキャンを呼び出し続けます.
b、media Retriverを呼び出してID 3情報を取得するprocessFileを再起動する
c,endfileを呼び出してスキャンした情報をデータベースに挿入することが望ましい