我设法用音频文件填充ListView
,但是当单击列表视图中的项目时没有任何反应,但出现异常“ setDataSource失败”。
public class songsFragment extends Fragment {
private static final int MY_PERMISSION_REQUEST = 1;
ListView trackList;
private RelativeLayout songs_layout;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
private MediaPlayer mp = new MediaPlayer();
public songsFragment() {
}
public void doStuff() {
arrayList = new ArrayList<>();
getMusic();
adapter = new ArrayAdapter<>(Objects.requireNonNull(getContext()), android.R.layout.simple_list_item_1, arrayList);
trackList.setAdapter(adapter);
trackList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long I) {
mp = new MediaPlayer();
try {
mp.setDataSource(arrayList.get(i));
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name),e.getMessage());
}
}
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.songs_fragment, container, false);
songs_layout = v.findViewById(R.id.songs_layout);
return v;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
trackList = view.findViewById(R.id.songList);
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(getActivity()).getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
}
} else {
doStuff();
}
if (trackList.getParent() != null) {
((ViewGroup)trackList.getParent()).removeView(trackList);
}
songs_layout.addView(trackList);
}
public void getMusic() {
ContentResolver contentResolver = Objects.requireNonNull(getContext()).getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
@SuppressLint("Recycle") Cursor songCursor = contentResolver.query
(songUri, null,null,null,null);
if (songCursor != null && songCursor.moveToFirst()) {
int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do {
String currentTitle = songCursor.getString(songTitle);
String currentArtist = songCursor.getString(songArtist);
arrayList.add("\n" + currentTitle + "\n" + currentArtist + "\n");
} while (songCursor.moveToNext());
}
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(getActivity()).getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
doStuff();
}
} else {
Toast.makeText(getContext(),"No permission granted", Toast.LENGTH_SHORT).show();
Objects.requireNonNull(getActivity()).finish();
}
}
}
}
}
Logcat
2018-12-19 20:06:37.429 9793-9793/com.oladapo.jukebox W/System.err: java.io.IOException: setDataSource failed.
2018-12-19 20:06:37.442 9793-9793/com.oladapo.jukebox W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1191)
2018-12-19 20:06:37.442 9793-9793/com.oladapo.jukebox W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1164)
2018-12-19 20:06:37.442 9793-9793/com.oladapo.jukebox W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1131)
2018-12-19 20:06:37.442 9793-9793/com.oladapo.jukebox W/System.err: at com.oladapo.jukebox.songsFragment$1.onItemClick(songsFragment.java:59)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.widget.AdapterView.performItemClick(AdapterView.java:318)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.widget.AbsListView$3.run(AbsListView.java:4052)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.os.Handler.handleCallback(Handler.java:873)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.os.Looper.loop(Looper.java:193)
2018-12-19 20:06:37.443 9793-9793/com.oladapo.jukebox W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6718)
2018-12-19 20:06:37.444 9793-9793/com.oladapo.jukebox W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2018-12-19 20:06:37.444 9793-9793/com.oladapo.jukebox W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2018-12-19 20:06:37.444 9793-9793/com.oladapo.jukebox W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2018-12-19 20:06:37.444 9793-9793/com.oladapo.jukebox V/Juke Box: setDataSource failed.
答案 0 :(得分:1)
您要在数组列表中添加标题和艺术家名称,
arrayList.add("\n" + currentTitle + "\n" + currentArtist + "\n");
并像这样添加数据源
mp.setDataSource(arrayList.get(i));
但是setDataSource需要文件路径,在您的情况下,arraylist仅包含标题和艺术家名称,而不是该mp3文件的路径。
这里是示例。 https://www.programcreek.com/java-api-examples/?class=android.media.MediaPlayer&method=setDataSource