广播+音乐播放器
程序员文章站
2022-03-09 08:03:24
广播创建一个广播接收者静态广播接收者创建一个广播接收者静态广播接收者加频道 主方法中 /...
创建一个广播接收者
静态广播接收者
加频道
<!-- 加频道 -->
<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();
}
}
本文地址:https://blog.csdn.net/weixin_52360287/article/details/109888975
推荐阅读
-
flv f4v mp4 视频播放器代码
-
Python实现的视频播放器功能完整示例
-
php网站视频播放器-手机网站~求解决思路
-
python获取酷狗音乐top500的下载地址 MP3格式
-
在vue-music中关于Player播放器组件的使用说明
-
UNP卷1:第二十章(广播)
-
米聊突然更新:广播提醒增红点/气泡 小米钱包入驻
-
Android 入门第十讲02-广播(广播概述,使用方法(系统广播,自定义广播,两个activity之间的交互和传值),EventBus使用方法,数据传递,线程切换,Android的系统广播大全)
-
Java swing仿酷狗音乐播放器
-
使用Python实现下载网易云音乐的高清MV