Android Studio初学者实例:仿网易音乐播放器

打印 上一主题 下一主题

主题 1709|帖子 1709|积分 5127

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本期带来的是以Service为主要的知识点的网易音乐播放器
看一下效果图

 首先项目准备:
在res下新建raw文件夹,并在文件夹中添加喜爱的mp3音乐

 OK,第一步,先写一个背景文件,在res/drawable文件夹中新建xml文件:
btn_bg_selector.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.     <item android:state_pressed="true" >
  4.         <shape android:shape="rectangle">
  5.             <corners android:radius="3dp"/>
  6.             <solid android:color="#d4d4d4"/>
  7.         </shape>
  8.     </item>
  9.     <item android:state_pressed="false" >
  10.         <shape android:shape="rectangle">
  11.             <corners android:radius="3dp"/>
  12.             <solid android:color="#ffffff" />
  13.         </shape>
  14.     </item>
  15. </selector>
复制代码
 编写主界面代码activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     android:gravity="center"
  9.     android:background="@drawable/music_bg"
  10.     tools:context=".MainActivity">
  11.     <RelativeLayout
  12.         android:layout_width="match_parent"
  13.         android:layout_height="160dp">
  14.         <RelativeLayout
  15.             android:layout_width="300dp"
  16.             android:layout_height="70dp"
  17.             android:id="@+id/rl_title"
  18.             android:layout_centerHorizontal="true"
  19.             android:background="@drawable/title_bg"
  20.             android:gravity="center_horizontal"
  21.             android:paddingLeft="80dp">
  22.             <TextView
  23.                 android:layout_width="wrap_content"
  24.                 android:layout_height="wrap_content"
  25.                 android:id="@+id/tv_music_title"
  26.                 android:layout_marginTop="8dp"
  27.                 android:text="体面"
  28.                 android:textSize="12sp"
  29.                 android:textStyle="bold"
  30.                 android:textColor="@color/black"/>
  31.             <TextView
  32.                 android:layout_width="wrap_content"
  33.                 android:layout_height="wrap_content"
  34.                 android:id="@+id/tv_type"
  35.                 android:layout_marginTop="4dp"
  36.                 android:layout_below="@id/tv_music_title"
  37.                 android:layout_alignLeft="@id/tv_music_title"
  38.                 android:text="流行音乐"
  39.                 android:textSize="10dp"/>
  40.             <SeekBar
  41.                 android:layout_width="150dp"
  42.                 android:layout_height="wrap_content"
  43.                 android:id="@+id/sb"
  44.                 android:layout_below="@+id/rl_time"
  45.                 android:layout_alignParentBottom="true"
  46.                 android:thumb="@null"/>
  47.             <RelativeLayout
  48.                 android:layout_width="150dp"
  49.                 android:layout_height="wrap_content"
  50.                 android:layout_marginTop="4dp"
  51.                 android:id="@+id/rl_time"
  52.                 android:layout_below="@id/tv_type">
  53.                 <TextView
  54.                     android:layout_width="wrap_content"
  55.                     android:layout_height="wrap_content"
  56.                     android:id="@+id/tv_progress"
  57.                     android:text="00:00"
  58.                     android:textSize="10dp"/>
  59.                 <TextView
  60.                     android:layout_width="wrap_content"
  61.                     android:layout_height="wrap_content"
  62.                     android:id="@+id/tv_total"
  63.                     android:layout_alignParentRight="true"
  64.                     android:text="00:00"/>
  65.             </RelativeLayout>
  66.         </RelativeLayout>
  67.         <LinearLayout
  68.             android:layout_width="340dp"
  69.             android:layout_height="90dp"
  70.             android:layout_below="@id/rl_title"
  71.             android:layout_centerHorizontal="true"
  72.             android:background="@drawable/btn_bg"
  73.             android:gravity="center_vertical"
  74.             android:paddingLeft="120dp"
  75.             android:paddingRight="10dp">
  76.             <Button
  77.                 android:layout_width="0dp"
  78.                 android:layout_height="25dp"
  79.                 android:layout_margin="4dp"
  80.                 android:id="@+id/btn_play"
  81.                 android:layout_weight="1"
  82.                 android:background="@drawable/btn_bg_selector"
  83.                 android:text="播放"
  84.                 android:textSize="10sp"/>
  85.             <Button
  86.                 android:layout_width="0dp"
  87.                 android:layout_height="25dp"
  88.                 android:id="@+id/btn_pause"
  89.                 android:layout_margin="4dp"
  90.                 android:layout_weight="1"
  91.                 android:background="@drawable/btn_bg_selector"
  92.                 android:text="暂停"
  93.                 android:textSize="10sp"/>
  94.             <Button
  95.                 android:layout_width="0dp"
  96.                 android:layout_height="25dp"
  97.                 android:id="@+id/btn_continue_play"
  98.                 android:layout_margin="4dp"
  99.                 android:layout_weight="1"
  100.                 android:background="@drawable/btn_bg_selector"
  101.                 android:text="继续"
  102.                 android:textSize="10sp"/>
  103.             <Button
  104.                 android:layout_width="0dp"
  105.                 android:layout_height="25dp"
  106.                 android:id="@+id/btn_exit"
  107.                 android:layout_margin="4dp"
  108.                 android:layout_weight="1"
  109.                 android:background="@drawable/btn_bg_selector"
  110.                 android:text="退出"
  111.                 android:textSize="10sp"/>
  112.         </LinearLayout>
  113.         <ImageView
  114.             android:layout_width="100dp"
  115.             android:layout_height="100dp"
  116.             android:id="@+id/iv_music"
  117.             android:layout_centerVertical="true"
  118.             android:layout_marginLeft="35dp"
  119.             android:layout_marginBottom="50dp"
  120.             android:src="@drawable/img_music"/>
  121.     </RelativeLayout>
  122. </LinearLayout>
