当文件位于assets文件夹中时,如何将音频文件设置为铃声?

ssgvzors  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(283)

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

我需要帮助

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题