Android在线音乐播放器
主要功能介绍
自动获取云端歌曲列表
自动加载云端音乐并播放
播放列表循环播放音乐
程序运行演示
程序运行截图
关键代码解析
获取云端音乐列表
这里使用腾讯云对象存储服务作为云端数据存储
Android端使用OkHttp3库进行HTTPS的GET请求
因为前期测试时,ListView初始化时无法正确读取AllMusic的数据
怀疑是回调函数的闭包所造成的,所以在这里使用广播的形式,将读取到的云端音乐列表广播出去后在处理加入AllMusic队列
但是在广播过程中也同时修改了初始化ListView的时间,所以造成无法正常初始话的原因有待商榷,可能是闭包造成的,也有可能是初始化过早造成的
- HTTPS的GET请求如下
String url = "https://code-1251175805.cos.ap-chengdu.myqcloud.com/List.txt"; final OkHttpClient okHttpClient=new OkHttpClient(); final Request request=new Request.Builder().url(url).build(); new Thread(new Runnable() { @Override public void run() { try { Response response=okHttpClient.newCall(request).execute(); if (response.isSuccessful()){ String body=response.body().string(); Intent intent = new Intent("com.example.leeli.musicplayer"); intent.putExtra(Intent.EXTRA_TEXT,body); MusicListBroad.sendBroadcast(intent); }else { Log.e("E", "run: "+response.code()+response.message()); } } catch (IOException e) { e.printStackTrace(); } } }).start();
- 数据处理如下:
IntentFilter filter = new IntentFilter("com.example.leeli.musicplayer"); MusicListBroad = LocalBroadcastManager.getInstance(this); MusicListBroad.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String ListBody = intent.getStringExtra(Intent.EXTRA_TEXT); Log.i("list1",ListBody); String Item[] = ListBody.split(" "); for(int i=0;i<Item.length;i++) { String temp[] = Item[i].split("@"); MusicInfo t = new MusicInfo(); t.Name = temp[0]; t.Url = temp[1]; t.Position = i; AllMusic.add(t); }
- 初始化ListView及表项的点击事件响应如下:
MusicList = (ListView) findViewById(R.id.MusicList); adapter = new BaseAdapter() { @Override public int getCount() { return AllMusic.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView MusicItem = new TextView(MainActivity.this); MusicItem.setText(AllMusic.get(position).Name); MusicItem.setTextSize(30); return MusicItem; } }; MusicList.setAdapter(adapter); MusicList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "开始播放:"+AllMusic.get(position).Name, Toast.LENGTH_LONG).show(); PlayMusic(position); } }); } },filter);
播放云端音乐
try { mediaPlayer.reset(); mediaPlayer.setDataSource(AllMusic.get(position).Url); mediaPlayer.prepare(); mediaPlayer.start(); Start.setText("暂停"); NowMusic = position; isFirst = false; } catch (Exception e) { e.printStackTrace(); }
云端的MusicList定义
G.E.M. 邓紫棋-光年之外@https://code-1251175805.cos.ap-chengdu.myqcloud.com/1.mp3 LON-我的一个道姑朋友@https://code-1251175805.cos.ap-chengdu.myqcloud.com/2.mp3 阿里郎-兰花指@https://code-1251175805.cos.ap-chengdu.myqcloud.com/3.mp3 岳云鹏-如果有个直达天堂的电梯@https://code-1251175805.cos.ap-chengdu.myqcloud.com/4.mp3 张杰_张碧晨-只要平凡@https://code-1251175805.cos.ap-chengdu.myqcloud.com/5.mp3
项目地址
https://github.com/leeli73/AndroidOnlineMusicPlayerDemo.git
APP下载链接
https://github.com/leeli73/AndroidOnlineMusicPlayerDemo/releases/download/Demo/OnlineMusicPlayer.apk