博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现音乐播放器
阅读量:4633 次
发布时间:2019-06-09

本文共 7232 字,大约阅读时间需要 24 分钟。

音乐播放器

   首先声明一下,本例是直接采用课本中范例122的方法。

效果图如下:

1、activity_main.xml布局

 

1 //四个按钮 2     
6 // 上一首 7
14 // 播放15
22 // 下一首23
30 // 暂停 31
38
View Code

 

2、MainAxtivity.java

  (1)定义四个按钮

  (2)绑定相应的监听对象

  (3)传递不同点击参数

代码如下:

1 public class MainActivity extends Activity implements OnClickListener { 2         //初始化控件 3     private Button mBtnPrevious; // 上一首 4     private Button mBtnPlay; // 播放 5     private Button mBtnNext; // 下一首 6     private Button mBtnPause; // 暂停 7     private ComponentName component; // 用于启动服务 8  9     public void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         setContentView(R.layout.activity_main);12         // 得到布局中的控件13         findView();14         // 绑定控件事件15         setListener();16     }17 18     // 得到布局中的控件19     private void findView() {20         component = new ComponentName(this, MusicService.class);21         mBtnPrevious = (Button) findViewById(R.id.previous);22         mBtnPlay = (Button) findViewById(R.id.play);23         mBtnNext = (Button) findViewById(R.id.next);24         mBtnPause = (Button) findViewById(R.id.pause);25     }26     // 绑定控件事件27     private void setListener() {28         mBtnPrevious.setOnClickListener(this);29         mBtnPlay.setOnClickListener(this);30         mBtnNext.setOnClickListener(this);31         mBtnPause.setOnClickListener(this);32     }33     // 按钮点击事件响应34     public void onClick(View v) {35         // 如果点击前一首歌,就在intent中传递前一首歌参数36         if (v == mBtnPrevious) {37             Intent mIntent = new Intent(MusicService.PREVIOUS_ACTION);38             mIntent.setComponent(component);39             startService(mIntent);40         // 如果点击前播放歌曲,就在intent中传递播放当前歌参数41         } else if (v == mBtnPlay) {42             Intent mIntent = new Intent(MusicService.PLAY_ACTION);43             mIntent.setComponent(component);44             startService(mIntent);45         // 如果点击前一首歌,就在intent中传递下一首歌参数46         } else if (v == mBtnNext) {47             Intent mIntent = new Intent(MusicService.NEXT_ACTION);48             mIntent.setComponent(component);49             startService(mIntent);50         // 如果点击前一首歌,就在intent中传递暂停首歌参数51         } else {52             Intent mIntent = new Intent(MusicService.PAUSE_ACTION);53             mIntent.setComponent(component);54             startService(mIntent);55         }56     }57 }
主要代码

3、自定义Service

    (1)获取音频数据的字段名称

     (2) 初始化MediaPlayer对象

    (3)通过getContentResolver得到系统中所有音乐,只获取播放时间在10秒以上的音乐

    (4)onStart方法中判断得到的intent中的参数,调用相应方法

    (5)分别定义音乐的播放、暂停、前一首、下一首的实现内容

代码如下:

1 //定义音乐服务类  2 public class MusicService extends Service {  3     // 定义需要显示的音乐的字段  4     String[] mCursorCols = new String[] {  5             "audio._id AS _id", // index must match IDCOLIDX below  6             MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,  7             MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,  8             MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.ALBUM_ID,  9             MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.DURATION }; 10     private MediaPlayer mMediaPlayer; // 声明播放器 11     private Cursor mCursor; // 声明游标 12     private int mPlayPosition = 0; // 当前播放的歌曲 13  14     // 注册意图 15     public static final String PLAY_ACTION = "com.wyl.music.PLAY_ACTION"; 16     public static final String PAUSE_ACTION = "com.wyl.music.PAUSE_ACTION"; 17     public static final String NEXT_ACTION = "com.wyl.music.NEXT_ACTION"; 18     public static final String PREVIOUS_ACTION = "com.wyl.music.PREVIOUS_ACTION"; 19  20     @Override 21     public IBinder onBind(Intent arg0) { 22         return null; 23     } 24  25     @Override 26     public void onCreate() { 27         super.onCreate(); 28         mMediaPlayer = new MediaPlayer(); 29         Uri MUSIC_URL = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;// 通过一个URI可以获取所有音频文件 30         //默认大于10秒的可以看作是系统音乐 31         mCursor = getContentResolver().query(MUSIC_URL, mCursorCols, 32                 "duration > 10000", null, null); 33     } 34  35     @Override 36     public void onStart(Intent intent, int startId) { 37         super.onStart(intent, startId); 38         // 根据不同的action,做不同的相应 39         String action = intent.getAction(); 40         //播放 41         if (action.equals(PLAY_ACTION)) { 42             play(); 43         //暂停 44         } else if (action.equals(PAUSE_ACTION)) { 45             pause(); 46         //下一首 47         } else if (action.equals(NEXT_ACTION)) { 48             next(); 49         //前一首 50         } else if (action.equals(PREVIOUS_ACTION)) { 51             previous(); 52         } 53     } 54  55     // 播放音乐 56     public void play() { 57         //初始化音乐播放器 58         inite(); 59     } 60  61     // 暂停时,结束服务 62     public void pause() { 63         //暂停音乐播放 64         stopSelf(); 65     } 66  67     // 上一首 68     public void previous() { 69         //得到前一首的歌曲 70         if (mPlayPosition == 0) { 71             mPlayPosition = mCursor.getCount() - 1; 72         } else { 73             mPlayPosition--; 74         } 75         //开始播放 76         inite(); 77     } 78  79     // 下一首 80     public void next() { 81         //得到后一首歌曲 82         if (mPlayPosition == mCursor.getCount() - 1) { 83             mPlayPosition = 0; 84         } else { 85             mPlayPosition++; 86         } 87         //开始播放 88         inite(); 89     } 90  91     // 初始化播放器 92     public void inite() { 93         //充值MediaPlayer 94         mMediaPlayer.reset(); 95         // 获取歌曲位置 96         String dataSource = getDateByPosition(mCursor, mPlayPosition); 97         // 歌曲信息 98         String info = getInfoByPosition(mCursor, mPlayPosition); 99         // 用Toast显示歌曲信息100         Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT)101                 .show();102         try {103             // 播放器绑定资源104             mMediaPlayer.setDataSource(dataSource);105             // 播放器准备106             mMediaPlayer.prepare();107             // 播放108             mMediaPlayer.start();109         } catch (IllegalArgumentException e1) {110             e1.printStackTrace();111         } catch (IllegalStateException e1) {112             e1.printStackTrace();113         } catch (IOException e1) {114             e1.printStackTrace();115         }116     }117 118     // 根据位置来获取歌曲位置119     public String getDateByPosition(Cursor c, int position) {120         c.moveToPosition(position);121         int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA);122         String data = c.getString(dataColumn);123         return data;124     }125 126     // 获取当前播放歌曲演唱者及歌名127     public String getInfoByPosition(Cursor c, int position) {128         c.moveToPosition(position);129         int titleColumn = c.getColumnIndex(MediaStore.Audio.Media.TITLE);130         int artistColumn = c.getColumnIndex(MediaStore.Audio.Media.ARTIST);131         String info = c.getString(artistColumn) + " "132                 + c.getString(titleColumn);133         return info;134 135     }136 137     // 服务结束时要释放MediaPlayer138     public void onDestroy() {139         super.onDestroy();140         mMediaPlayer.release();141     }142 }
音乐服务类

播放器完成!

 

转载于:https://www.cnblogs.com/j0820/p/4620104.html

你可能感兴趣的文章
ROM vs RAM
查看>>
mysql中sql语句
查看>>
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>
Python20-Day05
查看>>
Real World Haskell 第七章 I/O
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
ABAP 程序间的调用
查看>>
git分支管理
查看>>
移动端单屏解决方案
查看>>
一位资深Java架构师的晋级心得
查看>>
ass1_1
查看>>
senfile函数实例的运行过程截图
查看>>
程序编辑SHP文件并应用更改到数据源
查看>>
VS中C#读取app.config数据库配置字符串的三种方法(转)
查看>>
读取 android的内存、cpu、流量等 信息
查看>>
Python入门系列教程(三)列表和元组
查看>>
关于linux安装前规划分区二三事
查看>>