如何通过其uri播放音乐

How to play music by its uri

我正在尝试检索音乐数据,将数据保存到数组列表中,并将数据字符串转换为 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;
}
}

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

 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 class 我正在获取数据

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;
}
}

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

试试这个

try {
   Uri myUri = Uri.parse(item.getData());
   Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), myUri);
   r.play();
} catch (Exception e) {
  e.printStackTrace();
}

你应该初始化一次 MediaPlayer

并在 mediaPlayer.setDataSource(String.valueOf(myUri))

之前添加 mediaPlayer.reset

尝试这可能会解决您的问题

使用这个 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();
        }
    });

最近,我用以下逻辑解决了这个问题:

使用这个:

SetDataSourc(context, uri)

而不是这个:

setDataSource(uri)

对于此解决方案,您不需要阅读上面的许可 = Q