MainAvctivity.java
`
package com.example.shinelon.myapplication;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
MusicPlayService.MusicBinder musicBinder;
MusicServiceConnection musicServiceConnection;
EditText songNameEt;
//以下是获取权限的
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
“android.permission.READ_EXTERNAL_STORAGE”,
“android.permission.WRITE_EXTERNAL_STORAGE” };
//以上是获取权限的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
songNameEt = (EditText)findViewById(R.id.editText);
bindService();
verifyStoragePermissions(this);
//以上是获取权限的
}
//以下是获取权限的
public static void verifyStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
}
//以上是获取权限的
public void bindService(){
if(musicServiceConnection == null){
musicServiceConnection = new MusicServiceConnection();
}
Intent intent = new Intent(this,MusicPlayService.class);
bindService(intent, musicServiceConnection, BIND_AUTO_CREATE);
}
public void unbindService(){
if(musicServiceConnection != null){
unbindService(musicServiceConnection);
musicServiceConnection = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService();
}
public void control_play(View view) throws IOException {
if(musicBinder.call_getMediaPlayer().isPlaying()){
musicBinder.call_pause();
}
else if (!musicBinder.call_getMediaPlayer().isPlaying()){
musicBinder.call_play();
}
}
public void nextSong(View view) throws IOException {
musicBinder.call_playNext();
String musicName = musicBinder.call_getCurrMusicName();
songNameEt.setText(musicName);
}
public void lastSong(View view) throws IOException {
musicBinder.call_playPrevious();
String musicName = musicBinder.call_getCurrMusicName();
songNameEt.setText(musicName);
}
class MusicServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
musicBinder = (MusicPlayService.MusicBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
`
MusicPlayService.java
package com.example.shinelon.myapplication;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
public class MusicPlayService extends Service {
MediaPlayer mediaPlayer;
//以下验证是否能读取到SD卡文件
SongList songList;
String currMusicName = "";
int curIndex = 0;
public MusicPlayService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.v("MusicService", "音乐服务已经绑定");
return new MusicBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.v("MusicService", "音乐服务已经解除绑定");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.v("MusicService", "音乐服务已经被创建");
//以下验证是否能读取到SD卡文件
songList = new SongList();
Log.v("MusiceService", "这个列表下的所有歌曲" + songList.getMusicList().get(0).toString());
// mediaPlayer = MediaPlayer.create(this,R.raw.a);
mediaPlayer = new MediaPlayer();
}
class MusicBinder extends Binder{
public void call_play() throws IOException {
playMusic(curIndex);
}
public void call_playNext() throws IOException {
playNext();
}
public void call_playPrevious() throws IOException {
playPrevious();
}
public String call_getCurrMusicName(){
return getCurrMusicName();
}
public void call_pause(){
pause();
}
public MediaPlayer call_getMediaPlayer(){
return getMediaPlayer();
}
}
public void playMusic(int musicPo) throws IOException {
/*if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
Log.v("MusicService","音乐已经开始播放");//代表的就是音乐控制播放的相关代码*/
Map<String,Object> currMusic = songList.getMusicList().get(musicPo);
String musicUrl = (String)currMusic.get("musicAbPath");
currMusicName = (String)currMusic.get("musicName");
Log.v("Music","----音乐路径--"+musicUrl);
Log.v("Music","----音乐名称--"+currMusicName);
mediaPlayer.reset();
mediaPlayer.setDataSource(musicUrl);
mediaPlayer.prepare();
mediaPlayer.start();
curIndex = musicPo;
}
public void playNext( ) throws IOException {
int newIndex = 0;
newIndex = curIndex + 1;
if (newIndex>songList.getMusicList().size()-1){
newIndex = 0;
}
playMusic(newIndex);
curIndex = newIndex;
}
public void playPrevious() throws IOException {
int newIndex = 0;
newIndex = curIndex - 1;
if (newIndex<0){
newIndex = songList.getMusicList().size()-1;
}
playMusic(newIndex);
curIndex = newIndex;
}
public void pause(){
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
Log.v("MusicService","音乐已经开始暂停");//控制音乐暂停的相关代码
}
public String getCurrMusicName(){
return currMusicName;
}
public MediaPlayer getMediaPlayer(){
return mediaPlayer;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="music/a.map3"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/musicSeekBar"
android:padding="10dp"
android:indeterminate="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一首"
android:id="@+id/btn_before"
android:onClick="lastSong" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放/暂停"
android:id="@+id/btn_play"
android:onClick="control_play" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一首"
android:id="@+id/btn_next"
android:onClick="nextSong" />
</LinearLayout>
</LinearLayout>
本人刚接触Android不久 ,很多地方不懂,我也参考了很多资料,可是都不太明白,也尝试了很久都没有出结果,恳求各位大佬以上面代码为例,写一下怎么用进度条实现控制歌曲进度,在此,说声谢谢。