Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

java - How do I set an audio file as a ringtone when the file is on the assets folder?

Hello stackoverflow community. I managed to set the ringtone from the raw folder but the nameing of the files doesn't work as I need. For example I need tree folders, ringtones folder, notifications folder and alarm folder. My best guess is that I need this to be done from assets.

this is the code that I've made so far. but it works from the raw folder.

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();
}

I need help


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...