你好,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();
}
1条答案
按热度按时间qlvxas9a1#
您必须将文件从资源复制到文件系统,这样您就有了一个文件。
那你就可以用那个文件了。