微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

你好,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 举报,一经查实,本站将立刻删除。