qq_39511347
馒头是我偶像
采纳率14.3%
2019-02-09 17:02

在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条回答

  • qq_39511347 馒头是我偶像 2年前

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

    点赞 评论 复制链接分享
  • laoliu1977 刘书旗 2年前

    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();

    点赞 评论 复制链接分享

相关推荐