1600802032
一、实现的功能
1.实现播放,暂停,停止,播放上一首,下一首功能
2. 显示播放列表
3. 至少可以播放3首歌曲
二、项目实现
- 对音乐播放器界面截图
2.实现功能的关键代码 :
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UIinit(); logic(); musicList(); activityReceiver = new ActivityReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(UPDATE_ACTION); registerReceiver(activityReceiver, filter); intentservice = new Intent(this, MusicService.class); startService(intentservice); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public void UIinit() { play_pause = (ImageButton) this.findViewById(R.id.play_pause); stop = (ImageButton) this.findViewById(R.id.stop); onplay = (ImageButton) this.findViewById(R.id.onplay); close = (ImageButton) this.findViewById(R.id.close); downplay = (ImageButton) this.findViewById(R.id.downplay); } public void logic() { play_pause.setOnClickListener(this); stop.setOnClickListener(this); onplay.setOnClickListener(this); downplay.setOnClickListener(this); close.setOnClickListener(this); } @Override public void onClick(View source) { Intent intent = new Intent(CTL_ACTION); switch (source.getId()) { case R.id.play_pause: { intent.putExtra("control", 1); break; } case R.id.stop: { intent.putExtra("control", 2); break; } case R.id.onplay: { intent.putExtra("control", 3); break; } case R.id.downplay: { intent.putExtra("control", 4); break; } case R.id.close: { this.finish(); break; } } sendBroadcast(intent); } public class ActivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int update = intent.getIntExtra("update", -1); switch (update) { case 0x11: { play_pause.setImageResource(R.drawable.play); status = 0x11; break; } case 0x12: { play_pause.setImageResource(R.drawable.pause); status = 0x12; break; } case 0x13: { play_pause.setImageResource(R.drawable.play); status = 0x13; break; } } } } public void musicList() { String[] music = new String[] { Media._ID, Media.DISPLAY_NAME, Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA }; Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, music, null, null, null); while (cursor.moveToNext()) { Music temp = new Music(); temp.setFilename(cursor.getString(1)); temp.setTitle(cursor.getString(2)); temp.setDuration(cursor.getInt(3)); temp.setArtist(cursor.getString(4)); temp.setData(cursor.getString(5)); musiclists.add(temp); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", cursor.getString(1)); map.put("artist", cursor.getString(4)); list.add(map); } ListView listview = (ListView) findViewById(R.id.musics); SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.musicsshow, new String[] { "name", "artist" }, new int[] { R.id.name, R.id.artist }); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int current, long id) { Intent intent=new Intent(CTL_ACTION); intent.putExtra("control", 5); intent.putExtra("current", current); sendBroadcast(intent); } }); } }
3.代码链接:https://git.coding.net/yibula/Music.git
4.apk的下载地址:https://coding.net/u/yibula/p/Music/git/raw/master/1600802032/1600802032.apk
5.录屏链接:https://www.bilibili.com/video/av39908039/