android四大组件之Service 从网络上播放音乐
package com.example.musicplay; import java.io.InputStream; import java.io.Reader; import java.math.BigDecimal; import java.net.URL; import java.sql.Array; import java.sql.Blob; import java.sql.Clob; import java.sql.Connection; import java.sql.Date; import java.sql.NClob; import java.sql.ParameterMetaData; import java.sql.PreparedStatement; import java.sql.Ref; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.RowId; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Binder; import android.os.IBinder; public class PlayService extends Service { private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return new MusicBinder(); } class MusicBinder extends Binder implements PlayInterface{ @Override public void play() { // TODO Auto-generated method stub PlayService.this.play(); } @Override public void playContiue() { // TODO Auto-generated method stub PlayService.this.playContiue(); } @Override public void pause() { // TODO Auto-generated method stub PlayService.this.pause(); } } @Override public void onCreate() { mediaPlayer = new MediaPlayer(); } private void play(){ //重置 mediaPlayer.reset(); try { mediaPlayer.setDataSource("http://192.168.21.1:8080/ok/Charlotte Perrelli - Hero.mp3"); //准备 mediaPlayer.prepareAsync(); } catch (Exception e) { // TODO Auto-generated catcssh block e.printStackTrace(); } mediaPlayer.setOnPreparedListener(new OnPreparedListener() { //准备完毕此方法调用 @Override public void onPrepared(MediaPlayer arg0) { // TODO Auto-generated method stub mediaPlayer.start(); } }); } private void playContiue(){ mediaPlayer.start(); } private void pause(){ mediaPlayer.pause(); } }