基于android的网络音乐播放器-网络音乐的搜索和展示(五)

作为android初学者,最近把疯狂android讲义和疯狂Java讲义看了一遍,看到书中介绍的知识点非常多,很难全部记住,为了更好的掌握基础知识点,我将开发一个网络音乐播放器-EasyMusic来巩固下,也当作是练练手。感兴趣的朋友可以看看,有设计不足的地方也欢迎指出。

开发之前首先介绍下该音乐播放器将要开发的功能(需求):

1.本地音乐的加载和播放;

2.网络音乐的搜索,试听和下载;

3.音乐的断点下载;

4.点击播放图标加载专辑图片,点击歌词加载歌词并滚动显示(支持滑动歌词改变音乐播放进度);

5.支持基于popupWindow的弹出式菜单;

6.支持后台任务栏显示和控制。

该篇主要是介绍在NetFragment中实现网络音乐的搜索和展示功能开发:
先看下效果图如下
这里写图片描述

首先介绍下大概的实现思路:NetFragment布局顶部为搜索框SearchView,下面为一个ListView用来显示搜索到的网络音乐

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f0f" >
    </SearchView>

    <ListView
        android:id="@+id/netmusiclist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

搜索框中输入搜索关键字并点击搜索(软键盘最后一个搜索键),为了避免在主线程执行耗时任务,搜索时建立一个异步任务(NetFragment的内部类)执行搜索任务(包括建立Http连接,解析json数据)。异步任务的定义如下:

