以编程方式为App设置自定义通知铃声(从Mobile中的AUDIO文件中选择) [英] Programatically Set Custom Notification Ringtone (Choose from AUDIO files in Mobile) for App
问题描述
我想列出移动设备中所有可用的AUDIO(.mp3)文件.
I want to list all the available AUDIO (.mp3) files in Mobile device.
用户可以从列表中选择任何音频文件,并可以将此应用设置为通知音.
User can select any Audio file from list and can set as a Notification Tone for this App.
我找到了很多资料,但没有一个令人满意.
I worked out many sources and none of them satisfiable.
谢谢.
推荐答案
首先使用下面的代码获取所有可用的mp3文件并获取其名称,然后对获取的数据进行任何操作,例如,您可以设置为列表视图或在对话框等中显示它们.
First get all the available mp3 files and retreive their names using the code below do whatever u want with the retreived data for eg u can set up to a list view or showing them in a dialog etc.
String extPath = getSecondaryStorage();
if (extPath != null)
{ mySongs_onSystem = findSongs(new File(extPath));
}
else
mySongs_onSystem = findSongs(Environment.getExternalStorageDirectory());
public ArrayList<File> findSongs(File root) {
ArrayList<File> al = new ArrayList<>();
File[] files = root.listFiles();
for (File singleFile : files) {
if (singleFile.isDirectory()) {
al.addAll(findSongs(singleFile));
} else {
if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".Mp3") || singleFile.getName().endsWith(".wav")) {
al.add(singleFile);
}
}
}
return al;
}
private String getSecondaryStorage() {
String strSDCardPath = System.getenv("SECONDARY_STORAGE");
if ((strSDCardPath == null) || (strSDCardPath.length() == 0)) {
strSDCardPath = System.getenv("EXTERNAL_SDCARD_STORAGE");
}
//If may get a full path that is not the right one, even if we don't have the SD Card there.
//We just need the "/mnt/extSdCard/" i.e and check if it's writable
if (strSDCardPath != null) {
if (strSDCardPath.contains(":")) {
strSDCardPath = strSDCardPath.substring(0, strSDCardPath.indexOf(":"));
}
File externalFilePath = new File(strSDCardPath);
if (externalFilePath.exists() && externalFilePath.canWrite()) {
return strSDCardPath;
}
}
return null;
}
使用这样的for循环检索所有mp3文件的名称.
retreive the names of all the mp3 files using a for loop like this.
for (int i = 0; i < mySongs_onSystem.size(); i++) {
//declare a string array in global String[] songNames;
songNames[i] = mySongs_onSystem.get(i).getName().toString().replace(".mp3", "").replace(".Mp3", "").replace(".wav", "");
Log.i("songname", songNames[i]);
}
传递回传的mp3音的字符串数组,并将其附加到列表适配器,以显示列表中的所有歌曲名称,并附加一个Click Listener和
pass the string array of retreived mp3 tones and attach it to list adapter to show all song names in a list and attach a click listener and
store the uri of the selected mp3
Uri u = Uri.parse(mySongs_onSystem.get(position).getAbsolutePath());
创建一个自定义界面,该界面会在通知到达时触发,然后使用媒体播放器播放选定的mp3音频,如下所示:
create a custom interface which gets triggered when notification arrives and then play the selected mp3 audio using media player like this:
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
这篇关于以编程方式为App设置自定义通知铃声(从Mobile中的AUDIO文件中选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!