1

我正在 android 中构建一个音乐播放器应用程序。单击 listItem 时,将播放相应的歌曲,歌曲的名称将显示在 TextView 中的搜索栏上方,id 为“selectedItem”。但是数据绑定是在 MediaCursorAdapter.class 中完成的,它将 MediaStore.MediaColumns.DATA 作为字符串返回给 MainActivity.java 类的方法 onListItemClick()。此处将当前歌曲的名称设置为字符串。但这显示了应用程序中歌曲的整个路径,看起来很糟糕。有没有办法在我的 MainActivity.java 类中获取 DISPLAY_NAME 或以某种方式缩短此路径以使其在应用程序中看起来不那么糟糕?

MainActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    currentFile = (String) v.getTag();
    startPlay(currentFile);
}

MediaCursorAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //TextView title = (TextView) view.findViewById(R.id.title_name);
    TextView displayName = (TextView) view.findViewById(R.id.display_name);
    TextView duration = (TextView) view.findViewById(R.id.duration);
    String nameOfTheSong = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
    nameOfTheSong = nameOfTheSong.substring(0,nameOfTheSong.length()-4);
    displayName.setText(nameOfTheSong);
    //title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
    long durationInMS = Long.parseLong(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
    double durationInMin = ((double)durationInMS/1000.0)/60.0;
    BigDecimal bigDecimal = new BigDecimal(durationInMin);
    durationInMin = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
    duration.setText(""+durationInMin);
    //SETTING THE PATH WHICH IS THEN USED IN onListItemClick OF THE MAINACTIVITY.JAVA CLASS
    view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}

Github 链接:https ://github.com/vishwabhat19/PlayMusic.git

4

1 回答 1

1

您没有显示如何获得光标,但您可以使用:

    MediaStore.Audio.Media.TITLE

或者如果你有完整的路径

 String trackname = fullpath.substring(fullpath.lastIndexOf("/") + 1, fullpath.length());
于 2017-07-20T11:24:42.497 回答