gpt4 book ai didi

java - 如何播放从内部存储中获取的歌曲

转载 作者:行者123 更新时间:2023-12-01 16:30:13 24 4
gpt4 key购买 nike

我已经从手机的内部存储中获取了音乐,它显示得很好,但我不知道如何播放这些歌曲。这是我的代码,请告诉我如何玩它。我没有得到播放歌曲必须传递哪个 URI。

帮我解决这个问题。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_music, container, false);
if(ContextCompat.checkSelfPermission(getContext(), 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_PERMISSTION_REQUEST);

} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSTION_REQUEST);
}
} else {
doStuff();
}

return view;
}
public void doStuff(){
ListView listView = view.findViewById(R.id.musicrecyclerview);
musicModes = new ArrayList<>();
getMusic();
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, musicModes);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent =new Intent();
intent.setAction(Intent.ACTION_VIEW);

}
});
}
public void getMusic(){
ContentResolver contentResolver = getActivity().getApplicationContext().getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor mycursor = contentResolver.query(uri, null, null, null, null);
if(mycursor != null && mycursor.moveToFirst()){
songtitle = mycursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
songsize = mycursor.getColumnIndex(MediaStore.Audio.Media.DATA);
column_index = mycursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
do{
currenttitle = mycursor.getString(songtitle);
currentsize = mycursor.getString(songsize);
musicModes.add(currenttitle+ "\n"+ currentsize);
} while(mycursor.moveToNext());
}
}

最佳答案

请尝试这个:

int dataIndex= mycursor.getColumnIndex(MediaStore.Audio.Media.DATA);
String data = mycursor.getString(dataIndex)
Uri uri= Uri.parse("file:///" + data);

MediaPlayer player= new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(getApplicationContext(), uri);
player.prepare();
player.start();

关于java - 如何播放从内部存储中获取的歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62058908/

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