版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、本文出自:欢迎转载,转载请注明出自: 安卓开发网下面将和大家一起介绍一个音乐播放器项目,完成后的播放器具有暂停,下一首,前一首,歌曲列表,播放条进度等一些基本功能,它只是播放SDCARD上的.map文件,Android SDK为我们提供了一个MeidaPlayer类,有了这个类我们可以很方便的创建一个mdeiaplayer服务,该类中具有一些方法:MediaPlayer mp = new MediaPlayer(); / 设置文件存储路径setDataSource("/sdcard/path_to_song"); / 播放mp.start(); / 暂短mp.pause()
2、; / 复位mp.reset(); / 获取当前播放时长mp.getDuration(); / 进度条mp.getCurrentDuration(); / Move song to particular second - used for Forward or Backwardmp.seekTo(positon); / position in milliseconds / 检测歌曲是否正在播放mp.isPlaying(); / returns true or false1:音乐播放器的界面:播放器界面中所用的一些布局图片,这些图片大家可以到android SDK文件夹下或是到网络中去寻找一些类
3、似就可以,并不一定要这些图片,代码才是我们的关键,3:然后我们需要写一个XML布局文件用于播放器的播按钮在不同状态下的图片,此XML文件保存在drawable文件夹下,btn_play.xml<selector xmlns:android=" <item android:drawable="drawable/img_btn_play_pressed" android:state_focused="true" android:state_pressed="true" /> <item android:d
4、rawable="drawable/img_btn_play_pressed" android:state_focused="false" android:state_pressed="true" /> <item android:drawable="drawable/img_btn_play_pressed" android:state_focused="true" /> <item android:drawable="drawable/img_btn_pla
5、y" android:state_focused="false" android:state_pressed="false" /></selector>提示:大家可以根据此XML文件完成其它的一些按钮布局文件,d 在这里就没有再为大家一一提供了,4:为了让播放器更绚,我们可以自定义一个SeekBar作为歌曲的播放进度,自定义风格代如下:(4.1)更换背景图片:seekbar_progress_bg.xml<?xml version="1.0" encoding="utf-8"?&g
6、t; <layer-list xmlns:android=" <item> <clip> <bitmap xmlns:android=" android:src="drawable/img_seekbar_progress_blue" android:tileMode="repeat" android:antialias="true" android:dither="false" android:filter="false" android
7、:gravity="left" /> </clip> </item>(4.2)Changing SeekBar Progress:seekbar_progress.xml<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android=" <item android:id="android:id/background" android:drawable="drawable/img_
8、seekbar_bg" android:dither="true"> </item> <item android:id="android:id/secondaryProgress"> <clip> <shape> <gradient android:startColor="#80028ac8" android:centerColor="#80127fb1" android:centerY="0.75" android:endC
9、olor="#a004638f" android:angle="270" /> </shape> </clip> </item> <item android:id="android:id/progress" android:drawable="drawable/seekbar_progress_bg" /></layer-list>(4.3)实际的Seekbar控件定义如下 :<SeekBar android:id="+id/song
10、ProgressBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_marginBottom="20dp" android:layout_above="id/player_footer_bg" andro
11、id:thumb="drawable/seek_handler" android:progressDrawable="drawable/seekbar_progress" android:paddingLeft="6dp" android:paddingRight="6dp"/>5:接下来我们将实现播放的整体界面的布局文件:player.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout x
12、mlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:background="color/player_background"> <!- Player Header -> <LinearLayout android:id="+id/player_header_bg" android:layout_width="fill_parent&q
13、uot; android:layout_height="60dip" android:background="layout/bg_player_header" android:layout_alignParentTop="true" android:paddingLeft="5dp" android:paddingRight="5dp"> <!- Song Title -> <TextView android:id="+id/songTitle" an
14、droid:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#04b3d2" android:textSize="16dp" android:paddingLeft="10dp" android:textStyle="bold" android:text="The Good,
15、The Bad And The Ugly" android:layout_marginTop="10dp"/> <!- Playlist button -> <ImageButton android:id="+id/btnPlaylist" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="drawable/btn_playlist" andr
16、oid:background="null"/> </LinearLayout> <!- Song Thumbnail Image -> <LinearLayout android:id="+id/songThumbnail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom
17、="10dp" android:gravity="center" android:layout_below="id/player_header_bg"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="drawable/adele"/> </LinearLayout> <!- Player Foo
18、ter -> <LinearLayout android:id="+id/player_footer_bg" android:layout_width="fill_parent" android:layout_height="100dp" android:layout_alignParentBottom="true" android:background="layout/bg_player_footer" android:gravity="center">
19、<!- Player Buttons -> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" android:background="layout/rounded_corner" android:paddingLeft=&q
20、uot;10dp" android:paddingRight="10dp"> <!- Previous Button -> <ImageButton android:id="+id/btnPrevious" android:src="drawable/btn_previous" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background=&
21、quot;null"/> <!- Backward Button -> <ImageButton android:id="+id/btnBackward" android:src="drawable/btn_backward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="null"/> <!- Play But
22、ton -> <ImageButton android:id="+id/btnPlay" android:src="drawable/btn_play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="null"/> <!- Forward Button -> <ImageButton android:id="+i
23、d/btnForward" android:src="drawable/btn_forward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="null"/> <!- Next Button -> <ImageButton android:id="+id/btnNext" android:src="drawable
24、/btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="null"/> </LinearLayout> </LinearLayout> <!- Progress Bar/Seek bar -> <SeekBar android:id="+id/songProgressBar" android:layout_w
25、idth="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_marginBottom="20dp" android:layout_above="id/player_footer_bg" android:thumb="drawable/seek_handl
26、er" android:progressDrawable="drawable/seekbar_progress" android:paddingLeft="6dp" android:paddingRight="6dp"/> <!- Timer Display -> <LinearLayout android:id="+id/timerDisplay" android:layout_above="id/songProgressBar" android:layout
27、_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp"> <!- Current Duration Label -> <TextView android:id="+id/songCurrentDurat
28、ionLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:textColor="#eeeeee" android:textStyle="bold"/> <!- Total Duration Label -> <TextView
29、 android:id="+id/songTotalDurationLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:textColor="#04cbde" android:textStyle="bold"/> </Line
30、arLayout> <!- Repeat / Shuffle buttons -> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="id/timerDisplay" android:gravity="center"> <!- Repeat Button -> <ImageButton android:i
31、d="+id/btnRepeat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="drawable/btn_repeat" android:layout_marginRight="5dp" android:background="null"/> <!- Shuffle Button -> <ImageButton android
32、:id="+id/btnShuffle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="drawable/btn_shuffle" android:layout_marginLeft="5dp" android:background="null"/> </LinearLayout></RelativeLayout>6:有了
33、播放器的播放界面后,我们再将所有歌曲显示的界面实现了,播放器的歌曲列表用的一个listview控件,同样我们也为该listview控件自定义,适合播放的整体界面风格,list_selector.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="<!- Selector style for listrow -><item android:state_selected="false" android:state_
34、pressed="false" android:drawable="drawable/gradient_bg" /><item android:state_pressed="true" android:drawable="drawable/gradient_bg_hover" /><item android:state_selected="true" android:state_pressed="false" android:drawable=&quo
35、t;drawable/gradient_bg_hover" /></selector>歌曲列表界面布局文件:playlist.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="v
36、ertical"> <ListView android:id="android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="#242424" android:dividerHeight="1dp" android:listSelector="drawable/list_selector" /> <
37、;/LinearLayout>listviewr的item界面布局,用于定义单个歌曲的显示界面:playlist_item.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_pare
38、nt" android:orientation="vertical" android:gravity="center" android:background="drawable/list_selector" android:padding="5dp"> <TextVi
39、ew android:id="+id/songTitle" android:layout_width="fill_parent" android:layout_height="wrap_content"
40、0; android:textSize="16dp" android:padding="10dp" android:color="#f3f3f3"/></LinearLayout>歌曲列表界面图:7:到此为止我们在上面已经完成了该项目的所有布局文件,下面我们就来实现SongManager.java类
41、,这个类用于扫描设备中所有.mp3文件,SongsManager.mp3public class SongsManager / SDCard Path final String MEDIA_PATH = new String("/sdcard/"); private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); / Constructor public SongsManager() /* * Fu
42、nction to read all mp3 files from sdcard * and store the details in ArrayList * */ public ArrayList<HashMap<String, String>> getPlayList() File home = new File(MEDIA_PATH); if (home.listFiles(new FileExtensionFilter().length > 0) for (File file : home.listFiles(new FileExtensionFilter
43、() HashMap<String, String> song = new HashMap<String, String>(); song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4); song.put("songPath", file.getPath(); / Adding each song to SongList songsList.add(song); / return songs list array return
44、songsList; /* * Class to filter files which are having .mp3 extension * */ class FileExtensionFilter implements FilenameFilter public boolean accept(File dir, String name) return (name.endsWith(".mp3") | name.endsWith(".MP3"); 8:创建一个新的Activity 类,该类用于显示在ongsManager.java中扫描到的所有歌曲,P
45、layListActivity.javapackage com.androidhive.musicplayer; import java.util.ArrayList;import java.util.HashMap; import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClic
46、kListener;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.SimpleAdapter; public class PlayListActivity extends ListActivity / Songs list public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); Over
47、ride public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.playlist); ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>(); SongsManager plm = new SongsManager(); / get all songs from
48、 sdcard this.songsList = plm.getPlayList(); / looping through playlist for (int i = 0; i < songsList.size(); i+) / creating new HashMap HashMap<String, String> song = songsList.get(i); / adding HashList to ArrayList songsListData.add(song); / Adding menuItems to ListView ListAdapter adapter
49、 = new SimpleAdapter(this, songsListData, R.layout.playlist_item, new String "songTitle" , new int R.id.songTitle ); setListAdapter(adapter); / selecting single ListView item ListView lv = getListView(); / listening to single listitem click lv.setOnItemClickListener(new OnItemClickListener
50、() Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) / getting listitem index int songIndex = position; / Starting new intent Intent in = new Intent(getApplicationContext(), AndroidBuildingMusicPlayerActivity.class); / Sending songIndex to PlayerActivity
51、 in.putExtra("songIndex", songIndex); setResult(100, in); / Closing PlayListView finish(); ); 9:下面我们再来实现播放的主类AndroidBuildingMusicPlayerActivity.java:该类主要实现功能如下:1:在歌曲列表界面点击一首歌曲进入播放界面;2:播放,暂停,快进,复位等功能实现3:重复播放单一歌曲,随机播放等该类的完整代码如下:public class AndroidBuildingMusicPlayerActivity extends Activity
52、 implements OnCompletionListener, SeekBar.OnSeekBarChangeListener private ImageButton btnPlay; private ImageButton btnForward; private ImageButton btnBackward; private ImageButton btnNext; &
53、#160; private ImageButton btnPrevious; private ImageButton btnPlaylist; private ImageButton btnRepeat; private ImageButton btnShuffle; private SeekBar songProgressBar; private Text
54、View songTitleLabel; private TextView songCurrentDurationLabel; private TextView songTotalDurationLabel; / Media Player private MediaPlayer mp; / Handler to update UI timer, progr
55、ess bar etc,. private Handler mHandler = new Handler(); private SongsManager songManager; private Utilities utils; private int seekForwardTime = 5000; / 5000 milliseconds private int se
56、ekBackwardTime = 5000; / 5000 milliseconds private int currentSongIndex = 0; private boolean isShuffle = false; private boolean isRepeat = false; private ArrayList<HashMap<String, String>> songsLi
57、st = new ArrayList<HashMap<String, String>>(); Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); s
58、etContentView(R.layout.player); / 所有的播放按钮 btnPlay = (ImageButton) findViewById(R.id.btnPlay); btnForward = (ImageButton) findViewById(R.id.btnForward);
59、160; btnBackward = (ImageButton) findViewById(R.id.btnBackward); btnNext = (ImageButton) findViewById(R.id.btnNext); btnPrevious = (ImageButton) findViewById(R.id.bt
60、nPrevious); btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist); btnRepeat = (ImageButton) findViewById(R.id.btnRepeat); btnShuffle = (ImageButton) find
61、ViewById(R.id.btnShuffle); songProgressBar = (SeekBar) findViewById(R.id.songProgressBar); songTitleLabel = (TextView) findViewById(R.id.songTitle); songCurren
62、tDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel); songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel); / Mediaplayer
63、; mp = new MediaPlayer(); songManager = new SongsManager(); utils = new Utilities(); / Listeners
64、0;songProgressBar.setOnSeekBarChangeListener(this); / Important mp.setOnCompletionListener(this); / Important / 获取所有歌曲列表 songsList = songManager.getPlayL
65、ist(); / 默认从第一首开始播放 playSong(0); /* * Play button click event * plays a song and changes button to pause image * pauses a song and changes button to play image * */
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年池州职业技术学院单招综合素质考试参考题库含详细答案解析
- 2026年贵阳职业技术学院单招综合素质笔试参考题库含详细答案解析
- 2026年安徽电子信息职业技术学院单招综合素质考试参考题库含详细答案解析
- 2026年云南经济管理学院单招综合素质考试模拟试题含详细答案解析
- 2026年郑州旅游职业学院高职单招职业适应性测试模拟试题及答案详细解析
- 2026年内蒙古体育职业学院单招职业技能考试备考试题含详细答案解析
- 2026年山西林业职业技术学院单招综合素质笔试模拟试题含详细答案解析
- 2026年乌海职业技术学院单招综合素质考试备考试题含详细答案解析
- 2026年河南应用技术职业学院高职单招职业适应性测试备考题库及答案详细解析
- 2026广西百色市公开遴选公务员17人备考考试试题及答案解析
- DL-T 5861-2023 电化学储能电站初步设计内容深度规定
- 高中体育教师期末教学工作汇报
- 别克英朗说明书
- 地下管线测绘课件
- 珍稀植物移栽方案
- 新人教版数学三年级下册预习学案(全册)
- JJG 810-1993波长色散X射线荧光光谱仪
- GB/T 34336-2017纳米孔气凝胶复合绝热制品
- GB/T 20077-2006一次性托盘
- GB/T 1335.3-2009服装号型儿童
- GB/T 10046-2008银钎料
评论
0/150
提交评论