package com.example.x.music;
import android.os.Bundle;
import android.provider.MediaStore;
/**
* Created by X on 2016/7/4.
*/
public class Audio {
private String mTitle,
mTitleKey,
mArtist,
mArtistKey,
mComposer,
mAlbum,
mAlbumKey,
mDisplayName,
mMimeType,
mPath;
private int mId,
mArtistId,
mAlbumId,
mYear,
mTrack;
private int mDuration = 0 ,
mSize = 0 ;
private boolean isRingtone = false ,
isPodcast = false ,
isAlarm = false ,
isMusic = false ,
isNotification = false ;
public Audio (Bundle bundle) {
mId = bundle.getInt(MediaStore.Audio.Media._ID);
mTitle = bundle.getString(MediaStore.Audio.Media.TITLE);
mTitleKey = bundle.getString(MediaStore.Audio.Media.TITLE_KEY);
mArtist = bundle.getString(MediaStore.Audio.Media.ARTIST);
mArtistKey = bundle.getString(MediaStore.Audio.Media.ARTIST_KEY);
mComposer = bundle.getString(MediaStore.Audio.Media.COMPOSER);
mAlbum = bundle.getString(MediaStore.Audio.Media.ALBUM);
mAlbumKey = bundle.getString(MediaStore.Audio.Media.ALBUM_KEY);
mDisplayName = bundle.getString(MediaStore.Audio.Media.DISPLAY_NAME);
mYear = bundle.getInt(MediaStore.Audio.Media.YEAR);
mMimeType = bundle.getString(MediaStore.Audio.Media.MIME_TYPE);
mPath = bundle.getString(MediaStore.Audio.Media.DATA);
mArtistId = bundle.getInt(MediaStore.Audio.Media.ARTIST_ID);
mAlbumId = bundle.getInt(MediaStore.Audio.Media.ALBUM_ID);
mTrack = bundle.getInt(MediaStore.Audio.Media.TRACK);
mDuration = bundle.getInt(MediaStore.Audio.Media.DURATION);
mSize = bundle.getInt(MediaStore.Audio.Media.SIZE);
isRingtone = bundle.getInt(MediaStore.Audio.Media.IS_RINGTONE) == 1 ;
isPodcast = bundle.getInt(MediaStore.Audio.Media.IS_PODCAST) == 1 ;
isAlarm = bundle.getInt(MediaStore.Audio.Media.IS_ALARM) == 1 ;
isMusic = bundle.getInt(MediaStore.Audio.Media.IS_MUSIC) == 1 ;
isNotification = bundle.getInt(MediaStore.Audio.Media.IS_NOTIFICATION) == 1 ;
}
public int getId () {
return mId;
}
public String getMimeType () {
return mMimeType;
}
public int getDuration () {
return mDuration;
}
public int getSize () {
return mSize;
}
public boolean isRingtone () {
return isRingtone;
}
public boolean isPodcast () {
return isPodcast;
}
public boolean isAlarm () {
return isAlarm;
}
public boolean isMusic () {
return isMusic;
}
public boolean isNotification () {
return isNotification;
}
public String getTitle () {
return mTitle;
}
public String getTitleKey () {
return mTitleKey;
}
public String getArtist () {
return mArtist;
}
public int getArtistId () {
return mArtistId;
}
public String getArtistKey () {
return mArtistKey;
}
public String getComposer () {
return mComposer;
}
public String getAlbum () {
return mAlbum;
}
public int getAlbumId () {
return mAlbumId;
}
public String getAlbumKey () {
return mAlbumKey;
}
public String getDisplayName () {
return mDisplayName;
}
public int getYear () {
return mYear;
}
public int getTrack () {
return mTrack;
}
public String getPath () {
return mPath;
}
}
package com .example .x .music
import android.content .ContentResolver
import android.content .Context
import android.database .Cursor
import android.os .Bundle
import android.provider .MediaStore
import java.util .ArrayList
import java.util .List
public class MediaUtils {
public static final String[] AUDIO_KEYS = new String[]{
MediaStore.Audio .Media ._ID,
MediaStore.Audio .Media .TITLE ,
MediaStore.Audio .Media .TITLE _KEY,
MediaStore.Audio .Media .ARTIST ,
MediaStore.Audio .Media .ARTIST _ID,
MediaStore.Audio .Media .ARTIST _KEY,
MediaStore.Audio .Media .COMPOSER ,
MediaStore.Audio .Media .ALBUM ,
MediaStore.Audio .Media .ALBUM _ID,
MediaStore.Audio .Media .ALBUM _KEY,
MediaStore.Audio .Media .DISPLAY _NAME,
MediaStore.Audio .Media .DURATION ,
MediaStore.Audio .Media .SIZE ,
MediaStore.Audio .Media .YEAR ,
MediaStore.Audio .Media .TRACK ,
MediaStore.Audio .Media .IS _RINGTONE,
MediaStore.Audio .Media .IS _PODCAST,
MediaStore.Audio .Media .IS _ALARM,
MediaStore.Audio .Media .IS _MUSIC,
MediaStore.Audio .Media .IS _NOTIFICATION,
MediaStore.Audio .Media .MIME _TYPE,
MediaStore.Audio .Media .DATA
}
public static List<Audio> getAudioList(Context context) {
List<Audio> audioList = new ArrayList<Audio>()
ContentResolver resolver = context.getContentResolver ()
Cursor cursor = resolver.query (
MediaStore.Audio .Media .EXTERNAL _CONTENT_URI,
AUDIO_KEYS,
null,
null,
null)
for (cursor.moveToFirst ()
Bundle bundle = new Bundle ()
for (int i = 0
final String key = AUDIO_KEYS[i]
final int columnIndex = cursor.getColumnIndex (key)
final int type = cursor.getType (columnIndex)
switch (type) {
case Cursor.FIELD _TYPE_BLOB:
break
case Cursor.FIELD _TYPE_FLOAT:
float floatValue = cursor.getFloat (columnIndex)
bundle.putFloat (key, floatValue)
break
case Cursor.FIELD _TYPE_INTEGER:
int intValue = cursor.getInt (columnIndex)
bundle.putInt (key, intValue)
break
case Cursor.FIELD _TYPE_NULL:
break
case Cursor.FIELD _TYPE_STRING:
String strValue = cursor.getString (columnIndex)
bundle.putString (key, strValue)
break
}
}
Audio audio = new Audio (bundle)
audioList.add (audio)
}
cursor.close ()
return audioList
}
}
package com.example.x.first;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.example.x.first.com.x.Bean.Audio;
import com.example.x.first.com.x.tools.MediaUtils;
import com.example.x.first.com.x.tools.MyTools;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by X on 2016/7/4.
*/
public class MP3Player extends Activity implements View .OnClickListener , MediaPlayer .OnCompletionListener , SeekBar .OnSeekBarChangeListener {
ImageButton ibReturn;
ImageButton ibVolume_down;
ImageButton ibVolume_up;
ImageButton ibBefore;
ImageButton ibStart;
ImageButton ibNext;
ImageButton ibLoop_one;
ImageButton ibLoop_all;
TextView tvMusicTitle;
SeekBar sbMusic;
ListView lvMusic;
List<Audio> listMusic;
List<String> listMusicTitle;
List<Map<String, String>> listMusicTitleMap;
MediaPlayer mp;
private boolean isStartMusic = false ;
private boolean isPause = false ;
private int POSITION = 0 ;
private int CurrentPositon;
private boolean isChanging = false ;
private boolean isLoop_one = false ;
private boolean isFirstStart = true ;
private boolean isBreakByCall = false ;
AudioManager mAudioManager;
int maxVolume;
int currentVolume;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.mp3player);
initView();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
getMusicVolume();
mp = new MediaPlayer();
mp.setOnCompletionListener(this );
getMusicList();
setAdapter();
}
/**
* 获取当前音量
*/
private void getMusicVolume () {
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
System.out.println("maxVolume---->" + maxVolume);
System.out.println("currentVolume---->" + currentVolume);
}
/**
* 增大音量
*/
private void upMusicVolume () {
if (maxVolume == currentVolume) {
Toast.makeText(this , "已是最大音量" , Toast.LENGTH_SHORT).show();
} else {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
AudioManager.FX_FOCUS_NAVIGATION_UP);
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
System.out.println("currentVolume---->" + currentVolume);
System.out.println("maxVolume---->" + maxVolume);
}
}
/**
* 减小音量
*/
private void downMusicVolume () {
if (0 == currentVolume) {
Toast.makeText(this , "已是最小音量" , Toast.LENGTH_SHORT).show();
} else {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
AudioManager.FX_FOCUS_NAVIGATION_UP);
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
System.out.println("currentVolume---->" + currentVolume);
System.out.println("maxVolume---->" + maxVolume);
}
}
private void setAdapter () {
SimpleAdapter simpleAdapter = new SimpleAdapter(this , listMusicTitleMap, R.layout.musiclist,
new String[]{"title" },
new int []{R.id.musicTitle});
lvMusic.setAdapter(simpleAdapter);
lvMusic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
System.out.println("position---->" + position);
POSITION = position;
startMusic();
}
});
}
/**
* 开始播放
*/
private void startMusic () {
try {
String path = listMusic.get(POSITION).getPath();
String title = listMusic.get(POSITION).getTitle();
System.out.println("path---->" + path);
System.out.println("title---->" + title);
mp.reset();
mp.setDataSource(path);
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared (MediaPlayer mp) {
mp.start();
ibStart.setBackground(getResources().getDrawable(R.drawable.music_pause));
tvMusicTitle.setText(listMusic.get(POSITION).getTitle());
isStartMusic = true ;
isFirstStart = false ;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取音乐列表
*/
private void getMusicList () {
listMusic = MediaUtils.getAudioList(this );
listMusicTitleMap = new ArrayList<>();
Map map;
for (Audio audio : listMusic) {
map = new HashMap();
map.put("title" , audio.getTitle());
listMusicTitleMap.add(map);
System.out.println("--------------------------------------------" );
System.out.println("getDisplayName---->" + audio.getDisplayName());
System.out.println("getId---->" + audio.getId());
System.out.println("getAlbum---->" + audio.getAlbum());
System.out.println("getAlbumKey---->" + audio.getAlbumKey());
System.out.println("getArtist---->" + audio.getArtist());
System.out.println("getArtistKey---->" + audio.getArtistKey());
System.out.println("getPath---->" + audio.getPath());
System.out.println("getTitle---->" + audio.getTitle());
System.out.println("getSize---->" + audio.getSize());
System.out.println("getDuration---->" + audio.getDuration());
System.out.println("getYear---->" + audio.getYear());
System.out.println("getComposer---->" + audio.getComposer());
}
}
/**
* 初始化
*/
private void initView () {
ibReturn = (ImageButton) findViewById(R.id.ibReturn);
ibVolume_down = (ImageButton) findViewById(R.id.ibVolume_down);
ibVolume_up = (ImageButton) findViewById(R.id.ibVolume_up);
ibBefore = (ImageButton) findViewById(R.id.ibBefore);
ibStart = (ImageButton) findViewById(R.id.ibStart);
ibNext = (ImageButton) findViewById(R.id.ibNext);
ibLoop_one = (ImageButton) findViewById(R.id.ibLoop_one);
ibLoop_all = (ImageButton) findViewById(R.id.ibLoop_all);
tvMusicTitle = (TextView) findViewById(R.id.tvMusicTitle);
sbMusic = (SeekBar) findViewById(R.id.sbMusic);
lvMusic = (ListView) findViewById(R.id.lvMusic);
ibReturn.setOnClickListener(this );
ibVolume_down.setOnClickListener(this );
ibVolume_up.setOnClickListener(this );
ibBefore.setOnClickListener(this );
ibStart.setOnClickListener(this );
ibNext.setOnClickListener(this );
ibLoop_one.setOnClickListener(this );
ibLoop_all.setOnClickListener(this );
sbMusic.setOnSeekBarChangeListener(this );
}
/**
* 设置监听
*
* @param v
*/
@Override
public void onClick (View v) {
switch (v.getId()) {
case R.id.ibReturn:
finish();
break ;
case R.id.ibStart:
if (listMusic.size() > 0 ) {
changeState();
} else {
MyTools.showToast(this , "本地无音乐" );
}
break ;
case R.id.ibLoop_one:
isLoop_one = true ;
Toast.makeText(this , "单曲循环" , Toast.LENGTH_SHORT).show();
break ;
case R.id.ibLoop_all:
isLoop_one = false ;
Toast.makeText(this , "全部循环" , Toast.LENGTH_SHORT).show();
break ;
case R.id.ibBefore:
if (listMusic.size() > 0 ) {
beforePosition();
startMusic();
} else {
MyTools.showToast(this , "本地无音乐" );
}
break ;
case R.id.ibNext:
if (listMusic.size() > 0 ) {
NextPosition();
startMusic();
} else {
MyTools.showToast(this , "本地无音乐" );
}
break ;
case R.id.ibVolume_down:
downMusicVolume();
break ;
case R.id.ibVolume_up:
upMusicVolume();
break ;
}
}
/**
* 播放或暂停播放
*/
private void changeState () {
if (mp != null ) {
if (!isFirstStart) {
if (isStartMusic) {
mp.pause();
System.out.println("暂停播放" );
ibStart.setBackground(getResources().getDrawable(R.drawable.music_start));
isStartMusic = false ;
} else {
System.out.println("继续播放" );
ibStart.setBackground(getResources().getDrawable(R.drawable.music_pause));
mp.start();
isStartMusic = true ;
}
} else {
startMusic();
ibStart.setBackground(getResources().getDrawable(R.drawable.music_pause));
}
}
}
/**
* 播放完毕操作
*/
@Override
public void onCompletion (MediaPlayer mp) {
if (isLoop_one) {
startMusic();
} else {
NextPosition();
startMusic();
}
}
/**
* 下一首
*/
private void NextPosition () {
int i = listMusic.size();
if (POSITION < i - 1 ) {
POSITION++;
} else {
POSITION = 0 ;
}
}
/**
* 上一首
*/
private void beforePosition () {
if (POSITION != 0 ) {
POSITION--;
} else {
Toast.makeText(this , "已经是第一首" , Toast.LENGTH_SHORT).show();
}
}
/**
* 播放进度条
*/
@Override
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
mp.seekTo(progress);
}
@Override
public void onStartTrackingTouch (SeekBar seekBar) {
isChanging = true ;
}
@Override
public void onStopTrackingTouch (SeekBar seekBar) {
isChanging = false ;
}
@Override
protected void onStop () {
super .onStop();
if (mp != null ) {
if (mp.isPlaying()) {
mp.pause();
isBreakByCall = true ;
}
}
}
@Override
protected void onRestart () {
super .onRestart();
if (isBreakByCall) {
mp.start();
}
}
@Override
protected void onDestroy () {
if (mp != null ) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
}
super .onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
android:orientation ="vertical"
android:background ="@drawable/main" >
<LinearLayout
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
<RelativeLayout
android:id ="@+id/textView"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_alignParentStart ="true"
android:layout_alignParentTop ="true"
android:background ="@color/gray"
android:textColor ="@color/white"
android:gravity ="center" >
<TextView
android:id ="@+id/timingIcon"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_alignParentEnd ="true"
android:layout_alignParentStart ="true"
android:layout_alignParentTop ="true"
android:background ="@color/gray"
android:gravity ="center"
android:text ="MP3播放"
android:textColor ="@color/white"
android:textSize ="50px" />
<ImageButton
android:id ="@+id/ibReturn"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:layout_alignParentRight ="true"
android:layout_alignTop ="@+id/timingIcon"
android:background ="@drawable/returnbutton" />
</RelativeLayout >
</LinearLayout >
<LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="match_parent"
android:background ="@drawable/main"
android:orientation ="horizontal" >
<LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="match_parent"
android:orientation ="vertical" >
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_marginTop ="20dp"
android:layout_marginLeft ="20dp"
android:text ="播放列表"
android:textColor ="@color/white"
android:textSize ="40px" />
<ListView
android:id ="@+id/lvMusic"
android:layout_width ="180dp"
android:layout_height ="200dp"
android:layout_marginLeft ="20dp"
> </ListView >
</LinearLayout >
<LinearLayout
android:layout_marginRight ="20dp"
android:layout_marginTop ="20dp"
android:layout_width ="wrap_content"
android:layout_height ="match_parent"
android:orientation ="vertical"
android:layout_weight ="3" >
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="正在播放"
android:textColor ="@color/white"
android:textSize ="30px"
/>
<TextView
android:id ="@+id/tvMusicTitle"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="歌曲名1"
android:textSize ="40px"
android:textColor ="@color/blue"
android:layout_marginTop ="20dp"
android:layout_gravity ="center"
/>
<SeekBar
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="30dp"
android:id ="@+id/sbMusic"
android:layout_gravity ="center_horizontal" />
<LinearLayout
android:layout_marginTop ="30dp"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
<ImageButton
android:id ="@+id/ibVolume_down"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_volume_down"
android:layout_weight ="1" />
<ImageButton
android:id ="@+id/ibVolume_up"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_volume_up"
android:layout_weight ="1" />
<ImageButton
android:id ="@+id/ibBefore"
android:layout_marginLeft ="10dp"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_before"
android:layout_weight ="1" />
<ImageButton
android:id ="@+id/ibStart"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_start"
android:layout_weight ="1" />
<ImageButton
android:id ="@+id/ibNext"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_next"
android:layout_weight ="1" />
<ImageButton
android:id ="@+id/ibLoop_one"
android:layout_marginLeft ="10dp"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_loop_one"
android:layout_weight ="1" />
<ImageButton
android:id ="@+id/ibLoop_all"
android:layout_width ="40dp"
android:layout_height ="40dp"
android:background ="@drawable/music_loop_all"
android:layout_weight ="1" />
</LinearLayout >
</LinearLayout >
</LinearLayout >
</LinearLayout >
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<TextView
android:id ="@+id/musicTitle"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="3dp"
android:layout_marginBottom ="3dp"
android:textColor ="@color/white"
android:text ="aa"
android:textSize ="25px" />
</LinearLayout >
<uses-permission android:name ="android.permission.READ_EXTERNAL_STORAGE" />