效果
代码
Service.java
public class MusicFunctionService extends Service {
public ArrayList< String> musicList = null;
public ArrayList< Song> songs = new ArrayList < > ( ) ;
public MediaPlayer mediaPlayer;
public Handler handler;
private SeekBar seekBar;
public int type = 1 ;
public int current = 0 ;
public class MusicBinder extends Binder {
public MusicFunctionService get ( ) {
return MusicFunctionService. this ;
}
}
@Override
public void onCreate ( ) {
super . onCreate ( ) ;
handler = new Handler ( ) ;
seekBar = MediaPlayerTestActivity. seekBar;
initDate ( ) ;
}
@Nullable
@Override
public IBinder onBind ( Intent intent) {
return new MusicBinder ( ) ;
}
public void initDate ( ) {
musicList = new ArrayList < > ( ) ;
if ( Environment. getExternalStorageState ( ) . equals ( Environment. MEDIA_MOUNTED) ) {
File file0 = Environment. getExternalStorageDirectory ( ) ;
File[ ] files = file0. listFiles ( ) ;
for ( int i = 0 ; i< files. length; i++ ) {
if ( files[ i] . isFile ( ) && files[ i] . getName ( ) . endsWith ( ".mp3" ) ) {
musicList. add ( files[ i] . getAbsolutePath ( ) ) ;
}
}
}
}
public void playMusic ( String path) {
if ( mediaPlayer != null && mediaPlayer. isPlaying ( ) ) {
stopMusic ( ) ;
}
if ( mediaPlayer == null) {
try {
mediaPlayer = new MediaPlayer ( ) ;
mediaPlayer. setDataSource ( path) ;
mediaPlayer. prepareAsync ( ) ;
mediaPlayer. setOnPreparedListener ( new MediaPlayer. OnPreparedListener ( ) {
@Override
public void onPrepared ( MediaPlayer mp) {
mediaPlayer. start ( ) ;
seekBar. setMax ( mediaPlayer. getDuration ( ) ) ;
Thread t = new Thread ( ) {
@Override
public void run ( ) {
super . run ( ) ;
if ( mediaPlayer!= null && mediaPlayer. isPlaying ( ) ) {
seekBar. setProgress ( mediaPlayer. getCurrentPosition ( ) ) ;
handler. postDelayed ( this , 1000 ) ;
}
}
} ;
handler. post ( t) ;
}
} ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
else {
mediaPlayer. start ( ) ;
}
mediaPlayer. setOnCompletionListener ( new MediaPlayer. OnCompletionListener ( ) {
@Override
public void onCompletion ( MediaPlayer mp) {
if ( mediaPlayer != null)
stopMusic ( ) ;
switch ( type) {
case 1 :
if ( current < musicList. size ( ) - 1 ) {
playMusic ( musicList. get ( ++ current) ) ;
}
else
playMusic ( musicList. get ( current) ) ;
break ;
case 2 :
int index = new Random ( ) . nextInt ( musicList. size ( ) - 1 ) ;
playMusic ( musicList. get ( index) ) ;
break ;
case 3 :
playMusic ( musicList. get ( current) ) ;
break ;
}
}
} ) ;
}
public void stopMusic ( ) {
mediaPlayer. stop ( ) ;
mediaPlayer. release ( ) ;
mediaPlayer = null;
}
public void pauseMusic ( ) {
if ( mediaPlayer != null && mediaPlayer. isPlaying ( ) ) {
mediaPlayer. pause ( ) ;
}
}
}
Activity.java
public class MediaPlayerTestActivity extends AppCompatActivity {
private ListView listView;
public static SeekBar seekBar;
public MusicFunctionService mService;
public Intent intent;
public static final int ORDER_PLAY= 1 ;
public static final int RANDOM_PLAY= 2 ;
public static final int LOOPING_PLAY= 3 ;
ServiceConnection connection = new ServiceConnection ( ) {
@Override
public void onServiceConnected ( ComponentName name, IBinder service) {
mService = ( ( MusicFunctionService. MusicBinder) service) . get ( ) ;
}
@Override
public void onServiceDisconnected ( ComponentName name) {
}
} ;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_media_player_test) ;
listView = findViewById ( R. id. list_music) ;
seekBar = findViewById ( R. id. pro1) ;
if ( Build. VERSION. SDK_INT >= Build. VERSION_CODES. M) {
requestPermissions ( new String [ ] { Manifest. permission. READ_EXTERNAL_STORAGE,
Manifest. permission. WRITE_EXTERNAL_STORAGE} , 100 ) ;
}
intent = new Intent ( this , MusicFunctionService. class ) ;
bindService ( intent, connection, BIND_AUTO_CREATE) ;
}
public void init ( ) {
ArrayAdapter< String> arrayAdapter = new ArrayAdapter < String> ( this , R. layout. support_simple_spinner_dropdown_item, mService. musicList) ;
listView. setAdapter ( arrayAdapter) ;
listView. setOnItemClickListener ( new AdapterView. OnItemClickListener ( ) {
@Override
public void onItemClick ( AdapterView< ? > parent, View view, int position, long id) {
String musicPath = mService. musicList. get ( position) ;
mService. current = position;
mService. playMusic ( musicPath) ;
}
} ) ;
seekBar. setOnSeekBarChangeListener ( new SeekBar. OnSeekBarChangeListener ( ) {
@Override
public void onProgressChanged ( SeekBar seekBar, int progress, boolean fromUser) {
if ( mService. mediaPlayer != null && mService. mediaPlayer. isPlaying ( ) && fromUser == true ) {
mService. mediaPlayer. seekTo ( progress) ;
}
}
@Override
public void onStartTrackingTouch ( SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch ( SeekBar seekBar) {
}
} ) ;
}
public void play ( View view) {
}
public void pause ( View view) {
mService. pauseMusic ( ) ;
}
public void stop ( View view) {
if ( mService. mediaPlayer != null && mService. mediaPlayer. isPlaying ( ) )
mService. stopMusic ( ) ;
}
public void circular ( View view) {
mService. type = Type. LOOPING_PLAY. index;
}
public void orderPlay ( View view) {
mService. type = Type. ORDER_PLAY. index;
}
public void randomPlay ( View view) {
mService. type = Type. RANDOM_PLAY. index;
}
public void next ( View view) {
}
public void loadDate ( View view) {
init ( ) ;
}
}
布局
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: app= "http://schemas.android.com/apk/res-auto"
xmlns: tools= "http://schemas.android.com/tools"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
tools: context= ".MediaPlayerTestActivity"
android: orientation= "vertical" >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: orientation= "horizontal" >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "上一首"
android: onClick= "next" / >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "暂停"
android: onClick= "pause" / >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "停止"
android: onClick= "stop" / >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "下一首" / >
< / LinearLayout>
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "wrap_content" >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "单曲循环"
android: onClick= "circular" / >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "顺序播放"
android: onClick= "orderPlay" / >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "随机播放"
android: onClick= "randomPlay" / >
< Button
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: onClick= "loadDate"
android: text= "获取数据" / >
< / LinearLayout>
< SeekBar
android: id= "@+id/pro1"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_marginTop= "10dp" / >
< ListView
android: id= "@+id/list_music"
android: layout_width= "match_parent"
android: layout_height= "match_parent" >
< / ListView>
< / LinearLayout>
清单文件
< uses- permission android: name= "android.permission.READ_EXTERNAL_STORAGE" / >
< uses- permission android: name= "android.permission.WRITE_EXTERNAL_STORAGE" / >
< service android: name= ".service.MusicFunctionService" / >