1. 程式人生 > >求助Android進度條在音樂播放器中的使用方法

求助Android進度條在音樂播放器中的使用方法

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不久 ,很多地方不懂,我也參考了很多資料,可是都不太明白,也嘗試了很久都沒有出結果,懇求各位大佬以上面程式碼為例,寫一下怎麼用進度條實現控制歌曲進度,在此,說聲謝謝。