import android.Manifest; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.SeekBar; public class MainActivity extends AppCompatActivity { private Button btn1; private Button btn2; private Button btn3; private Button btn4; private static SeekBar sb; private IService iService; private MusicConn conn; public static Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { Bundle bundle = msg.getData(); int duration = bundle.getInt("duration"); int position = bundle.getInt("position"); sb.setMax(duration); sb.setProgress(position); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); sb = (SeekBar) findViewById(R.id.sb); // Intent intent = new Intent(this, MusicService.class); startService(intent); conn = new MusicConn(); bindService(intent, conn, BIND_AUTO_CREATE); // btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getPermissons(); iService.callPlay(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { iService.callStop(); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { iService.callPause(); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { iService.callContinueMusic(); } }); sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {} @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) { iService.callSeekTo(seekBar.getProgress()); } }); } private void getPermissons() { int code = ActivityCompat.checkSelfPermission( MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (code != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); } //播放網絡歌曲 未請求權限 // code = ActivityCompat.checkSelfPermission( // MainActivity.this, // Manifest.permission.INTERNET); // if (code != PackageManager.PERMISSION_GRANTED) { // ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.INTERNET},1); // } } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } class MusicConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { iService = (IService) service; } @Override public void onServiceDisconnected(ComponentName name) { } } }
import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.util.Log; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import static android.content.ContentValues.TAG; public class MusicService extends Service { private MediaPlayer mediaPlayer; private Timer timer = null; private TimerTask task = null; public MusicService() {} @Override public void onCreate() { //初始化mediaplayer mediaPlayer = new MediaPlayer(); //播放結束後,取消定時器 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { task.cancel(); timer.cancel(); } }); super.onCreate(); } @Override public IBinder onBind(Intent intent) { return new MusicBinder(); } private void play() { try { mediaPlayer.reset(); //播放本地音樂 // mediaPlayer.setDataSource("/storage/emulated/0/fly.mp3"); // mediaPlayer.prepare(); //播放網絡音樂,拖放會有問題 mediaPlayer.setDataSource("http://123.56.111.226:8080/boat.mp3"); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); } }); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); refereshSeekBar(); } private void refereshSeekBar() { timer = new Timer(); task = new TimerTask() { @Override public void run() { int position = mediaPlayer.getCurrentPosition(); int duration = mediaPlayer.getDuration(); Message msg = new Message(); Bundle data = new Bundle(); data.putInt("duration",duration); data.putInt("position",position); msg.setData(data); MainActivity.handler.sendMessage(msg); } }; timer.schedule(task,100,1000); } public void seekTo(int position) { mediaPlayer.seekTo(position); } private void stop(){ mediaPlayer.stop(); Log.i(TAG, "stop: "); } private void continueMusic() { mediaPlayer.start(); } private void pause() { mediaPlayer.pause(); } //------------------------------------------------------ class MusicBinder extends Binder implements IService{ @Override public void callPlay() { play(); } @Override public void callStop() { stop(); } @Override public void callPause() { pause(); } @Override public void callContinueMusic() { continueMusic(); } @Override public void callSeekTo(int position) { seekTo(position); } } }
public interface IService { void callPlay(); void callStop(); void callPause(); void callContinueMusic(); void callSeekTo(int position); }