馒头是我偶像 2019-02-09 17:02 采纳率: 14.3%
浏览 594
已采纳

在listView列表中点击歌曲,没有播放音乐,日志也打印不出来

ListContent类用来获取和设置歌曲的信息

public class ListContent {
    private int imageId;
    private String song;
    private int duration;
    private String songPath;

    public ListContent() {

    }

    public ListContent(int imageId,String song,int duration,String songPath) {

        this.imageId = imageId;
        this.song = song;
        this.duration = duration;
        this.songPath = songPath;
    }
    //获得歌曲的图片,歌名,时长,路径
    public int getImageId(){
        return imageId;
    }

    public String getSong(){
        return song;
    }

    public int getDuration(){
        return duration;
    }
    //设置歌曲的图片,歌名,时长,路径
    public String getSongPath() {
        return songPath;
    }

    public int setImageId(int ImageId) {
        return this.imageId = ImageId;
    }

    public String setSong(String song) {
        return this.song = song;
    }

    public int setDuration(int duration) {
        return this.duration = duration;
    }

    public String setSongPath(String songPath) {
        return this.songPath;
    }

}

GetMedia类用来获取手机中歌曲的标题,时长,路径

public class GetMedia {

    public static List<ListContent> getSongInfo(Context context){
        List<ListContent> songInfos = new ArrayList<ListContent>();
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        //cursor.moveToFirst();
        while(cursor.moveToNext()) {
            ListContent songInfo = new ListContent();
            //获取歌曲的标题
            String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            //MediaStore.Audio.Media.DURATION 音频文件的持续时间,以毫秒为单位
            int songDuration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            String songPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//获得歌曲的路径
            int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
            if(isMusic != 0) {
                songInfo.setImageId(R.drawable.first_song);
                songInfo.setSong(songName);
                songInfo.setDuration(songDuration);
                songInfo.setSongPath(songPath);
                songInfos.add(songInfo);
            }
        }
        cursor.close();
        return songInfos;
    }
}

主活动HomeActivity

public class HomeActivity extends Activity implements OnItemClickListener{
private List listContent;
private MediaPlayer mediaPlayer = new MediaPlayer();
private Button previous;
private Button repeat;
private Button play;
private Button shuffle;
private Button next;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    //静态方法应该使用ClassName.staticMethod()的形式使用
    listContent = GetMedia.getSongInfo(HomeActivity.this);
    SongListAdapter songListAdapter = new SongListAdapter(HomeActivity.this,R.layout.song_item,listContent);
    ListView listView = (ListView) findViewById(R.id.home_list);
    listView.setAdapter(songListAdapter);
    listView.setOnItemClickListener(this);
    previous = (Button) findViewById(R.id.previous);
    repeat = (Button) findViewById(R.id.repeat);
    play = (Button) findViewById(R.id.play);
    shuffle = (Button) findViewById(R.id.previoshuffleus);
    next = (Button) findViewById(R.id.next);
}

//监听listView
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    ListContent songListContent = listContent.get(position);//获得被点击的对象
    try {
        mediaPlayer.setDataSource(songListContent.getSongPath());//指定音频的路径
        //想打印出音频的路径,但日志没有显示
        String mySong = songListContent.getSongPath();
        Log.d("HomeActivity",mySong);
        mediaPlayer.prepareAsync();
    }catch (Exception e) {
        e.printStackTrace();
    }
    mediaPlayer.start();//播放音乐

}

}

  • 写回答

2条回答

  • 馒头是我偶像 2019-02-09 21:14
    关注

    问题解决了,是我在ListContent类中的setSongPath的返回值写成return this.songPath;应该改成return this.songPath = songPath;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?