I want to create a Music player which can download a song online and add it to MediaStore. I'm using Download Manager and allow MediaScanner scan this file when download completed.
DownloadManager.Request request ....
request.allowScanningByMediaScanner();
...
downloadManager.enqueue(request);
It's work fine in android 5.0 and above.
But the song was downloaded using codec (opus) which not supported in android below lollipop version, so MediaScanner doesn't add this file to MediaStore.
That's my problem, my app can play opus codec but the song didn't exist in MediaStore after it has downloaded, so my app can't find this song.
How to force MediaScanner add downloaded file to MediaStore.Audio as a Music track. If can not, how can I manual add this song to MediaStore.Audio after download completed:
public class BroadcastDownloadComplete extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.DOWNLOAD_COMPLETE")) {
//addSongToMediaStore(intent);
}
}
}
You can use MediaScannerConnection to ask Android to scan a file to be included as media. You'll want to use the scanFile() static method.
From the source code here, we can see the final implementation of the scanner has two steps to scan an audio file. If either of these two step fail, the audio file will not insert into media provider.
step 1 check the file extension
More extensions have been added since Android 5.0. The common container for opus codec is
ogg
, this extension exists before Android 5.0. Assume your audio file extension isogg
, the scanning process is fine at this step.step2 retrieve metadata
After the first step passed, the scanner need to retrieve media's metadata for later database insertion. I think the scanner do the codec level checking at this step.
For Android version before 5.0, the scanner might be failed at this step. Because of lacking of built-in opus codec support,
setDataSource
will get failed at last. The media file won't be added to media provider finally.suggested solution
Because we know the audio file will be added to
we can do database operation manually. If you want your audio file keeps consistent with other audio files in the database, you have to retrieve all the metadata by yourself. Since you can play the opus file, I think it's easy to retrieve the metadata.
After that, you app can find the audio file.