【达内课程】音乐播放器4.0(播放准备)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010356768/article/details/83107323

这一节我们需要实现播放歌曲的功能。想要实现播放歌曲,需要得到歌曲的url地址。由于上一节的接口中我们得到的歌曲列表中并没有歌曲地址,所以我们需要根据song_id再次发送请求,获取歌曲url
百度音乐api接口

流程如下
在这里插入图片描述

NewMusicListFragment
中给listview添加点击事件

public class NewMusicListFragment extends Fragment {
    private ListView listView;
    private MusicAdapter adapter;
    private List<Music> musics;
    private MusicModel model = new MusicModel();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music_list,null);

        setViews(view);
        setListeners();
        ......
        return view;
    }

    private void setListeners() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final Music music = musics.get(i);
                String songId = music.getSong_id();
                Toast.makeText(getActivity(),"position:"+i,Toast.LENGTH_SHORT).show();
                //执行业务层 加载这首歌曲的基本信息
                model.loadMusicInfoBySongId(songId, new MusicInfoCallback() {
                    @Override
                    public void onMusicInfoLoaded(Songinfo songinfo, BitrateModel bitrateModel) {
                        music.setSonginfo(songinfo);
                        music.setBitrate(bitrateModel);
                        //准备通过url地址执行播放业务
                    }
                });
            }
        });
    }

    ......
}

MusicModel
中增加根据songid获取歌其他信息的方法

public class MusicModel {

    /**
     * 根据songid异步记载这首歌的其他信息
     * @param songid
     * @param callback
     */
    public void loadMusicInfoBySongId(final String songid, final MusicInfoCallback callback){
        AsyncTask<String,String,Music> task = new AsyncTask<String, String, Music>() {
            //工作线程中发送请求 获取基本信息
            @Override
            protected Music doInBackground(String... strings) {
                String url = UrlFactory.getSongInfoUrl(songid);
                try {
                    InputStream is = HttpUtils.getInputStream(url);
                    String json = HttpUtils.isToString(is);
                    Log.d("info",json);

                    JSONObject object = new JSONObject(json);
                    JSONObject songInfo = object.getJSONObject("songinfo");
                    JSONObject bitrate = object.getJSONObject("bitrate");

                    Songinfo songinfoModel = JSONParser.parseSongInfo(songInfo);
                    BitrateModel bitrateModel = JSONParser.parseBitrate(bitrate);

                    Music music = new Music();
                    music.setSonginfo(songinfoModel);
                    music.setBitrate(bitrateModel);
                    //把music交给onPostExecute
                    return music;
                } catch (IOException e) {
                    e.printStackTrace();
                }catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Music music) {
                if(music == null){
                    callback.onMusicInfoLoaded(null,null);
                }else {
                    //从上面传来的music
                    callback.onMusicInfoLoaded(music.getSonginfo(),music.getBitrate());
                }
            }
        };
        task.execute();
    }
	......
}

MusicInfoCallback

/**
 * 音乐数据相关的回调接口
 */
public interface MusicInfoCallback {
    /**
     * 当音乐信息加载完成将会执行该方法
     */
    void onMusicInfoLoaded(Songinfo songinfo,BitrateModel bitrateModel);
}

根据接口返回的json,需要再Model模型中增加

**
 * 封装一首歌的信息
 */
public class Music {
   ......
    private Songinfo songinfo;
    private BitrateModel bitrate;

    public Music() { ;
    }

    ......
    public Songinfo getSonginfo() {
        return songinfo;
    }

    public void setSonginfo(Songinfo songinfo) {
        this.songinfo = songinfo;
    }

    public BitrateModel getBitrate() {
        return bitrate;
    }

    public void setBitrate(BitrateModel bitrate) {
        this.bitrate = bitrate;
    }


    @Override
    public String toString() {
        return this.title;
    }
}

根据接口返回数据创建BitrateModel

