我正在尝试检索音乐数据,将数据保存到数组列表中并将数据字符串转换为uri并使用它来播放音乐,但是当我点击时应用程序崩溃。列表视图项目它将播放由其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();
}
});
我的主要活动课是我正在获取数据
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;
}
}
通过这种方式,我可以获取数据以及如何在单击项目时播放它
答案 0 :(得分:3)
使用此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();
}
});
答案 1 :(得分:0)
试试这个
try {
Uri myUri = Uri.parse(item.getData());
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), myUri);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
答案 2 :(得分:0)
最近,我用以下逻辑解决了这个问题:
使用这个:
SetDataSourc(context, uri)
而不是这个:
setDataSource(uri)
对于这个解决方案,你不需要上面的阅读权限= Q
答案 3 :(得分:-1)
你应该初次启动MediaPlayer
并添加mediaPlayer.reset befor mediaPlayer.setDataSource(String.valueOf(myUri))
试试这可能会解决您的问题