文档库 最新最全的文档下载
当前位置:文档库 › 一个完整android音乐播放器源码

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

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

本文出自:

https://www.wendangku.net/doc/7511557117.html,/forum.php?mod=viewthread&tid=350&page=1&extra=#pid422

欢迎转载,转载请注明出自:https://www.wendangku.net/doc/7511557117.html,安卓开发网

下面https://www.wendangku.net/doc/7511557117.html,将和大家一起介绍一个音乐播放器项目,完成后的播放器具有暂停,下一首,前一首,歌曲列表,播放条进度等一些基本功能,它只是播放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

android:state_focused="true"

android:state_pressed="true" />

android:state_focused="false"

android:state_pressed="true" />

android:state_focused="true" />

android:state_focused="false"

android:state_pressed="false" />

提示:大家可以根据此XML文件完成其它的一些按钮布局文件,https://www.wendangku.net/doc/7511557117.html,d 在这里就没有再为大家一一提供了,

4:

为了让播放器更绚,我们可以自定义一个SeekBar作为歌曲的播放进度,自定义风格代如下:

(4.1)更换背景图片:

seekbar_progress_bg.xml

android:src="@drawable/img_seekbar_progress_blue"

android:tileMode="repeat"

android:antialias="true"

android:dither="false"

android:filter="false"

android:gravity="left"

/>

(4.2)Changing SeekBar Progress:

seekbar_progress.xml

android:drawable="@drawable/img_seekbar_bg"

android:dither="true">

android:startColor="#80028ac8"

android:centerColor="#80127fb1"

android:centerY="0.75"

android:endColor="#a004638f"

android:angle="270"

/>

android:id="@android:id/progress"

android:drawable="@drawable/seekbar_progress_bg"

/>

(4.3)实际的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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/player_background">

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">

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"/>

android:id="@+id/btnPlaylist"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:src="@drawable/btn_playlist"

android:background="@null"/>

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">

android:layout_height="wrap_content"

android:src="@drawable/adele"/>

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">

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">

android:id="@+id/btnPrevious"

android:src="@drawable/btn_previous"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"/>

android:id="@+id/btnBackward"

android:src="@drawable/btn_backward"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"/>

android:id="@+id/btnPlay"

android:src="@drawable/btn_play"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"/>

android:id="@+id/btnForward"

android:src="@drawable/btn_forward"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"/>

android:id="@+id/btnNext"

android:src="@drawable/btn_next"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"/>

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"/>

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">

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"/>

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"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_above="@id/timerDisplay"

android:gravity="center">

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"/>

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"/>

6:

有了播放器的播放界面后,我们再将所有歌曲显示的界面实现了,播放器的歌曲列表用的一个listview控件,同样我们也为该listview控件自定义,适合播放的整体界面风格,list_selector.xml

android:state_selected="false"

android:state_pressed="false"

android:drawable="@drawable/gradient_bg" />

android:drawable="@drawable/gradient_bg_hover" />

android:state_pressed="false"

android:drawable="@drawable/gradient_bg_hover" />

歌曲列表界面布局文件:

playlist.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

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" />

listviewr的item界面布局,用于定义单个歌曲的显示界面:

playlist_item.xml

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center"

android:background="@drawable/list_selector"

android:padding="5dp">

android:id="@+id/songTitle"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="16dp"

android:padding="10dp"

android:color="#f3f3f3"/>

歌曲列表界面图:

7:

到此为止我们在上面已经完成了该项目的所有布局文件,下面我们就来实现SongManager.java类,这个类用于扫描设备中所有.mp3文件,

SongsManager.mp3

