怎么在Android中利用SeekBar实现音乐播放器功能
怎么在Android中利用SeekBar实现音乐播放器功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
成都创新互联公司长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为久治企业提供专业的网站建设、成都网站制作,久治网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
一.Demo简介
利用AIDL为Activity绑定服务,利用Handler机制,发送消息更新SeekBar的UI,利用计时器定时更新SeekBar的进度。实现对音乐的开始播放,到暂停,继续,以及停止播放的功能。以及实现拖动进度条播放音乐的功能。
二.AIDL接口
利用AIDL机制提供给MainActivity访问MyMusicService的方法,实现播放暂停等功能。
三.主要类代码
1.MyMusicService
package my.com.mymusicp; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; import android.support.annotation.Nullable; import android.util.Log; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; /** * Created by c_ljf on 16-12-26. */ public class MyMusicService extends Service { private String tag="MyMusicService"; private MediaPlayer mediaPlayer; @Override public void onCreate() { super.onCreate(); mediaPlayer = new MediaPlayer(); } @Nullable @Override public IBinder onBind(Intent intent) { return new MyMusicBinder(); } class MyMusicBinder extends IMyMusicService.Stub{ @Override public void play() throws RemoteException { playmusic(); } @Override public void pause() throws RemoteException { pausemusic(); } @Override public void stop() throws RemoteException { stopmusic(); } @Override public void continuePlay() throws RemoteException { rePlayMusic(); } @Override public void seekPlayProgress(int position) throws RemoteException { seekPlayPositiom(position); } } //播放音乐 public void playmusic(){ mediaPlayer.reset(); String url = "sdcard/Music/Fade.mp3"; Log.i(tag,url); try { mediaPlayer.setDataSource(url); mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } // might take long! (for buffering, etc) mediaPlayer.start(); //更新播放进度条 seekPlayProgress(); } public void pausemusic(){ mediaPlayer.pause(); Log.i(tag,"暂停播放"); } public void stopmusic(){ mediaPlayer.reset(); Log.i(tag,"停止播放"); mediaPlayer.stop(); } @Override public void onDestroy() { super.onDestroy(); } public void seekPlayPositiom(int positon){ Log.i(tag,"设置播放位置"); mediaPlayer.seekTo(positon); } public void seekPlayProgress(){ /*1.获取当前歌曲的总时长*/ final int duration=mediaPlayer.getDuration(); //计时器对象 final Timer timer=new Timer(); final TimerTask task=new TimerTask() { @Override public void run() { //开启线程定时获取当前播放进度 int currentposition=mediaPlayer.getCurrentPosition(); //利用message给主线程发消息更新seekbar进度 Message ms=Message.obtain(); Bundle bundle=new Bundle(); bundle.putInt("duration",duration); Log.i(tag,"歌曲总长度"+duration); bundle.putInt("currentposition",currentposition); Log.i(tag,"当前长度"+currentposition); //设置发送的消息内容 ms.setData(bundle); //发送消息 MainActivity.handler.sendMessage(ms); } }; timer.schedule(task,300,500); //当播放结束时停止播放 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.i(tag,"取消计时任务"); timer.cancel(); task.cancel(); } }); } public void rePlayMusic() { Log.i(tag, "继续播放音乐"); mediaPlayer.start(); } }
2.Mainactivity
package my.com.mymusicp; import android.Manifest; import android.app.Activity; 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.os.RemoteException; 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 IMyMusicService iMyMusicService; private static SeekBar seekBar; private Button btpause_play; private boolean b=true;//判断暂停和继续播放 public static Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { //处理消息 Bundle bundle=msg.getData(); //获取歌曲长度和当前播放位置,并设置到进度条上 int duration=bundle.getInt("duration"); int currentposition=bundle.getInt("currentposition"); seekBar.setMax(duration); seekBar.setProgress(currentposition); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); verifyStoragePermissions(MainActivity.this); //获取播放暂停按钮 btpause_play=(Button) findViewById(R.id.bt_pause); Intent intent=new Intent(MainActivity.this,MyMusicService.class); bindService(intent,new MyConn(),BIND_AUTO_CREATE); seekBar=(SeekBar) findViewById(R.id.seekBar); seekBar.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) { try { iMyMusicService.seekPlayProgress(seekBar.getProgress()); } catch (RemoteException e) { e.printStackTrace(); } } }); } //定义服务连接类 class MyConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { iMyMusicService=IMyMusicService.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } } public void play(View v){ try { iMyMusicService.play(); } catch (RemoteException e) { e.printStackTrace(); } } public void pause(View v){ try { if(b) { iMyMusicService.pause(); b=false; btpause_play.setText("继续播放"); } else{ b=true; iMyMusicService.continuePlay(); btpause_play.setText("暂停播放"); } } catch (RemoteException e) { e.printStackTrace(); } } public void stop(View v){ try { iMyMusicService.stop(); } catch (RemoteException e) { e.printStackTrace(); } } // Storage Permissions private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, }; /** * Checks if the app has permission to write to device storage * * If the app does not has permission then the user will be prompted to grant permissions * * @param activity */ public static void verifyStoragePermissions(Activity activity) { // Check if we have write permission int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions( activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE ); } } }
四.布局文件
<?xml version="1.0" encoding="utf-8"?>
五.注册服务
<?xml version="1.0" encoding="utf-8"?>
关于怎么在Android中利用SeekBar实现音乐播放器功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
当前名称:怎么在Android中利用SeekBar实现音乐播放器功能
URL网址:http://hbruida.cn/article/joogce.html