当前位置:天才代写 > tutorial > 安卓教程 > 安卓音乐播放器

安卓音乐播放器

2021-02-03 12:16 星期三 所属: 安卓教程 浏览:21

程序代码当地音频文件的播放功能,表明播放音乐的信息内容,表明当地歌曲歌词,从互联网上获得歌曲歌词,摇一摇切歌,转换情况等作用!

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

 

 

    关键字:

天才代写-代写联系方式