【问题标题】:Get the DISPLAY_NAME using DATA in android MediaStore在 android MediaStore 中使用 DATA 获取 DISPLAY_NAME
【发布时间】:2017-07-19 17:22:51
【问题描述】:

我正在 android 中构建一个音乐播放器应用程序。单击 listItem 时,将播放相应的歌曲,歌曲的名称将显示在 TextView 中的搜索栏上方,id 为“selectedItem”。但是数据绑定是在 MediaCursorAdapter.class 中完成的,它将 MediaStore.MediaColumns.DATA 作为字符串返回给 MainActivity.java 类的方法 onListItemClick()。此处将当前歌曲的名称设置为字符串。但这显示了 App 中歌曲的整个路径,看起来很糟糕。有没有办法在我的 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

【问题讨论】:

    标签: android mediastore android-music-player displayname-attribute


    【解决方案1】:

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

        MediaStore.Audio.Media.TITLE
    

    或者如果你有完整的路径

     String trackname = fullpath.substring(fullpath.lastIndexOf("/") + 1, fullpath.length());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多