安卓音乐播放器

时间:2022-08-03 10:13:42

程序实现本地音乐文件的播放功能,显示播放歌曲的信息,显示本地歌词,从网络上获取歌词,摇一摇切歌,切换背景等功能!

1.要去实现播放音乐文件,因为程序关掉之后,音乐是不能停掉的,所以要在活动中去绑定服务,对音乐文件的操作都在服务中进行,

所以第一步新建工程之后将活动与新建的服务绑定,在这里给服务命名为MusicService,

public class MusicService extends Service {

private ArrayList<MyMusic>  data=new ArrayList<>();//用来保存所有的音乐文件信息,MyMusic为实例类,一个MyMusic代表一个音乐文件

  @Override
    public IBinder onBind(Intent intent) {
        return new LocalBinder();
    }

    @Override
    public void unbindService(ServiceConnection conn) {
        super.unbindService(conn);
    }

    public class LocalBinder extends Binder{

        public MusicService getService(){
            return  MusicService.this;
        }

 

 /**
     * 得到所有的音乐文件,这个方法去获得安卓手机里所有的音频文件,并保存在服务中
     * @return
     */
    private void getMusic(){

        Cursor cursor = getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null
        );
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            //得到文件id
            Long id = cursor.getLong(
                    cursor.getColumnIndex(MediaStore.Audio.Media._ID));
            //得到作者
            String author = cursor.getString(
                    cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            //得到标题
            String title = cursor.getString(cursor.getColumnIndex
                    (MediaStore.Audio.Media.TITLE));
            //得到路径,播放音乐时候需要用到,必不可少
            String url = cursor.getString(cursor.getColumnIndex
                    (MediaStore.Audio.Media.DATA));
            //得到播放时间
            Long time = cursor.getLong(
                    cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            //得到大小
            Long size = cursor.getLong(
                    cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
            //是否为音乐
            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
            if (isMusic != 0&& time > 100000&&size>3500000) {
                MyMusic myMusic = new MyMusic(url, title, author, size, time, id);

      //将音乐添加到ArrayList中ArrayList用来保存音乐文件信息
                data.add(myMusic);
            }
        }
        cursor.close();
    }
    }

}

服务中这么写,只是第一步,后续还有更多的功能需要去添加,接下来在活动去绑定服务,获取服务中的音乐数据

 private MusicAdapter adapter;
   private ArrayList<MyMusic> data=new ArrayList<>();

 private ServiceConnection conn= new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            //绑定服务后开始运行,记得打括号
            musicService=((MusicService.LocalBinder)service).getService();

            //得到数据
            data=musicService.getData();

            adapter = new MusicAdapter(ListActivity.this, data,height);

            listView_music.setAdapter(adapter);

            textview_title.setText(musicService.gettitle());

            if(musicService.isRun()){
                imageButton_pause.setImageDrawable(getResources().getDrawable(R.drawable.play));

            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService=null;
        }
    };

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_list);
        height=getWindowManager().getDefaultDisplay().getHeight();

        listView_music = (ListView) findViewById(R.id.listView);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        imageButton_pause = (ImageButton) findViewById(R.id.imageButton_pause);
        imageButton_next = (ImageButton) findViewById(R.id.imageButton_next);
        imageButton_special = (ImageButton) findViewById(R.id.imageButton_special);
        textview_title = (TextView) findViewById(R.id.textView_title);
        listView_left= (ListView) findViewById(R.id.draw_listview);
        line_list= (LinearLayout) findViewById(R.id.line_list);


        //初始化按钮
        imageButton_special.setImageDrawable(getResources().getDrawable(R.drawable.special));
        imageButton_pause.setImageDrawable(getResources().getDrawable(R.drawable.pause));
        imageButton_next.setImageDrawable(getResources().getDrawable(R.drawable.next));
        textview_title.setText("");

        intent=new Intent(this,MusicService.class);
        startService(intent);
        bindService(intent, conn, BIND_AUTO_CREATE);

        adpater_left=new Adpater_left(this);
        listView_left.setAdapter(adpater_left);
        listView_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case 0:
                        //视频播放
                        Intent intent_video=new Intent(ListActivity.this,VideoListActivity.class);
                        startActivity(intent_video);
                        break;
                    case 1:
                        break;
                    case 2:
                        Intent intent_set = new Intent(ListActivity.this, SetActivity.class);
                        startActivity(intent_set);
                        break;
                    case 3:
                        Intent intent_bg = new Intent(ListActivity.this, BG_Activity.class);
                        startActivity(intent_bg);
                        break;
                    case 4:
                        ListActivity.this.finish();
                        musicService.exit();
                        stopService(intent);
                        break;
                }
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int px, px1, px2;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                px1 = seekBar.getProgress();

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                px2 = seekBar.getProgress();
                if (px1 == px2) {
                    musicService.seek(px2);
                    p = px2;
                    musicService.setProgress(p);
                } else if (px - 500 < px1 && px1 < px1 + 500) {
                    musicService.seek(px2);
                    p = px2;
                    musicService.setProgress(p);
                }
                imageButton_pause.setImageDrawable(getResources().getDrawable(R.drawable.play));
            }
        });
        listView_music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //播放当前曲目
                musicService.play(position);
                seek();
                musicService.startTimer();
                textview_title.setText(musicService.gettitle());
                imageButton_pause.setImageDrawable(getResources().getDrawable(R.drawable.play));
            }

        });
        listView_music.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        registerForContextMenu(listView_music);
    }

 安卓音乐播放器安卓音乐播放器