gpt4 book ai didi

android - 如何通过其uri播放音乐

转载 作者:太空狗 更新时间:2023-10-29 13:12:21 33 4
gpt4 key购买 nike

我正在尝试检索音乐数据,将数据保存到数组列表中,并将数据字符串转换为 uri 并使用它来播放音乐,但每次单击时应用程序都会崩溃。 ListView 项目它将通过其 uri 播放音乐这是我的代码`

public class Mediafileinfo {
private String title;
private String artist;
private String data;
public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}

`这是我保存媒体数据的类我正在尝试这样玩我哪里做错了

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Mediafileinfo item = new Mediafileinfo();
Uri myUri = Uri.parse(item.getData());
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(String.valueOf(myUri));
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});

我的主要 Activity 类是我正在获取数据

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
External();
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter arrayAdapter = new CustomeAdapter(this, audioList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Mediafileinfo item = new Mediafileinfo();
Uri myUri = Uri.parse(item.getData());

mediaPlayer = new MediaPlayer();

mediaPlayer.reset();
try {
mediaPlayer.setDataSource(MainActivity.this,myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();

}
});
}

private void External() {
try {
String[] proj = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA};// Can include more data for more details and check it.

String selection = MediaStore.Audio.Media.DURATION + ">=90000";

String[] selectionArgs = null;

String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, selectionArgs, sortOrder);

if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int audioTitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int audioartist = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
int audioalbum = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
int audiodata= audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(audioCursor.getString(audiodata));
info.setTitle(audioCursor.getString(audioTitle));
info.setAlbum(audioCursor.getLong(audioalbum));
info.setArtist(audioCursor.getString(audioartist));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

我的客户适配器

public class CustomeAdapter extends ArrayAdapter {
public CustomeAdapter(Context context, ArrayList<Mediafileinfo> audioList) {
super(context,R.layout.custome,audioList);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflate=LayoutInflater.from(getContext());
View v=inflate.inflate(R.layout.custome,parent,false);
Mediafileinfo item = (Mediafileinfo) getItem(position);
TextView text1 = (TextView) v.findViewById(R.id.textView);
text1.setText(item.getData());
TextView text2 = (TextView) v.findViewById(R.id.textView2);
text2.setText(item.getArtist());
TextView text3=(TextView)v.findViewById(R.id.textView3);
text3.setText(String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(item.getAlbum()),
TimeUnit.MILLISECONDS.toSeconds(item.getAlbum()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(item.getAlbum()))));

return v;
}
}

通过这种方式,我可以获取数据以及如何在点击项目时播放它

最佳答案

使用这个 mediaPlayer.setDataSource(MainActivity.this,myUri); 而不是 mediaPlayer.setDataSource(String.valueOf(myUri));

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Mediafileinfo item = new Mediafileinfo();
Mediafileinfo item = audioList.get(i);
Uri myUri = Uri.parse(item.getData());
mediaPlayer = new MediaPlayer();
try {
// mediaPlayer.setDataSource(String.valueOf(myUri));
mediaPlayer.setDataSource(MainActivity.this,myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});

关于android - 如何通过其uri播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711776/

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