我是Java新手,我遇到了一些小问题,我正在编写类似于闹钟的应用程序(适用于Android),我有一个名为设置声音的按钮,我想选择当闹钟响起时将播放的声音, 但我不想要音乐位置的单一路径,但我想就像在标准的andoid音乐(标准应用程序)中一样,在您粘贴音乐文件的任何地方,您不需要查找它,因为当您单击音乐应用程序时, 它出现在音乐列表中。我正在做 onClick 方法,所以它是对的吗?我希望我正确地描述了我的问题。
稍后,我将尝试通过单击“保存”按钮来保存此(声音、小时、星期几),但我还没有这样做,这是我应该考虑的事情吗?
对不起我的英语
感谢您的帮助:)
@UP 抱歉,这是负责此操作的部分代码: 这是从别人那里复制过来的,但是不起作用。
>
final Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver music = getApplication().getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = music.query(musicUri, null, null, null, null);
String thisPath = null;
if (musicCursor != null && musicCursor.moveToFirst()) {
int pathColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
counter = 0;
do {
thisPath = musicCursor.getString(pathColumn);
} while (musicCursor.moveToNext());
}
File f = new File(thisPath);
f.getParent();
}
最佳答案
是的,您可以像代码中一样通过从 MediaStore 查询歌曲来获取音乐文件的路径。但是您需要将路径收集到 ArrayList 而不是单个“thisPath”字符串中,并使用 RecyclerView/ListView 将它们显示给用户。这样他们就可以选择他们想要的音乐。您可能需要在不同的线程中执行此操作,因为这是一个繁重的过程,会阻塞您的 UI 线程。
或者,您可以使用音乐选择器获取音乐文件的 URI
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, requestCode);
这将打开一个默认播放器,用户可以选择他们想要的音乐。
关于java - 引用音乐应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47093880/