欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现音乐播放器

程序员文章站 2023-11-09 12:54:58
需求描述: 拥有播放,暂停,重新播放和停止等功能。 并且随着音乐的进度,进图条会自动更新。手动拖动进度条也会更新音乐的进度。 效果展示 示例代码 mainac...

需求描述: 拥有播放,暂停,重新播放和停止等功能。 并且随着音乐的进度,进图条会自动更新。手动拖动进度条也会更新音乐的进度。

效果展示

android实现音乐播放器

示例代码

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"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".mainactivity">

 <button
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginstart="8dp"
  android:layout_margintop="8dp"
  android:onclick="playmusic"
  android:text="播放"
  app:layout_constraintend_tostartof="@+id/button2"
  app:layout_constrainthorizontal_bias="0.5"
  app:layout_constraintstart_tostartof="parent"
  app:layout_constrainttop_totopof="parent" />

 <button
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="暂停"
  app:layout_constraintend_tostartof="@+id/button3"
  app:layout_constrainthorizontal_bias="0.5"
  app:layout_constraintstart_toendof="@+id/button"
  app:layout_constrainttop_totopof="@+id/button"
  android:onclick="onpause"/>

 <button
  android:id="@+id/button3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="重新播放"
  app:layout_constraintend_tostartof="@+id/button4"
  app:layout_constrainthorizontal_bias="0.5"
  app:layout_constraintstart_toendof="@+id/button2"
  app:layout_constrainttop_totopof="@+id/button2"
  android:onclick="replaymusic"/>

 <button
  android:id="@+id/button4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="停止播放"
  app:layout_constraintend_toendof="parent"
  app:layout_constrainthorizontal_bias="0.5"
  app:layout_constraintstart_toendof="@+id/button3"
  app:layout_constrainttop_totopof="@+id/button3"
  android:onclick="stopmusci"/>

 <seekbar
  android:id="@+id/seekbar"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_marginstart="8dp"
  android:layout_margintop="32dp"
  android:layout_marginend="8dp"
  app:layout_constraintend_toendof="parent"
  app:layout_constrainthorizontal_bias="1.0"
  app:layout_constraintstart_tostartof="parent"
  app:layout_constrainttop_tobottomof="@+id/button2" />

</android.support.constraint.constraintlayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.www.musicdemo">

 <uses-permission android:name="android.permission.internet" />

 <application
  android:allowbackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundicon="@mipmap/ic_launcher_round"
  android:supportsrtl="true"
  android:theme="@style/apptheme">
  <activity android:name=".mainactivity">
   <intent-filter>
    <action android:name="android.intent.action.main" />

    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>

  <service
   android:name=".myservice"
   android:enabled="true"
   android:exported="true"></service>
 </application>

</manifest>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。