郑州app开发android音乐播放器实例。布局代码是一个imagebutton和seekbar。
创新互联公司-专业网站定制、快速模板网站建设、高性价比唐县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式唐县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖唐县地区。费用合理售后完善,十多年实体公司更值得信赖。
下面是java代码
MainActivity.java
package cn.xhhkj.musiccyd;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.Mediaplayer;
import android.os.bundle;
import android.os.Handler;
import android.os.Ibinder;
import android.view.View;
import android.widget.Imagebutton;
import android.widget.Seekbar;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "结果是";
private static final int UpDATE_pROGRESS = 0;
public String path = "mnt/sdcard/Music/pingguo.mp3";
private Imagebutton ib_play;
private Seekbar sb_progress;
private MusicplayerService.Mybinder musicController;
private MyConnection conn;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case UpDATE_pROGRESS:
updateprogress();
break;
}
};
};
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib_play = findViewbyId(R.id.musicbutton);
sb_progress = findViewbyId(R.id.seekbar);
sb_progress.setOnSeekbarChangeListener(new Seekbar.OnSeekbarChangeListener() {
@Override
public void onprogressChanged(Seekbar seekbar, int progress, boolean fromUser) {
if(fromUser){
musicController.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(Seekbar seekbar) {
}
@Override
public void onStopTrackingTouch(Seekbar seekbar) {
}
});
Intent service = new Intent(this,MusicplayerService.class);
startService(service);
conn = new MyConnection();
bindService(service, conn, bIND_AUTO_CREATE);
}
public void play(View view) {
// Mediaplayer player = new Mediaplayer();
// try {
// player.setDataSource(path);
// player.prepare();
// player.start();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
musicController.playpause();
updateplayIcon();
}
class MyConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, Ibinder service) {
musicController = (MusicplayerService.Mybinder) service;
updateplayIcon();
sb_progress.setMax(musicController.getDuration());
sb_progress.setprogress(musicController.getCurrentposition());
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
private void updateplayIcon(){
if(musicController.isplaying()){
ib_play.setImageResource(R.drawable.pause);
handler.sendEmptyMessage(UpDATE_pROGRESS);
}else{
ib_play.setImageResource(R.drawable.play);
handler.removeMessages(UpDATE_pROGRESS);
}
}
@Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
@Override
protected void onResume() {
super.onResume();
if(musicController!=null){
handler.sendEmptyMessage(UpDATE_pROGRESS);
}
}
private void updateprogress(){
int currentposition = musicController.getCurrentposition();
sb_progress.setprogress(currentposition);
handler.sendEmptyMessageDelayed(UpDATE_pROGRESS, 500);
}
}
MusicplayerService.java
package cn.xhhkj.musiccyd;
import android.app.Service;
import android.content.Intent;
import android.media.Mediaplayer;
import android.os.binder;
import android.os.Ibinder;
import androidx.annotation.Nullable;
public class MusicplayerService extends Service {
private Mediaplayer player;
private String path = "mnt/sdcard/Music/pingguo.mp3";
@Nullable
@Override
public Ibinder onbind(Intent intent) {
return new Mybinder();
}
public class Mybinder extends binder {
public void playpause(){
if(player.isplaying()){
player.pause();
}else{
player.start();
}
}
public boolean isplaying(){
return player.isplaying();
}
public int getDuration(){
return player.getDuration();
}
public int getCurrentposition(){
return player.getCurrentposition();
}
public void seekTo(int msec){
player.seekTo(msec);
}
}
@Override
public void onCreate() {
super.onCreate();
player = new Mediaplayer();
try {
player.setDataSource(path);
player.prepare();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
网页标题:android音乐播放器实例
新闻来源:http://lswzjz.com/article/sccoco.html