问题描述
你好,stackoverflow 社区。我设法从原始文件夹设置了铃声,但文件的命名无法按我的需要工作。例如我需要树文件夹、铃声文件夹、通知文件夹和闹钟文件夹。 我最好的猜测是我需要从资产中完成这项工作。
public class MainActivity extends AppCompatActivity {
TextView textView;
ListView listView;
ArrayList<String> tonesArrayList;
MediaPlayer mediaPlayer;
AudioManager audioManager; //this line lets control audio
Resources myResources;/////////*******////////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
tonesArrayList = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tonesArrayList); //display the aray in the list view //
final Field[] fields=R.raw.class.getFields(); ////// this loop works for getting the names of the files in the raw directory and list them //////
for(int count=0; count < fields.length; count++){
String toneName = fields[count].getName();
tonesArrayList.add(toneName);
}
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 {
listView.setAdapter(arrayAdapter);
listView.setonItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
audioManager = (AudioManager) getSystemService(AUdio_SERVICE); // the variable audioManager is initialized here so it's not null anymore
String filetoPlay = tonesArrayList.get(position); // gets the id-position and stores it in a string to pass it to play()
Uri path = Uri.parse("android.resource://com.example.codetriangelist/raw/"+ filetoPlay); // prepares the address string to be processed
Log.d("filetoPlay: ",filetoPlay);
play(filetoPlay,path);
}
});
}
}
public void play (String filetoPlayer,Uri path) { // play with the button
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this,getResources().getIdentifier(filetoPlayer,"raw",getPackageName())); // the audio file is set here in the mediaplayer
mediaPlayer.start();
mediaPlayer.setonCompletionListener(new MediaPlayer.OnCompletionListener() { // this listener waits for the audio to stop to do something
@Override
public void onCompletion(MediaPlayer mp) {
pause();
}
});
}
else {
pause();
}
ringtoneManager.setActualDefaultringtoneUri(this,ringtoneManager.TYPE_ringtone,path);
}
public void pause (){ // pause with the button
if (mediaPlayer != null) {
mediaPlayer.pause();
mediaPlayer.release();
mediaPlayer = null; }
}
@Override
protected void onStop() { // overrides the pause() method
super.onStop();
pause();
}
我需要帮助
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)