// 负责搜索音乐的异步任务,搜索完成后显示网络音乐列表
    private class SearchMusicTask extends AsyncTask<String, Void, Void> {
        private ListView musicList;

        public SearchMusicTask(ListView musicList) {
            this.musicList = musicList;
        }

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected Void doInBackground(String... params) {
            String url = params[0];
            try {
                HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
                conn.setConnectTimeout(5000);
                //使用缓存提高处理效率
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                //网络响应赋值给成员变量searchResponse
                searchResponse = sb.toString();
                parseResponse();
                Log.d(TAG, "searchResponse = " + searchResponse);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            //adapter数据更新后通知列表更新
            netMusicListAdapter.notifyDataSetChanged();
            //musicList.setAdapter(netMusicListAdapter);
        }

        //json解析网络响应
        private void parseResponse() {
            try {
                JSONObject response = new JSONObject(searchResponse);
                JSONObject result = response.getJSONObject("result");
                JSONArray songs = result.getJSONArray("songs");
                if (netMusicList.size() > 0) netMusicList.clear();
                for (int i = 0; i < songs.length(); i++) {
                    JSONObject song = songs.getJSONObject(i);
                    //获取歌曲名字
                    String title = song.getString("name");
                    //获取歌词演唱者
                    String artist = song.getJSONArray("artists")
                            .getJSONObject(0).getString("name");
                    //获取歌曲专辑图片的url
                    String albumPicUrl = song.getJSONObject("album").getString(
                            "picUrl");
                    //获取歌曲音频的url
                    String audioUrl = song.getString("audio");
                    Log.d(TAG, "doenloadUrl = " + audioUrl);
                    //保存音乐信息的Map
                    Map<String, Object> item = new HashMap<>();
                    item.put("title", title);
                    item.put("artist", artist);
                    item.put("picUrl", albumPicUrl);
                    picUrlMap.put(title + artist, new SoftReference<String>(
                            albumPicUrl));
                    item.put("audio", audioUrl);
                    //将一条歌曲信息存入list中
                    netMusicList.add(item);
                }
                Log.d(TAG, "搜到" + netMusicList.size() + "首歌");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

异步任务在执行之前会调用onPreExecute(这里可以进行一些初始化的操作),然后调用核心方法doInBackground(该方法运行在独立的线程中,耗时操作往往放在该方法里),当doInBackground执行完成后会回调onPostExecute方法,当执行完任务在该方法中更新UI,值得指出来的是异步任务的onPreExecute,onProgressUpdate(该方法通常用在需要实时更新任务进度的场景),onPostExecute都是运行在主线程中。该异步任务的主要难点是json数据的解析上。json数据的解析可以参考我的另一篇博客–json数据的解析

根据搜索得到网络音乐列表后应该考虑如下进行下载了,在异步任务里我们已经将音乐的音频url保存起来了,下一步只需要根据点击的歌曲取出相应的url建立Http连接并取出输入流文件-音频文件。该篇主要介绍网络音乐的搜索和展示,下载任务的建立和下载列表的更新将放在下一篇中将。下面附上NetFragment.java的完整代码。

package com.sprd.easymusic.fragment;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sprd.easymusic.MainActivity;
import com.sprd.easymusic.R;
import com.sprd.easymusic.util.DownloadTask;
import com.sprd.easymusic.util.DownloadUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.Toast;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

public class NetFragment extends Fragment {
    private final String TAG = "NetFragment";
    //音乐搜索接口
    public static final String CLOUD_MUSIC_API_PREFIX = "http://s.music.163.com/search/get/?";
    private Context mContext;
    //获取输入的搜索框
    private SearchView searchView;
    //根据关键字搜索到的音乐列表
    private ListView netMusicListView;
    //存放搜索到的音乐信息的list
    private List<Map<String, Object>> netMusicList = new ArrayList<>();
    //获取的json数据格式的网络响应赋值给searchResponse
    private String searchResponse = null;
    private LayoutInflater inflater;
    //缓存专辑图片的下载链接,当下载完成并播放歌曲时,点击播放图标加载专辑图标时避免重新联网搜索,直接从缓存中获取
    private Map<String, SoftReference<String>> picUrlMap = new HashMap<String, SoftReference<String>>();

    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getActivity();
        inflater = LayoutInflater.from(mContext);
        Log.d(TAG, "onCreate");
    }

    // NetFragment向外界展示的内容,返回值为view
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.netmusic, container, false);
        netMusicListView = (ListView) view.findViewById(R.id.netmusiclist);
        netMusicListView.setAdapter(netMusicListAdapter);
        netMusicListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击搜索到的网络音乐列表的第position项即可建立下载任务下载对应歌曲,下一篇介绍
                //createDownloadTask(position);
            }
        });
        searchView = (SearchView) view.findViewById(R.id.searchView);
        //搜索框监听,点击搜索建立异步任务执行搜索任务
        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(mContext, "搜索内容为:" + query, 100).show();
                String musicUrl = getRealUrl(query);
                new SearchMusicTask(netMusicListView).execute(musicUrl);
                return false;
            }

            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        searchView.setOnCloseListener(new OnCloseListener() {

            public boolean onClose() {
                netMusicListView.setAdapter(null);
                // netMusicList.clear();adapter.notifyDataSetChanged();
                return false;
            }
        });
        return view;
    }

    // 网络音乐列表适配器
    private BaseAdapter netMusicListAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return netMusicList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View view = convertView;
            Map<String, Object> item = netMusicList.get(position);
            if (convertView == null) {
                view = inflater.inflate(R.layout.netmusiclist_item, null);
            }
            TextView musicTitle = (TextView) view.findViewById(R.id.musicTitle);
            musicTitle.setTag("title");
            TextView musicArtist = (TextView) view.findViewById(R.id.musicArtist);
            musicTitle.setText((String) item.get("title"));
            musicArtist.setText((String) item.get("artist"));
            return view;
        }

    };

    //返回可以访问的网络资源
    private String getRealUrl(String query) {
        String key = null;
        try {
            key = URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return CLOUD_MUSIC_API_PREFIX + "type=1&s='" + key
                + "'&limit=20&offset=0";
    }

    public void onDestroy() {
        super.onDestroy();
    }

    // 负责搜索音乐的异步任务,搜索完成后显示网络音乐列表
    private class SearchMusicTask extends AsyncTask<String, Void, Void> {
        private ListView musicList;

        public SearchMusicTask(ListView musicList) {
            this.musicList = musicList;
        }

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected Void doInBackground(String... params) {
            String url = params[0];
            try {
                HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
                conn.setConnectTimeout(5000);
                //使用缓存提高处理效率
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                //网络响应赋值给成员变量searchResponse
                searchResponse = sb.toString();
                parseResponse();
                Log.d(TAG, "searchResponse = " + searchResponse);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            //adapter数据更新后通知列表更新
            netMusicListAdapter.notifyDataSetChanged();
            //musicList.setAdapter(netMusicListAdapter);
        }

        //json解析网络响应
        private void parseResponse() {
            try {
                JSONObject response = new JSONObject(searchResponse);
                JSONObject result = response.getJSONObject("result");
                JSONArray songs = result.getJSONArray("songs");
                if (netMusicList.size() > 0) netMusicList.clear();
                for (int i = 0; i < songs.length(); i++) {
                    JSONObject song = songs.getJSONObject(i);
                    //获取歌曲名字
                    String title = song.getString("name");
                    //获取歌词演唱者
                    String artist = song.getJSONArray("artists")
                            .getJSONObject(0).getString("name");
                    //获取歌曲专辑图片的url
                    String albumPicUrl = song.getJSONObject("album").getString(
                            "picUrl");
                    //获取歌曲音频的url
                    String audioUrl = song.getString("audio");
                    Log.d(TAG, "doenloadUrl = " + audioUrl);
                    //保存音乐信息的Map
                    Map<String, Object> item = new HashMap<>();
                    item.put("title", title);
                    item.put("artist", artist);
                    item.put("picUrl", albumPicUrl);
                    picUrlMap.put(title + artist, new SoftReference<String>(
                            albumPicUrl));
                    item.put("audio", audioUrl);
                    //将一条歌曲信息存入list中
                    netMusicList.add(item);
                }
                Log.d(TAG, "搜到" + netMusicList.size() + "首歌");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

}

音乐播放器已完成,下载地址:
Android音乐播放器

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值