public class SongsManager {

// SDCard Path

final String MEDIA_PATH = new String("/sdcard/");

private ArrayList> songsList = new ArrayList>();

// Constructor

public SongsManager(){

}

/**

* Function to read all mp3 files from sdcard

* and store the details in ArrayList

* */

public ArrayList> getPlayList(){

File home = new File(MEDIA_PATH);

if (home.listFiles(new FileExtensionFilter()).length > 0) {

for (File file : home.listFiles(new FileExtensionFilter())) {

HashMap song = new HashMap();

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> songsList = new ArrayList>();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(https://www.wendangku.net/doc/7511557117.html,yout.playlist);

ArrayList> songsListData = new ArrayList>();

SongsManager plm = new SongsManager();

// get all songs from sdcard

this.songsList = plm.getPlayList();

// looping through playlist

for (int i = 0; i < songsList.size(); i++) {

// creating new HashMap

HashMap song = songsList.get(i);

// adding HashList to ArrayList

songsListData.add(song);

}

// Adding menuItems to ListView

ListAdapter adapter = new SimpleAdapter(this, songsListData,

https://www.wendangku.net/doc/7511557117.html,yout.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() {

@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

in.putExtra("songIndex", songIndex);

setResult(100, in);

// Closing PlayListView

finish();

}

});

}

}

9:下面我们再来实现播放的主类AndroidBuildingMusicPlayerActivity.java:

该类主要实现功能如下:

1:在歌曲列表界面点击一首歌曲进入播放界面;

2:播放,暂停,快进,复位等功能实现

3:重复播放单一歌曲,随机播放等

该类的完整代码如下:

public class AndroidBuildingMusicPlayerActivity extends Activity implements OnCompletionListener, SeekBar.OnSeekBarChangeListener {

private ImageButton btnPlay;

private ImageButton btnForward;

private ImageButton btnBackward;

private ImageButton btnNext;

private ImageButton btnPrevious;

private ImageButton btnPlaylist;

private ImageButton btnRepeat;

private ImageButton btnShuffle;

private SeekBar songProgressBar;

private TextView songTitleLabel;

private TextView songCurrentDurationLabel;

private TextView songTotalDurationLabel;

// Media Player

private MediaPlayer mp;

// Handler to update UI timer, progress bar etc,.

private Handler mHandler = new Handler();;

private SongsManager songManager;

private Utilities utils;

private int seekForwardTime = 5000; // 5000 milliseconds

private int seekBackwardTime = 5000; // 5000 milliseconds

private int currentSongIndex = 0;

private boolean isShuffle = false;

private boolean isRepeat = false;

private ArrayList> songsList = new ArrayList>();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(https://www.wendangku.net/doc/7511557117.html,yout.player);

// 所有的播放按钮

btnPlay = (ImageButton) findViewById(R.id.btnPlay);

btnForward = (ImageButton) findViewById(R.id.btnForward);

btnBackward = (ImageButton) findViewById(R.id.btnBackward);

btnNext = (ImageButton) findViewById(R.id.btnNext);

btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);

btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);

btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);

btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);

songProgressBar = (SeekBar)

findViewById(R.id.songProgressBar);

songTitleLabel = (TextView) findViewById(R.id.songTitle);

songCurrentDurationLabel = (TextView)

findViewById(R.id.songCurrentDurationLabel);

songTotalDurationLabel = (TextView)

findViewById(R.id.songTotalDurationLabel);

// Mediaplayer

mp = new MediaPlayer();

songManager = new SongsManager();

utils = new Utilities();

// Listeners

songProgressBar.setOnSeekBarChangeListener(this); // Important mp.setOnCompletionListener(this); // Important

// 获取所有歌曲列表

songsList = songManager.getPlayList();

// 默认从第一首开始播放

playSong(0);

/**

* Play button click event

* plays a song and changes button to pause image

* pauses a song and changes button to play image

* */

btnPlay.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// check for already playing

if(mp.isPlaying()){

if(mp!=null){

mp.pause();

// Changing button image to play button

btnPlay.setImageResource(R.drawable.btn_play) ;

}

}else{

// Resume song

if(mp!=null){

mp.start();

// Changing button image to pause button

btnPlay.setImageResource(R.drawable.btn_pause );

}

}

}

});

/**

* Forward button click event

* Forwards song specified seconds

* */

btnForward.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// get current song position

int currentPosition = mp.getCurrentPosition();

// check if seekForward time is lesser than song duration if(currentPosition + seekForwardTime <=

mp.getDuration()){

// forward song

mp.seekTo(currentPosition + seekForwardTime);

}else{

// forward to end position

mp.seekTo(mp.getDuration());

}

}

});

/**

* Backward button click event

* Backward song to specified seconds

* */

btnBackward.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// get current song position

int currentPosition = mp.getCurrentPosition();

// check if seekBackward time is greater than 0 sec if(currentPosition - seekBackwardTime >= 0){

// forward song

mp.seekTo(currentPosition - seekBackwardTime); }else{

// backward to starting position

mp.seekTo(0);

}

}

});

/**

* Next button click event

*点击后一首歌事件

* */

btnNext.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// check if next song is there or not

if(currentSongIndex < (songsList.size() - 1)){

playSong(currentSongIndex + 1);

currentSongIndex = currentSongIndex + 1;

}else{

// play first song

playSong(0);

currentSongIndex = 0;

}

}

});

/**

* 单击:前一首按钮事件

* */

btnPrevious.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

