Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> 安卓音樂播放器

安卓音樂播放器

編輯:Android編程入門

程序實現本地音樂文件的播放功能,顯示播放歌曲的信息,顯示本地歌詞,從網絡上獲取歌詞,搖一搖切歌,切換背景等功能!

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);
    }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved