问题描述
我有一个应用程序,在该应用程序的资产文件夹中有一些mp3声音。在应用程序启动时,我将获取文件列表并将所有文件从资产复制到getExternalFilesDir()。为每个文件调用媒体扫描器,并且仅当媒体扫描器返回一个URI时,然后在共享首选项中写一些内容
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag","Failed to get asset file list.",e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null),filename);
out = new FileOutputStream(outFile);
copyFile(in,out);
MediaScannerConnection.scanFile(this,new String[] { outFile.getAbsolutePath() },null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String contentPath,Uri uri) {
Log.i("onScanCompleted",uri.toString());
ringtoneSoundChooserFragment.path = uri.toString();
SharedPreferences sharedPreferences = getSharedPreferences("songs_uri",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(filename,uri.toString());
editor.apply();
}
});
} catch(IOException e) {
Log.e("tag","Failed to copy asset file: " + filename,e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
在要加载这些声音并播放的片段中,我在名为“ ringtoneSoundChooserFragment”的片段类中编写了以下代码
List<SoundData> sounds = new ArrayList<>();
SharedPreferences sharedPreferences = getContext().getSharedPreferences("songs_uri",Context.MODE_PRIVATE);
sounds.add(new SoundData("Shukran",SoundData.TYPE_ringtone,sharedPreferences.getString("shukran.mp3","null")));
sounds.add(new SoundData("My Shortcomings",sharedPreferences.getString("my_shortcomings.mp3","null")));
sounds.add(new SoundData("Need the love",sharedPreferences.getString("need_the_love.mp3","null")));
sounds.add(new SoundData("La",sharedPreferences.getString("la_ilaha_illallah.mp3","null")));
sounds.add(new SoundData("ma",sharedPreferences.getString("subhanallah.mp3","null")));
sounds.add(new SoundData("Qal",sharedPreferences.getString("baydh.mp3","null")));
sounds.add(new SoundData("Be Yourself",sharedPreferences.getString("be_yourself.mp3","null")));
sounds.add(new SoundData("Jamal Ul-Wujoodi",sharedPreferences.getString("jamal_ul_wujoodi.mp3","null")));
sounds.add(new SoundData("Yan",sharedPreferences.getString("ya.mp3","null")));
sounds.add(new SoundData("Kun",sharedPreferences.getString("kun.mp3","null")));
sounds.add(new SoundData("Day",sharedPreferences.getString("eid.mp3","null")));
sounds.add(new SoundData("Sharab",sharedPreferences.getString("sharab.mp3","null")));
sounds.add(new SoundData("Ao",sharedPreferences.getString("thirteen.mp3","null")));
sounds.add(new SoundData("Hoyoo",sharedPreferences.getString("hoyoo.mp3","null")));
sounds.add(new SoundData("Yapo",sharedPreferences.getString("fifteen.mp3","null")));
sounds.add(new SoundData("mi",sharedPreferences.getString("sixteen.mp3","null")));
sounds.add(new SoundData("io",sharedPreferences.getString("seventeen.mp3","null")));
SoundsAdapter adapter = new SoundsAdapter(getAlarmio(),sounds);
adapter.setListener(this);
recyclerView.setAdapter(adapter);
我的适配器屏幕带有播放按钮,当我想播放声音时,请点击播放按钮。 但是问题是,在某些Android设备中,声音显示的是蜂鸣声,而不是实际声音。但是,在某些设备中,它保持完全静音,而在某些设备中,它可以完美播放声音。 这个问题也出现在android 10中,但它发生在随机设备上。有些可以完美地播放声音,有些则保持沉默或发出蜂鸣声。我缺少什么或代码有什么问题? 请帮助
解决方法
媒体扫描器不会为您的应用程序私有目录中的文件编制索引。不再来自getExternalFilesDir()。
但是您不需要媒体扫描仪来获取uri或将信息保存为共享首选项。
只需使用FileProvider来提供文件。
另一种方法是直接将文件复制到媒体存储。然后,您将拥有不错的媒体存储库,并且文件也将被扫描。
第三种可能性是采用您自己的ContentProvider并直接从资产中提供文件。无需先复制。