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_getMediaP ().isPlaying()){
musicBinder.call_pause();
}
else if (!musicBinder.call_getMediaP ().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.MediaP ;
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 {
MediaP mediaP ;
//以下验证是否能读取到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());
// mediaP = MediaP .create(this,R.raw.a);
mediaP = new MediaP ();
}
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 MediaP call_getMediaP (){
return getMediaP ();
}
}
public void playMusic(int musicPo) throws IOException {
/*if(!mediaP .isPlaying()){
mediaP .start();
}
Log.v(\"MusicService\",\"音乐已经开始播放\");//代表的就是音乐控制播放的相关代码*/
Map<String, > currMusic = songList.getMusicList().get(musicPo);
String musicUrl = (String)currMusic.get(\"musicAbPath\");
currMusicName = (String)currMusic.get(\"musicName\");
Log.v(\"Music\",\"----音乐路径--\"+musicUrl);
Log.v(\"Music\",\"----音乐名称--\"+currMusicName);
mediaP .reset();
mediaP .setDataSource(musicUrl);
mediaP .prepare();
mediaP .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(mediaP .isPlaying()){
mediaP .pause();
}
Log.v(\"MusicService\",\"音乐已经开始暂停\");//控制音乐暂停的相关代码
}
public String getCurrMusicName(){
return currMusicName;
}
public MediaP getMediaP (){
return mediaP ;
}
}
<? version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout ns: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: =\"lastSong\" />
<Button
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"播放/暂停\"
android:id=\"@+id/btn_play\"
android: =\"control_play\" />
<Button
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"下一首\"
android:id=\"@+id/btn_next\"
android: =\"nextSong\" />
</LinearLayout>
</LinearLayout>
本人刚接触Android不久 ,很多地方不懂,我也参考了很多资料,可是都不太明白,也尝试了很久都没有出结果,恳求各位大佬以上面代码为例,写一下怎么用进度条实现控制歌曲进度,在此,说声谢谢。
继续阅读与本文标签相同的文章
YOYOW——一种基于区块链的崭新的内容激励方法
维基解码(1)
-
200多万市民实现办事“免交证明”,阿里助力晋城数字化升级
2026-05-19栏目: 教程
-
聚游:颠覆传统规则 构筑区块链游戏新生态
2026-05-19栏目: 教程
-
跟并列式人民日报时评学布局谋篇
2026-05-19栏目: 教程
-
阿里开发者技术交流钉钉群汇总【2019】
2026-05-19栏目: 教程
-
9月书讯:别抱怨读书苦,那是你看世界的路
2026-05-19栏目: 教程