复制代码
编写MusicService
  1. import android.app.Service;
  2. import android.content.Intent;
  3. import android.media.MediaPlayer;
  4. import android.os.Binder;
  5. import android.os.Bundle;
  6. import android.os.IBinder;
  7. import android.os.Message;
  8. import androidx.annotation.Nullable;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11. public class MusicService extends Service {
  12.     private MediaPlayer player;
  13.     private Timer timer;
  14.     public MusicService(){
  15.     }
  16.     @Nullable
  17.     @Override
  18.     public IBinder onBind(Intent intent) {
  19.         return new MusicControl();
  20.     }
  21.     @Override
  22.     public void onCreate() {
  23.         super.onCreate();
  24.         player=new MediaPlayer();
  25.     }
  26.     public  void addTimer(){
  27.         if (timer==null){
  28.             timer=new Timer();
  29.             TimerTask task=new TimerTask() {
  30.                 @Override
  31.                 public void run() {
  32.                     if (player==null)return;
  33.                     int duration=player.getDuration();
  34.                     int currentPosition=player.getCurrentPosition();
  35.                     Message msg=MainActivity.handler.obtainMessage();
  36.                     Bundle bundle=new Bundle();
  37.                     bundle.putInt("duration",duration);
  38.                     bundle.putInt("currentPosition",currentPosition);
  39.                     msg.setData(bundle);
  40.                     MainActivity.handler.sendMessage(msg);
  41.                 }
  42.             };
  43.             timer.schedule(task,5,500);
  44.         }
  45.     }
  46.     class MusicControl extends Binder{
  47.         public void play(){
  48.             try{
  49.                 player.reset();
  50.                 player=MediaPlayer.create(getApplicationContext(),R.raw.music);
  51.                 addTimer();
  52.             }catch (Exception e){
  53.                 e.printStackTrace();
  54.             }
  55.         }
  56.         public  void pausePlay(){
  57.             player.pause();
  58.         }
  59.         public  void continuePlay(){
  60.             player.start();
  61.         }
  62.         public void seekTo(int progress){
  63.             player.seekTo(progress);
  64.         }
  65.     }
  66.     @Override
  67.     public void onDestroy() {
  68.         super.onDestroy();
  69.         if (player==null)return;
  70.         if (player.isPlaying())player.stop();
  71.         player.release();
  72.         player=null;
  73.     }
  74. }
复制代码
注意:检查AndroidManifest.xml文件中是否注册了Service

 


  1.         <service android:name=".MusicService"
  2.             android:enabled="true"
  3.             android:exported="true"/>
