收藏 分销(赏)

一个完整android音乐播放器源码.doc

上传人:xrp****65 文档编号:7037074 上传时间:2024-12-25 格式:DOC 页数:25 大小:105.07KB
下载 相关 举报
一个完整android音乐播放器源码.doc_第1页
第1页 / 共25页
一个完整android音乐播放器源码.doc_第2页
第2页 / 共25页
点击查看更多>>
资源描述
本文出自: 欢迎转载,转载请注明出自: 安卓开发网 下面将和大家一起介绍一个音乐播放器项目,完成后的播放器具有暂停,下一首,前一首,歌曲列表,播放条进度等一些基本功能,它只是播放SDCARD上的.map文件, Android SDK为我们提供了一个MeidaPlayer类,有了这个类我们可以很方便的创建一个mdeiaplayer服务,该类中具有一些方法: MediaPlayer mp = new MediaPlayer(); // 设置文件存储路径 setDataSource("/sdcard/path_to_song"); // 播放 mp.start(); // 暂短 mp.pause(); // 复位 mp.reset(); // 获取当前播放时长 mp.getDuration(); // 进度条 mp.getCurrentDuration(); // Move song to particular second - used for Forward or Backward mp.seekTo(positon); // position in milliseconds // 检测歌曲是否正在播放 mp.isPlaying(); // returns true or false 1:音乐播放器的界面: 播放器界面中所用的一些布局图片,这些图片大家可以到android SDK文件夹下或是到网络中去寻找一些类似就可以,并不一定要这些图片,代码才是我们的关键, 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:drawable="@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_play" 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"?> <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: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_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:endColor="#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/songProgressBar" 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" android: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 xmlns: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" 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" android: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, 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" android: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="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 Footer --> <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"> <!-- 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="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="@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 Button --> <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="@+id/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/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_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" android:thumb="@drawable/seek_handler" 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_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/songCurrentDurationLabel" 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 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"/> </LinearLayout> <!-- 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:id="@+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: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: 有了播放器的播放界面后,我们再将所有歌曲显示的界面实现了,播放器的歌曲列表用的一个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_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="@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="vertical"> <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" /> </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_parent"     android:orientation="vertical"     android:gravity="center"     android:background="@drawable/list_selector"     android:padding="5dp">     <TextView         android:id="@+id/songTitle"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:textSize="16dp"         android:padding="10dp"         android:color="#f3f3f3"/> </LinearLayout> 歌曲列表界面图: 7: 到此为止我们在上面已经完成了该项目的所有布局文件,下面我们就来实现SongManager.java类,这个类用于扫描设备中所有.mp3文件, SongsManager.mp3 public 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(){ } /** * Function 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())) { 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 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中扫描到的所有歌曲, PlayListActivity.java package 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.OnItemClickListener; 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>>(); @Override 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 sdcard this.songsList = plm.getPlayList(); // looping through playlist for (int i = 0; i < songsList.size();
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服