if(currentSongIndex > 0){

playSong(currentSongIndex - 1);

currentSongIndex = currentSongIndex - 1;

}else{

// play last song

playSong(songsList.size() - 1);

currentSongIndex = songsList.size() - 1;

}

}

});

/**

*循环播放按钮事件

* */

btnRepeat.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

if(isRepeat){

isRepeat = false;

Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();

btnRepeat.setImageResource(R.drawable.btn_repeat) ;

}else{

// make repeat to true

isRepeat = true;

Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();

// make shuffle to false

isShuffle = false;

btnRepeat.setImageResource(R.drawable.btn_repeat_ focused);

btnShuffle.setImageResource(R.drawable.btn_shuffl e);

}

}

});

/**

* 随机播放按钮事件

* */

btnShuffle.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

if(isShuffle){

isShuffle = false;

Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();

btnShuffle.setImageResource(R.drawable.btn_shuffl e);

}else{

// make repeat to true

isShuffle= true;

Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();

// make shuffle to false

isRepeat = false;

btnShuffle.setImageResource(R.drawable.btn_shuffl e_focused);

btnRepeat.setImageResource(R.drawable.btn_repeat) ;

}

}

});

/**

* Button Click event for Play list click event

* Launches list activity which displays list of songs

* */

btnPlaylist.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Intent i = new Intent(getApplicationContext(), PlayListActivity.class);

startActivityForResult(i, 100);

}

});

}

/**

* Receiving song index from playlist view

* and play the song

* */

@Override

protected void onActivityResult(int requestCode,

int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

if(resultCode == 100){

currentSongIndex = data.getExtras().getInt("songIndex"); // play selected song

playSong(currentSongIndex);

}

}

/**

* Function to play a song

* @param songIndex - index of song

* */

public void playSong(int songIndex){

// Play song

try {

mp.reset();

mp.setDataSource(songsList.get(songIndex).get("songPath") );

mp.prepare();

mp.start();

// Displaying Song title

String songTitle =

songsList.get(songIndex).get("songTitle");

songTitleLabel.setText(songTitle);

// Changing Button Image to pause image

btnPlay.setImageResource(R.drawable.btn_pause);

// set Progress bar values

songProgressBar.setProgress(0);

基于Android的音乐播放器设计与实现

基于Android的音乐播放器设计与实现

摘要 在如今这个生活节奏越来越快的社会,科技也随之发展的越来越智能化。而手机的智能化就是其中体现的一个方面,现在市场上比较主流的手机系统就是Android,苹果和w8。Android是一个开源的系统,它底层是基于Linux的操作系统。 本毕业设计的音乐播放器采用了Android开源系统技术,利用Java语言和EclipseJDK编辑工具对音乐播放器进行编写。同时也给出了比较详细的系统设计过程、部分界面图及主要功能运行流程图,本设计还对一些架构的和界面的高度过程中遇到的问题和解决方法进行了详细的讨论,还有一些开发过程中遇到的错误问题进行了举例。该音乐播放器集播放、暂停、停止、上一首、下一首、歌词显示等功能于一体,有良好的性能,炫酷的播放界面。能在Android 手机系统中能独立运行。该播放器还拥有对手机文件浏览器的访问功能、歌曲播放模式(单曲循环,单曲循环,顺序循环,顺序播放,随机播放)、以及歌词开闭状态等比较人性化的设置.该音乐播放器的名称是:“旋风播放器“,名字就和它的风格一样。 关键词:Linux操作系统;Android;流程图;音乐播放器;开源系统

Abstract In this increasingly fast pace of life society, science and technology also will be the development of more and more intelligent. The intelligent phone which reflects one aspect of the market is now more mainstream phone system is Android, Apple and W8. Android is an open source system, it is the underlying Linux-based operating system. The music player of the graduate design uses the Android open source technology, the use the Java language and EclipseJDK of editing tools to write the music player. But also gives a more detailed system design process, part of the interface map and main functions of a flowchart of the operation, the design of a high degree of process architecture and interface problems encountered and solutions are discussed in detail, as well as some development process errors encountered examples. The music player is set to play, pause, stop, previous one, the next song, lyrics display and other functions in one, good performance, cool player interface. Android mobile phone system can run independently. The player also has access to the phone file browser function, song playback mode (single cycle, single cycle, order cycle, the order of play, random play), as well as the opening and closing lyrics state humane set the music the name of the player: "whirlwind player", the name and its style. Key words: Linux operating system; Android; flowchart; music player; open source system

安卓计算机源代码

Mxl代码

相关文档
相关文档 最新文档