复制代码
编写主界面逻辑代码MainActivity
  1. import androidx.annotation.NonNull;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.animation.ObjectAnimator;
  4. import android.animation.ValueAnimator;
  5. import android.content.ComponentName;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.IBinder;
  11. import android.os.Message;
  12. import android.view.View;
  13. import android.view.animation.LinearInterpolator;
  14. import android.widget.ImageView;
  15. import android.widget.SeekBar;
  16. import android.widget.TextView;
  17. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  18.     private static SeekBar sb;
  19.     private static TextView tv_progress,tv_total;
  20.     private ObjectAnimator animator;
  21.     private MusicService.MusicControl musicControl;
  22.     MyServiceConn conn;
  23.     Intent intent;
  24.     private boolean isUnbind=false;
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.         init();
  30.     }
  31.     private void  init(){
  32.         tv_progress=findViewById(R.id.tv_progress);
  33.         tv_total=findViewById(R.id.tv_total);
  34.         sb=findViewById(R.id.sb);
  35.         findViewById(R.id.btn_play).setOnClickListener(this);
  36.         findViewById(R.id.btn_pause).setOnClickListener(this);
  37.         findViewById(R.id.btn_continue_play).setOnClickListener(this);
  38.         findViewById(R.id.btn_exit).setOnClickListener(this);
  39.         intent=new Intent(this,MusicService.class);
  40.         conn=new MyServiceConn();
  41.         bindService(intent,conn,BIND_AUTO_CREATE);
  42.         sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  43.             @Override
  44.             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  45.                 if (progress==seekBar.getMax()){
  46.                     animator.pause();
  47.                 }
  48.             }
  49.             @Override
  50.             public void onStartTrackingTouch(SeekBar seekBar) {
  51.             }
  52.             @Override
  53.             public void onStopTrackingTouch(SeekBar seekBar) {
  54.                 int progress=seekBar.getProgress();
  55.                 musicControl.seekTo(progress);
  56.             }
  57.         });
  58.         ImageView iv_music = (ImageView) findViewById(R.id.iv_music);
  59.         animator = ObjectAnimator.ofFloat(iv_music, "rotation", 0f, 360.0f);
  60.         animator.setDuration(10000);  //动画旋转一周的时间为10秒
  61.         animator.setInterpolator(new LinearInterpolator());
  62.         animator.setRepeatCount(-1);  //-1表示设置动画无限循环
  63.     }
  64.     public  static Handler handler=new Handler(){
  65.         @Override
  66.         public void handleMessage(@NonNull Message msg) {
  67.             Bundle bundle=msg.getData();
  68.             int duration=bundle.getInt("duration");
  69.             int currentPosition=bundle.getInt("currentPosition");
  70.             sb.setMax(duration);
  71.             sb.setProgress(currentPosition);
  72.             int minute=duration/1000/60;
  73.             int second=duration/1000%60;
  74.             String strMinute=null;
  75.             String strSecond=null;
  76.             if (minute<10){
  77.                 strMinute="0"+minute;
  78.             }else {
  79.                 strMinute=minute+"";
  80.             }
  81.             if (second<10){
  82.                 strSecond="0"+second;
  83.             }else {
  84.                 strSecond=second+"";
  85.             }
  86.             tv_total.setText(strMinute+":"+strSecond);
  87.             minute=currentPosition/1000/60;
  88.             second=currentPosition/1000%60;
  89.             if (minute<10){
  90.                 strMinute="0"+minute;
  91.             }else {
  92.                 strMinute=minute+"";
  93.             }
  94.             if (second<10){
  95.                 strSecond="0"+second;
  96.             }else {
  97.                 strSecond=second+"";
  98.             }
  99.             tv_progress.setText(strMinute+":"+strSecond);
  100.             super.handleMessage(msg);
  101.         }
  102.     };
  103.     class MyServiceConn implements ServiceConnection{
  104.         @Override
  105.         public void onServiceConnected(ComponentName name, IBinder service) {
  106.             musicControl= (MusicService.MusicControl) service;
  107.         }
  108.         @Override
  109.         public void onServiceDisconnected(ComponentName name) {
  110.         }
  111.     }
  112.     private void unbind(boolean isUnbind){
  113.         if (!isUnbind){
  114.             musicControl.pausePlay();
  115.             unbindService(conn);
  116.             stopService(intent);
  117.         }
  118.     }
  119.     @Override
  120.     public void onClick(View v) {
  121.         switch (v.getId()){
  122.             case R.id.btn_play:
  123.                 musicControl.play();
  124.                 animator.start();
  125.                 break;
  126.             case R.id.btn_pause:
  127.                 musicControl.pausePlay();
  128.                 animator.pause();
  129.                 break;
  130.             case R.id.btn_continue_play:
  131.                 musicControl.continuePlay();
  132.                 animator.start();
  133.                 break;
  134.             case R.id.btn_exit:
  135.                 unbind(isUnbind);
  136.                 isUnbind=true;
  137.                 finish();
  138.                 break;
  139.         }
  140.     }
  141.     @Override
  142.     protected void onDestroy() {
  143.         super.onDestroy();
  144.         unbind(isUnbind);
  145.     }
  146. }
复制代码
期末比较繁忙,讲解后续补充,如有问题私信、评论联系

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表