public class BitrateModel {
    private String show_link;//"http://zhangmenshiting.qianqian.com/data2/music/45bddc070feaeb4e690da721bf92a938/604682956/604682956.mp3?xcode=9ecab6d33c85c56ddcd0b1435228577d",
    private String free;//1,
    private String song_file_id;//604682956,
    private String file_size;//3857267,
    private String file_extension;//"mp3",
    private String file_duration;//240,
    private String file_bitrate;//128,
    private String file_link;//"http://zhangmenshiting.qianqian.com/data2/music/45bddc070feaeb4e690da721bf92a938/604682956/604682956.mp3?xcode=9ecab6d33c85c56ddcd0b1435228577d",
    private String hash;
    
    ......
	省略各种set、get方法
}

根据接口返回数据创建SongInfo

public class Songinfo {
    public String pic_huge;// "http://qukufile2.qianqian.com/data2/pic/920b3f0596b54f54a3c5575aabefe2b8/604682905/604682905.jpg@s_1,w_1000,h_1000",
    public String pic_premium;// "http://qukufile2.qianqian.com/data2/pic/920b3f0596b54f54a3c5575aabefe2b8/604682905/604682905.jpg@s_1,w_500,h_500",
    public String author;// "李剑青",
    public String song_id;// "604682907",
    public String title;// "找到你(电影《找到你》同名主题曲)",
    public String artist_id;// "107692473",
    public String lrclink;// "http://qukufile2.qianqian.com/data2/lrc/f1a7467936a4cff1c7c52483a741d4ce/605187388/605187388.lrc",
    public String pic_big;// "http://qukufile2.qianqian.com/data2/pic/920b3f0596b54f54a3c5575aabefe2b8/604682905/604682905.jpg@s_1,w_150,h_150",
    public String album_id;// "604682904",
    public String pic_radio;// "http://qukufile2.qianqian.com/data2/pic/920b3f0596b54f54a3c5575aabefe2b8/604682905/604682905.jpg@s_1,w_300,h_300",
    public String album_title;// "找到你(电影《找到你》同名主题曲)",
    public String pic_small;

	......
	省略各种set、get方法
}

UrlFactory增加

    /**
     * 新歌榜url地址
     * @param offSet
     * @param size
     * @return  xml格式字符串
     */
    public static String getNewMusicListUrl(int offSet,int size){
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?format=xml&calback=&from=webapp_music&method=baidu.ting.billboard.billList&type=1&size="+size+"&offset="+offSet;
        return url;
    }

JSONParser
来解析数据

public class JSONParser {

    public static Songinfo parseSongInfo(JSONObject songInfo) throws JSONException {
        Songinfo songinfoModel = new Songinfo(
                songInfo.getString("pic_huge") ,
                songInfo.getString("pic_premium") ,
                songInfo.getString("author") ,
                songInfo.getString("song_id") ,
                songInfo.getString("title") ,
                songInfo.getString("artist_id") ,
                songInfo.getString("lrclink") ,
                songInfo.getString("pic_big") ,
                songInfo.getString("album_id") ,
                songInfo.getString("pic_radio") ,
                songInfo.getString("album_title") ,
                songInfo.getString("pic_small")
                );
        return songinfoModel;
    }

    public static BitrateModel parseBitrate(JSONObject bitrate) throws JSONException {
        BitrateModel bitrateModel = new BitrateModel(
                bitrate.getString("show_link") ,
                bitrate.getString("free") ,
                bitrate.getString("song_file_id") ,
                bitrate.getString("file_size") ,
                bitrate.getString("file_extension") ,
                bitrate.getString("file_duration") ,
                bitrate.getString("file_bitrate") ,
                bitrate.getString("file_link") ,
                bitrate.getString("hash")
        );
        return bitrateModel;
    }
}

下一节中根据url来实现播放

猜你喜欢

转载自blog.csdn.net/u010356768/article/details/83107323