gpt4 book ai didi

java - "setDataSource failed"异常

转载 作者:行者123 更新时间:2023-11-29 23:23:51 25 4
gpt4 key购买 nike

我已经设法用音频文件填充我的 ListView,但是当单击 ListView 中的项目时没有任何反应,我收到异常“setDataSource failed”。

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();

}

}

}

}

}

日志

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.

最佳答案

您在此处的数组列表中添加标题和艺术家姓名

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

关于java - "setDataSource failed"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com