广播+音乐播放器

广播

创建一个广播接收者

广播+音乐播放器

静态广播接收者

加频道

 <!-- 加频道 -->
            <intent-filter>
                <action android:name="com.zhang" />
                <action android:name="com.zhang1" />
            </intent-filter>

主方法中

 //给广播发送消息
    public void send(View view) {
        Intent intent = new Intent();
        intent.setAction("com.zhang");
        Bundle bundle = new Bundle();
        bundle.putString("name","憨憨赵岩博");
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }

    public void send1(View view) {
        Intent intent = new Intent();
        intent.setAction("com.zhang1");
        Bundle bundle = new Bundle();
        bundle.putString("name1","马保国");
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }

广播接收者

 String action = intent.getAction();
        if (action.equals("com.hehe")){
            Bundle extras = intent.getExtras();
            String name = extras.getString("name");
            Log.i(TAG, "onReceive: "+name);
        }else if (action.equals("com.hehe1")){
            Bundle extras = intent.getExtras();
            String name1 = extras.getString("name1");
            Log.i(TAG, "onReceive: "+name1);
        }

音乐播放器

在网页上下载音乐播放

 public void play(View view) {
        //初始化播放状态
        mediaPlayer.reset();
        //1.本地文件2.网络地址
        try {
            //加载数据源
            mediaPlayer.setDataSource("http://m10.music.126.net/20201124193128/6175f2c178baccf22b0208567d3ccd9e/ymusic/0152/535b/005a/68fda1acb7d170a837d3702701335098.mp3");
            //异步准备
            mediaPlayer.prepareAsync();
            //准备完成监听器
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    //开始播放
                    mediaPlayer.start();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void zt(View view) {
        mediaPlayer.pause();
    }
    //继续
    public void jx(View view) {
        mediaPlayer.start();
    }
    //暂停
    public void tz(View view) {
        mediaPlayer.stop();
    }
    //下一首
    public void next(View view) {
        playnext();
    }
    //上一首
    public void down(View view) {
        if (--index>=0){
            index=list.size()-1;
        }
        mu(index);
    }

读取本地音乐并播放

读取本地音乐

		public static List<Music> du(Context context){
        List<Music> list=new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (query!=null){
            while (query.moveToNext()){
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String name = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String id = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                if (Integer.parseInt(duration)>=1000*60){
                    Music music = new Music(title, name, duration, data, size, id);
                    list.add(music);
                }
            }
            query.close();
        }
        return list;
    }

音乐类

public class Music {
    private String title;
    private String name;
    private String duration;
    private String data;
    private String size;
    private String id;

    public Music(String title, String name, String duration, String data, String size, String id) {
        this.title = title;
        this.name = name;
        this.duration = duration;
        this.data = data;
        this.size = size;
        this.id = id;
    }

    public Music() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Music{" +
                "title='" + title + '\'' +
                ", name='" + name + '\'' +
                ", duration='" + duration + '\'' +
                ", data='" + data + '\'' +
                ", size='" + size + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}

播放

  public void play(int position){
     
        mediaPlayer.reset();
        try {
            index=position;
            mediaPlayer.setDataSource(list.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    switch (aaa){
                        case Musicutil.ORANGE:
                            next();
                            break;
                        case Musicutil.SINGLE:
                            play(index);
                            break;
                        case Musicutil.RANDOW:
                            int i = new Random().nextInt(list.size());
                            play(i);
                            break;
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
上一篇:相册


下一篇:第十六周总结