参考音乐应用程序

时间:2017-11-03 10:45:37

标签: java android

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

1 个答案:

答案 0 :(得分:0)

Yes, you can get paths of music files by querying songs from MediaStore like in your code. But you need to collect paths into an ArrayList instead of a single "thisPath" String and show them to user using a RecyclerView/ListView. So they can select the music they want. And you may need to do this in different thread since this is an heavy process which will block your UI thread.

Alternatively you can get an URI of a music file using a music picker by

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, requestCode);

This will open a default player which user can select the music they want.