重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
需求描述: 拥有播放,暂停,重新播放和停止等功能。 并且随着音乐的进度,进图条会自动更新。手动拖动进度条也会更新音乐的进度。
成都创新互联专业为企业提供牡丹网站建设、牡丹做网站、牡丹网站设计、牡丹网站制作等企业网站建设、网页设计与制作、牡丹企业网站模板建站服务,十余年牡丹做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
效果展示
示例代码
MainActivity
package com.example.www.musicdemo; import android.Manifest; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.media.MediaPlayer; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.SeekBar; import java.io.IOException; public class MainActivity extends AppCompatActivity { private Iservice mIservice; private static SeekBar mSeekBar; public static Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { //获取我们携带的数据 Bundle data = msg.getData(); //获取歌曲的总时长 和 当前进度 int duration = data.getInt("duration"); int currentPosition = data.getInt("currentPosition"); mSeekBar.setMax(duration); mSeekBar.setProgress(currentPosition); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // requestPermissions(new String[]{Manifest.permission.INTERNET}, 100); mSeekBar = (SeekBar) findViewById(R.id.seekBar); Intent intent = new Intent(this, MyService.class); startService(intent); bindService(intent, conn, BIND_AUTO_CREATE); // 设置seekbar的拖动时间监听 mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mIservice.callSeekTo(seekBar.getProgress()); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } public void playmusic(View view) { mIservice.callPlayMusic(); } public void onPause(View view) { mIservice.callPauseMusic(); } public void stopMusci(View view) { mIservice.stopMusic(); } public void replaymusic(View view) { mIservice.callRePlayMusic(); } ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mIservice = (Iservice) service; } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } }
MyService
package com.example.www.musicdemo; 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 java.util.Timer; import java.util.TimerTask; public class MyService extends Service { private MediaPlayer mMp; public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return new MyBinder(); } @Override public void onCreate() { mMp = new MediaPlayer(); super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } public void playMusic(){ try { mMp.setDataSource("/resource/n1/77/39/2163816420.mp3"); mMp.prepare(); mMp.start(); updateSeekBar(); } catch (Exception e) { e.printStackTrace(); } } public void pauseMusic(){ mMp.pause(); } public void stopMusic(){ mMp.stop(); } public void updateSeekBar(){ final int duration = mMp.getDuration(); final Timer timer = new Timer(); final TimerTask timerTask = new TimerTask() { @Override public void run() { int currentPosition = mMp.getCurrentPosition(); Message msg = Message.obtain(); Bundle bundle = new Bundle(); // map bundle.putInt("duration", duration); bundle.putInt("currentPosition", currentPosition); msg.setData(bundle); MainActivity.mHandler.sendMessage(msg); } }; timer.schedule(timerTask, 100, 1000); // 监听音乐播放完毕 mMp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { System.out.println("歌曲播放完成了"); timer.cancel(); timerTask.cancel(); } }); } //实现指定播放的位置 public void seekTo(int position){ mMp.seekTo(position); } private class MyBinder extends Binder implements Iservice{ @Override public void callPlayMusic() { playMusic(); } @Override public void callPauseMusic() { pauseMusic(); } @Override public void callRePlayMusic() { mMp.start(); } @Override public void stopMusic() { MyService.this.stopMusic(); } @Override public void callSeekTo(int position) { seekTo(position); } } }
Iservice
package com.example.www.musicdemo; /** * @author Administrator * @name mutilMedia * @class name:com.example.www.musicdemo * @class describe * @time 2019/4/8 11:23 * @change * @chang time * @class describe */ public interface Iservice { //把想暴露的方法都定义在接口中 public void callPlayMusic(); public void callPauseMusic(); public void callRePlayMusic(); public void stopMusic(); public void callSeekTo(int position); }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。