├── .gitignore ├── .idea ├── caches │ ├── build_file_checksums.ser │ └── gradle_models.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.txt ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example4job1 │ │ └── example4job1 │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── drawable │ │ ├── default_cover.png │ │ ├── login.png │ │ └── music_player.png │ ├── java │ │ └── com │ │ │ └── example4job1 │ │ │ └── musicplayer │ │ │ ├── activity │ │ │ ├── ListActivity.java │ │ │ ├── LoginActivity.java │ │ │ ├── PlayActivity.java │ │ │ ├── Song.java │ │ │ └── SongAdapter.java │ │ │ └── service │ │ │ └── MusicService.java │ ├── layout │ │ ├── activity_list.xml │ │ ├── activity_login.xml │ │ ├── activity_play.xml │ │ ├── content_play.xml │ │ └── song_item.xml │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ ├── res │ │ ├── drawable │ │ │ ├── default_cover.png │ │ │ ├── image.png │ │ │ ├── login.png │ │ │ ├── music_player.png │ │ │ ├── play.png │ │ │ ├── sound.png │ │ │ ├── sound2.png │ │ │ └── stop.png │ │ ├── layout │ │ │ ├── activity_list.xml │ │ │ ├── activity_login.xml │ │ │ ├── activity_play.xml │ │ │ └── song_item.xml │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example4job1 │ └── example4job1 │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── 开发文档 ├── Android移动终端开发大作业_黎为楷.doc ├── MusicPlayer.apk └── 运行效果截图.zip /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DIWEIKA/Android_MusicPlayer/bb2e0b6705bd9d3f68300898f040b4f2eb8ea208/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/caches/gradle_models.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DIWEIKA/Android_MusicPlayer/bb2e0b6705bd9d3f68300898f040b4f2eb8ea208/.idea/caches/gradle_models.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | 阅读【开发文档】 -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "25.0.0" 6 | defaultConfig { 7 | applicationId "com.qinposhi.example4job1" 8 | minSdkVersion 24 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation 'com.android.support:design:24.2.1' 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 26 | exclude group: 'com.android.support', module: 'support-annotations' 27 | }) 28 | implementation 'com.android.support:appcompat-v7:24.2.1' 29 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example4job1/example4job1/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.example4job1; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.qinposhi.example4job1", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 14 | 15 | 19 | 20 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/drawable/default_cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DIWEIKA/Android_MusicPlayer/bb2e0b6705bd9d3f68300898f040b4f2eb8ea208/app/src/main/drawable/default_cover.png -------------------------------------------------------------------------------- /app/src/main/drawable/login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DIWEIKA/Android_MusicPlayer/bb2e0b6705bd9d3f68300898f040b4f2eb8ea208/app/src/main/drawable/login.png -------------------------------------------------------------------------------- /app/src/main/drawable/music_player.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DIWEIKA/Android_MusicPlayer/bb2e0b6705bd9d3f68300898f040b4f2eb8ea208/app/src/main/drawable/music_player.png -------------------------------------------------------------------------------- /app/src/main/java/com/example4job1/musicplayer/activity/ListActivity.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.musicplayer.activity; 2 | 3 | import android.Manifest; 4 | import android.content.BroadcastReceiver; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.IntentFilter; 8 | import android.content.pm.PackageManager; 9 | import android.database.Cursor; 10 | import android.os.Bundle; 11 | import android.provider.MediaStore; 12 | import android.support.v4.app.ActivityCompat; 13 | import android.support.v4.content.ContextCompat; 14 | import android.support.v7.app.AppCompatActivity; 15 | import android.view.View; 16 | import android.widget.AdapterView; 17 | import android.widget.Button; 18 | import android.widget.ListView; 19 | import android.widget.TextView; 20 | import android.widget.Toast; 21 | 22 | import com.example4job1.R; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class ListActivity extends AppCompatActivity { 28 | 29 | private IntentFilter intentFilter; 30 | private HeadsetPlugReceiver headsetPlugReceiver; 31 | private List songArray = new ArrayList(); 32 | public String Songpath; 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_list); 37 | 38 | intentFilter = new IntentFilter(); 39 | intentFilter.addAction("android.intent.action.HEADSET_PLUG"); 40 | headsetPlugReceiver = new HeadsetPlugReceiver(); 41 | registerReceiver(headsetPlugReceiver, intentFilter); 42 | TextView welcomeTxt=(TextView)findViewById(R.id.txt_welcome); 43 | Button backBtn=(Button)findViewById(R.id.btn_back); 44 | String uname=this.getIntent().getStringExtra("uname"); 45 | welcomeTxt.setText("欢迎您,"+uname+"!"); 46 | 47 | backBtn.setOnClickListener(new View.OnClickListener() { 48 | @Override 49 | public void onClick(View v) { 50 | Intent intent=new Intent(ListActivity.this, LoginActivity.class); 51 | startActivity(intent); 52 | } 53 | }); 54 | 55 | if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 56 | ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE }, 1); 57 | } else { 58 | initSongs(); 59 | } 60 | 61 | ListView songList=(ListView)findViewById(R.id.list_song); 62 | SongAdapter myAdapter=new SongAdapter(ListActivity.this,R.layout.song_item,songArray); 63 | songList.setAdapter(myAdapter); 64 | songList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 65 | @Override 66 | public void onItemClick(AdapterView parent, View view, int position, long id) { 67 | Song song = songArray.get(position); 68 | Songpath = song.getSongPath(); 69 | Toast.makeText(ListActivity.this, Songpath, Toast.LENGTH_SHORT).show(); 70 | Intent intent=new Intent(ListActivity.this, PlayActivity.class); 71 | intent.putExtra("position",Songpath); 72 | startActivity(intent); 73 | } 74 | }); 75 | 76 | } 77 | 78 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 79 | switch (requestCode) { 80 | case 1: 81 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 82 | initSongs(); 83 | } else { 84 | Toast.makeText(this, "未授权,功能无法实现", Toast.LENGTH_SHORT).show(); 85 | } 86 | break; 87 | default: 88 | } 89 | } 90 | 91 | 92 | public void initSongs() { 93 | 94 | Cursor cursor = null; 95 | cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); 96 | if (cursor != null) { 97 | while (cursor.moveToNext()) { 98 | String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); 99 | String disName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); 100 | String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); 101 | Song song = new Song(disName, artist, url); 102 | songArray.add(song); 103 | } 104 | } 105 | } 106 | 107 | 108 | class HeadsetPlugReceiver extends BroadcastReceiver { 109 | @Override 110 | public void onReceive(Context context, Intent intent) { 111 | if(intent.getAction().equalsIgnoreCase("android.intent.action.HEADSET_PLUG")){ 112 | if(intent.hasExtra("state")){ 113 | if (intent.getIntExtra("state", 0) == 1){ 114 | Toast.makeText(context, "耳机已连接", Toast.LENGTH_SHORT).show(); 115 | }else{ 116 | Toast.makeText(context, "耳机已断开", Toast.LENGTH_SHORT).show(); 117 | } 118 | } 119 | } 120 | } 121 | } 122 | 123 | protected void onDestroy() { 124 | super.onDestroy(); 125 | unregisterReceiver(headsetPlugReceiver); 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/java/com/example4job1/musicplayer/activity/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.musicplayer.activity; 2 | 3 | import android.content.Intent; 4 | import android.content.SharedPreferences; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.CheckBox; 10 | import android.widget.EditText; 11 | import android.widget.Toast; 12 | 13 | import com.example4job1.R; 14 | 15 | public class LoginActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_login); 22 | final EditText unameEdt=(EditText)findViewById(R.id.edt_username); 23 | final EditText pwdEdt=(EditText)findViewById(R.id.edt_password); 24 | SharedPreferences pref = getSharedPreferences("Userinfo",MODE_PRIVATE); 25 | unameEdt.setText(pref.getString("uname","")); 26 | Button quitBtn=(Button)findViewById(R.id.btn_quit); 27 | Button loginBtn=(Button)findViewById(R.id.btn_login); 28 | final CheckBox savenameChk=(CheckBox)findViewById(R.id.chk_savename); 29 | 30 | quitBtn.setOnClickListener(new View.OnClickListener() { 31 | @Override 32 | public void onClick(View v) { 33 | android.os.Process.killProcess(android.os.Process.myPid()); 34 | System.exit(0); 35 | } 36 | }); 37 | 38 | loginBtn.setOnClickListener(new View.OnClickListener() { 39 | @Override 40 | public void onClick(View v) { 41 | if(unameEdt.getText().toString()==null||unameEdt.getText().toString().equals("") || !pwdEdt.getText().toString().equals("123456")){ 42 | Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); 43 | }else{ 44 | SharedPreferences.Editor editor= getSharedPreferences("Userinfo",MODE_PRIVATE).edit(); 45 | if(savenameChk.isChecked() == true){ 46 | editor.putString("uname",unameEdt.getText().toString()); 47 | } 48 | else { 49 | editor.putString("uname",""); 50 | } 51 | editor.apply(); 52 | Intent intent=new Intent(LoginActivity.this,ListActivity.class); 53 | intent.putExtra("uname",unameEdt.getText().toString()); 54 | startActivity(intent); 55 | } 56 | } 57 | }); 58 | 59 | 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/example4job1/musicplayer/activity/PlayActivity.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.musicplayer.activity; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | import android.media.MediaPlayer; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.support.v4.app.ActivityCompat; 11 | import android.support.v4.content.ContextCompat; 12 | import android.support.v7.app.AppCompatActivity; 13 | import android.view.View; 14 | import android.widget.Button; 15 | import android.widget.SeekBar; 16 | import android.widget.TextView; 17 | import android.widget.Toast; 18 | 19 | import com.example4job1.R; 20 | 21 | public class PlayActivity extends AppCompatActivity { 22 | 23 | private MediaPlayer mediaPlayer = new MediaPlayer(); 24 | SeekBar volumeBar; 25 | SeekBar positionBar; 26 | Button playBtn; 27 | int totalTime; 28 | TextView elapsedTimeLabel; 29 | TextView remainingTimeLabel; 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_play); 35 | playBtn = (Button) findViewById(R.id.playBtn); 36 | playBtn.setOnClickListener(new View.OnClickListener() { 37 | @Override 38 | public void onClick(View view) { 39 | if (!mediaPlayer.isPlaying()) { 40 | mediaPlayer.start(); 41 | playBtn.setBackgroundResource(R.drawable.stop); 42 | 43 | } else { 44 | mediaPlayer.pause(); 45 | playBtn.setBackgroundResource(R.drawable.play); 46 | } 47 | } 48 | }); 49 | 50 | if(ContextCompat.checkSelfPermission(PlayActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){ 51 | ActivityCompat.requestPermissions(PlayActivity.this,new String[]{ 52 | Manifest.permission.WRITE_EXTERNAL_STORAGE},1); 53 | } else { 54 | initMediaPlayer(); 55 | } 56 | 57 | elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel); 58 | remainingTimeLabel = (TextView) findViewById(R.id.remainingTimeLabel); 59 | totalTime = mediaPlayer.getDuration(); 60 | 61 | positionBar = (SeekBar) findViewById(R.id.positionBar); 62 | positionBar.setMax(totalTime); 63 | positionBar.setOnSeekBarChangeListener( 64 | new SeekBar.OnSeekBarChangeListener() { 65 | @Override 66 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 67 | if (fromUser) { 68 | mediaPlayer.seekTo(progress); 69 | positionBar.setProgress(progress); 70 | } 71 | } 72 | 73 | @Override 74 | public void onStartTrackingTouch(SeekBar seekBar) { 75 | 76 | } 77 | 78 | @Override 79 | public void onStopTrackingTouch(SeekBar seekBar) { 80 | 81 | } 82 | } 83 | ); 84 | 85 | volumeBar = (SeekBar) findViewById(R.id.volumeBar); 86 | volumeBar.setOnSeekBarChangeListener( 87 | new SeekBar.OnSeekBarChangeListener() { 88 | @Override 89 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 90 | float volumeNum = progress / 100f; 91 | mediaPlayer.setVolume(volumeNum, volumeNum); 92 | } 93 | @Override 94 | public void onStartTrackingTouch(SeekBar seekBar) { 95 | 96 | } 97 | @Override 98 | public void onStopTrackingTouch(SeekBar seekBar) { 99 | 100 | } 101 | } 102 | ); 103 | 104 | new Thread(new Runnable() { 105 | @Override 106 | public void run() { 107 | while (mediaPlayer != null) { 108 | try { 109 | Message msg = new Message(); 110 | if(!mediaPlayer.isPlaying()) break; 111 | msg.what = mediaPlayer.getCurrentPosition(); 112 | handler.sendMessage(msg); 113 | Thread.sleep(1000); 114 | } catch (InterruptedException e) {} 115 | 116 | } 117 | } 118 | }).start(); 119 | 120 | } 121 | 122 | private Handler handler = new Handler() { 123 | @Override 124 | public void handleMessage(Message msg) { 125 | int currentPosition = msg.what; 126 | positionBar.setProgress(currentPosition); 127 | String elapsedTime = createTimeLabel(currentPosition); 128 | elapsedTimeLabel.setText(elapsedTime); 129 | String remainingTime = createTimeLabel(totalTime-currentPosition); 130 | remainingTimeLabel.setText("- " + remainingTime); 131 | } 132 | }; 133 | 134 | public String createTimeLabel(int time) { 135 | String timeLabel = ""; 136 | int min = time / 1000 / 60; 137 | int sec = time / 1000 % 60; 138 | timeLabel = min + ":"; 139 | if (sec < 10) timeLabel += "0"; 140 | timeLabel += sec; 141 | return timeLabel; 142 | } 143 | 144 | public void initMediaPlayer(){ 145 | 146 | Intent intent = getIntent(); 147 | String currentIndex = intent.getStringExtra("position"); 148 | try{ 149 | mediaPlayer.setDataSource(currentIndex); 150 | mediaPlayer.prepare(); 151 | 152 | } catch (Exception e){ 153 | e.printStackTrace(); 154 | } 155 | } 156 | 157 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 158 | switch (requestCode) { 159 | case 1: 160 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 161 | initMediaPlayer(); 162 | } else { 163 | Toast.makeText(this, "未授权,功能无法实现", Toast.LENGTH_SHORT).show(); 164 | finish(); 165 | } 166 | break; 167 | default: 168 | } 169 | 170 | } 171 | 172 | protected void onDestroy(){ 173 | super.onDestroy(); 174 | if (mediaPlayer!=null){ 175 | mediaPlayer.stop(); 176 | mediaPlayer.release(); 177 | } 178 | } 179 | 180 | 181 | } 182 | -------------------------------------------------------------------------------- /app/src/main/java/com/example4job1/musicplayer/activity/Song.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.musicplayer.activity; 2 | 3 | public class Song { 4 | 5 | private String songName; 6 | private String singer; 7 | public String songPath; 8 | 9 | public Song(String songName,String singer,String songPath){ 10 | this.singer=singer; 11 | this.songName=songName; 12 | this.songPath=songPath; 13 | } 14 | 15 | public String getSongName(){return songName;} 16 | public String getSinger(){return singer;} 17 | public String getSongPath(){return songPath;} 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/example4job1/musicplayer/activity/SongAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.musicplayer.activity; 2 | 3 | 4 | import android.content.Context; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | import com.example4job1.R; 13 | 14 | import java.util.List; 15 | 16 | public class SongAdapter extends ArrayAdapter { 17 | private int resourceId; 18 | 19 | public SongAdapter(Context context, int textViewResourceId, 20 | List objects) { 21 | super(context, textViewResourceId, objects); 22 | resourceId = textViewResourceId; 23 | } 24 | 25 | @Override 26 | public View getView(int position, View convertView, ViewGroup parent) { 27 | 28 | Song song = getItem(position); 29 | View view; 30 | ViewHolder viewHolder; 31 | if (convertView == null) { 32 | view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false); 33 | viewHolder = new ViewHolder(); 34 | viewHolder.coverImg = (ImageView) view.findViewById (R.id.img_cover); 35 | viewHolder.songNameTxt = (TextView) view.findViewById (R.id.txt_song_name); 36 | viewHolder.singerTxt = (TextView) view.findViewById (R.id.txt_singer); 37 | view.setTag(viewHolder); 38 | } else { 39 | view = convertView; 40 | viewHolder = (ViewHolder) view.getTag(); 41 | } 42 | 43 | viewHolder.songNameTxt.setText(song.getSongName()); 44 | viewHolder.singerTxt.setText(song.getSinger()); 45 | viewHolder.songPath=song.getSongPath(); 46 | return view; 47 | } 48 | 49 | class ViewHolder { 50 | ImageView coverImg; 51 | TextView songNameTxt; 52 | TextView singerTxt; 53 | String songPath; 54 | } 55 | 56 | } 57 | 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/example4job1/musicplayer/service/MusicService.java: -------------------------------------------------------------------------------- 1 | package com.example4job1.musicplayer.service; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.database.Cursor; 6 | import android.media.MediaPlayer; 7 | import android.os.Binder; 8 | import android.os.IBinder; 9 | import android.provider.MediaStore; 10 | 11 | /*下列代码完成了Service各种状态下所进行的操作,目的是在Service运行时保证MediaPlayer的存在, 12 | 而在service被停止时使MediaPlayer销毁。Service在后台持续运行 13 | **/ 14 | public class MusicService extends Service { 15 | 16 | private MediaPlayer mediaPlayer = new MediaPlayer(); 17 | private MusicBinder mBinder = new MusicBinder(); //获取MusicBinder的实例 18 | /*创建一个MusicBinder类,让它继承自Binder,然后在它的内部提供了initMediaPlayer方法**/ 19 | public class MusicBinder extends Binder { 20 | 21 | public void initMediaPlayer(){ 22 | Cursor cursor = null; 23 | try{ 24 | cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 25 | null, null, null, null); 26 | if (cursor!=null) { 27 | while(cursor.moveToNext()) { 28 | 29 | mediaPlayer.setDataSource(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))); 30 | mediaPlayer.prepare(); 31 | 32 | } 33 | } 34 | 35 | } catch (Exception e){ 36 | e.printStackTrace(); 37 | } 38 | } 39 | 40 | } 41 | 42 | //在onBinder方法中返回这个实例 43 | public IBinder onBind(Intent intent){ 44 | return mBinder; 45 | } 46 | 47 | @Override 48 | public void onCreate() 49 | { 50 | super.onCreate(); 51 | mBinder.initMediaPlayer(); 52 | 53 | } 54 | 55 | 56 | public int onStartCommand(Intent intent, int flags, int id) 57 | { 58 | //mediaPlayer.reset(); 59 | if (!mediaPlayer.isPlaying()){ 60 | mediaPlayer.start(); 61 | } 62 | return super.onStartCommand(intent, flags, id); 63 | } 64 | 65 | @Override 66 | public void onDestroy() 67 | { 68 | super.onDestroy(); 69 | if (mediaPlayer.isPlaying()) { 70 | mediaPlayer.stop(); 71 | mediaPlayer.release(); 72 | } 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /app/src/main/layout/activity_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 18 | 19 |