【发布时间】:2021-04-04 19:18:12
【问题描述】:
你好 stackoverflow 社区。我知道以前有人提出过这个问题,但在答案中总是留下一些东西,而且我缺乏专业知识,无法推断出其余的东西。
问题是,我正在开发我的第一个 android 应用程序,它从 assets 文件夹内的文件夹中设置铃声。
我已经设法播放资产中的声音文件,但找不到获取 Uri 路径以将相同的音频文件设置为铃声的方法。
我需要帮助!
ListView assetsListView;
MediaPlayer mediaPlayer;
ArrayList<String> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
assetsListView = findViewById(R.id.assetsListView);
final AssetManager assetManager = getAssets();
System.out.println("ASSES" + assetManager);
//***************************** listting files from the assets folder ***********************//
try {
// for assets folder add empty string
String[] fileList = assetManager.list("");
if (fileList == null) {
// dir does not exist or is not a directory
} else {
for (int i=0; i<fileList.length; i++) {
// Get filename of file or directory
String filename = fileList[i];
}
}
// for assets/subFolderInAssets add only subfolder name
String[] fileListInSubfolder = assetManager.list("ringtones");
System.out.println("filesinsubfolder " + (fileListInSubfolder == null));
if(fileListInSubfolder == null) {
// dir does not exist or is not a directory
}else{
for (int i=0; i < fileListInSubfolder.length; i++) {
// get filename from files in the subdirectory
String filenameSubD = fileListInSubfolder[i];
System.out.println("FILENAME IN SUBFOLDER plin: " + filenameSubD.substring(0,(int)filenameSubD.length()-4)); // cuts the extension of the file
arrayList.add(filenameSubD.substring(0,(int)filenameSubD.length()-4)); /// adds the filename to the list array
}
}
} catch (IOException e) {
e.printStackTrace();
}
//******************************************************************************************//
final boolean settingsCanWrite = Settings.System.canWrite(this); // since API23 permission for write-settings is needed
if(!settingsCanWrite) { // If do not have write settings permission then open the Can modify system settings panel.
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); // goes to settings and is needed to manually add permission to this app
startActivity(intent);
}else {
assetsListView.setAdapter(arrayAdapter);
assetsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String fileName = arrayList.get(position);
String fileWithExtension = "file:///android_assets/ringtones/" + fileName;
//++++++++++++++++++++++++++++++++++++++RINGTONE++++++++++++++++++++++++++++++++++++++++++++//
Uri uri = Uri.fromFile(new File("file:///android_assets/ringtones/" + fileName + ".mp3"));
Uri uri2 = Uri.parse("file:///android_assets/" + fileName);
Uri uri3 = Uri.parse("assets/ringtones/" + fileName + ".mp3");
Uri uri4 = Uri.parse("assets/ostinato.mp3");
String testFilePath = uri.getPath();
File file = new File("file:///android_assets/ringtones/", fileName);
System.out.println("FILEEE" + file);
System.out.println("lavativa " + uri);
play(fileName, uri4);
}
});
}
}
public void play (String fileToPlayer, Uri uriPath) { // play from the assets/ringtone folder
if (mediaPlayer == null) {
try {
AssetFileDescriptor afd = getAssets().openFd("ringtones/" + fileToPlayer + ".mp3");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
pause();
}
});
} else {
pause();
}
System.out.println("URI" + uriPath);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE,uriPath);
Toast.makeText(this, "message: " + uriPath, Toast.LENGTH_LONG).show();
}
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。