我有一个应用程序,其中资产文件夹中有一些 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", SoundData.TYPE_RINGTONE,sharedPreferences.getString("my_shortcomings.mp3","null")));
sounds.add(new SoundData("Need the Love", SoundData.TYPE_RINGTONE,sharedPreferences.getString("need_the_love.mp3","null")));
sounds.add(new SoundData("La", SoundData.TYPE_RINGTONE,sharedPreferences.getString("la_ilaha_illallah.mp3","null")));
sounds.add(new SoundData("ma", SoundData.TYPE_RINGTONE,sharedPreferences.getString("subhanallah.mp3","null")));
sounds.add(new SoundData("Qal", SoundData.TYPE_RINGTONE,sharedPreferences.getString("baydh.mp3","null")));
sounds.add(new SoundData("Be Yourself", SoundData.TYPE_RINGTONE,sharedPreferences.getString("be_yourself.mp3","null")));
sounds.add(new SoundData("Jamal Ul-Wujoodi", SoundData.TYPE_RINGTONE,sharedPreferences.getString("jamal_ul_wujoodi.mp3","null")));
sounds.add(new SoundData("Yan", SoundData.TYPE_RINGTONE,sharedPreferences.getString("ya.mp3","null")));
sounds.add(new SoundData("Kun", SoundData.TYPE_RINGTONE,sharedPreferences.getString("kun.mp3","null")));
sounds.add(new SoundData("Day", SoundData.TYPE_RINGTONE,sharedPreferences.getString("eid.mp3","null")));
sounds.add(new SoundData("Sharab", SoundData.TYPE_RINGTONE,sharedPreferences.getString("sharab.mp3","null")));
sounds.add(new SoundData("Ao", SoundData.TYPE_RINGTONE,sharedPreferences.getString("thirteen.mp3","null")));
sounds.add(new SoundData("Hoyoo", SoundData.TYPE_RINGTONE,sharedPreferences.getString("hoyoo.mp3","null")));
sounds.add(new SoundData("Yapo", SoundData.TYPE_RINGTONE,sharedPreferences.getString("fifteen.mp3","null")));
sounds.add(new SoundData("mi", SoundData.TYPE_RINGTONE,sharedPreferences.getString("sixteen.mp3","null")));
sounds.add(new SoundData("io", SoundData.TYPE_RINGTONE,sharedPreferences.getString("seventeen.mp3","null")));
SoundsAdapter adapter = new SoundsAdapter(getAlarmio(), sounds);
adapter.setListener(this);
recyclerView.setAdapter(adapter);
我有带有播放按钮的适配器屏幕,当我想播放声音时,我点击播放按钮。但问题是,在某些 android 设备中,声音显示哔哔哔而不是实际声音。但是在某些设备中,它会保持完全静音,而在某些设备中,它可以完美地播放声音。这个问题也在 android 10 中,但它发生在随机设备上。有些完美地播放声音,有些则保持沉默或发出哔哔声。我缺少什么或我的代码有什么问题?请帮忙