码迷,mamicode.com
首页 > 其他好文 > 详细

1600802032

时间:2018-12-12 00:35:01      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:final   ada   地址   raw   obj   info   log   设置   spl   

         一、实现的功能

           1.实现播放,暂停,停止,播放上一首,下一首功能  

           2.  显示播放列表

           3. 至少可以播放3首歌曲

        二、项目实现

  1. 对音乐播放器界面截图

技术分享图片       技术分享图片

        2.实现功能的关键代码 :

public class MainActivity extends AppCompatActivity implements OnClickListener {
    List<Object> musiclists = new ArrayList<Object>();
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    ImageButton play_pause, stop, onplay, downplay, close;
    ActivityReceiver activityReceiver;
    public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";
    public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
    Intent intentservice;
    // 定义音乐的播放状态 ,0X11 代表停止 ,0x12代表播放,0x13代表暂停
    int status = 0x11;
    @Override
    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) {
        // Inflate the menu; this adds items to the action bar if it is present.
        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) {
            // 获取Intent中的update消息,update代表播放状态
            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

1600802032

标签:final   ada   地址   raw   obj   info   log   设置   spl   

原文地址:https://www.cnblogs.com/zhangxiongwx/p/10105217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!