简易音乐播放器(Android Studio)

Android期末作业,临时抱佛脚(手动滑稽

分享下音乐播放的源码吧。(只能播放内嵌音乐

PS:完整工程下载及相关说明:https://download.csdn.net/download/weixin_41918712/10586908


注意事项

卫星式菜单参考:https://www.cnblogs.com/tianhengblogs/p/5265195.html

接下来是音乐播放器步骤:

  • 在工程文件夹“ ..\app\src\main\res ”下,新建一个文件夹“raw”,然后把音乐放进去,命名为“ lucky.mp3 ” ;
  • 在工程文件夹 “ ..\app\src\main\res\drawable ”下,放入你喜欢的图片作为播放器背景,命名为“bg1”;以及4个控制按钮(尺寸:34*30),分别命名为 “exit”,“stop”,“play”,“pause”,和菜单按钮(尺寸:45*42)  “menu” ;
  • 在工程文件夹 “ ..\app\src\main\res\mipmap-hdpi ”下,放入歌手海报,命名为“img”。由于要制作旋转效果,故推荐选择圆形的图片;
  • 在工程文件夹“ ..\app\src\main\res ”下,新建一个文件夹“anim”,在里面新建3个xml文档,命名为“anim.xml”,“bigalpha.xml”,“smallalpha.xml”;
  • “ AndroidManifest.xml ”里,在</activity>后加一句<service android:name=".MusicService" android:enabled="true" android:exported="true" />
  • 在工程文件夹“ ..\app\src\main\res\values ”下,新建xml文档,命名为“dimens.xml”;找到styles.xml文件,把
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    改为

    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

预览图


源码

anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="300"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"/>
</set>

 

bigalpha.xml

<?xml version="1.0" encoding="utf-8"?>
<!--android:fillAfter="true"得加,取动画结束后的最后一帧-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="200"
        android:fromAlpha="1"
        android:toAlpha="0"/>
    <scale
        android:duration="200"
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="3"
        android:toYScale="3"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>

 

smallalpha.xml

<?xml version="1.0" encoding="utf-8"?>
<!--android:fillAfter="true"得加,取动画结束后的最后一帧-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="200"
        android:fromAlpha="1"
        android:toAlpha="0"/>
    <scale
        android:duration="200"
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="0"
        android:toYScale="0"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>

 

dimens.xml

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

 

MainActivity.java

1package com.example.slyarh.text;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {
    private TextView musicStatus, musicTime, musicTotal;
    private SeekBar seekBar;
    private ImageView btnPlayOrPause, btnStop, btnQuit;
    private SimpleDateFormat time = new SimpleDateFormat("mm:ss");
    private boolean tag1 = false;
    private boolean tag2 = false;
    private MusicService musicService;
    private ArcDemo mArc;
    //  在Activity中调用 bindService 保持与 Service 的通信
    private void bindServiceConnection() {
        Intent intent = new Intent(MainActivity.this, MusicService.class);
        startService(intent);
        bindService(intent, serviceConnection, this.BIND_AUTO_CREATE);
    }

    //  回调onServiceConnected 函数,通过IBinder 获取 Service对象,实现Activity与 Service的绑定
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicService = ((MusicService.MyBinder) (service)).getService();
            Log.i("musicService", musicService + "");
            musicTotal.setText(time.format(musicService.mediaPlayer.getDuration()));
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = null;
        }
    };

    //  通过 Handler 更新 UI 上的组件状态
    public Handler handler = new Handler();
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            musicTime.setText(time.format(musicService.mediaPlayer.getCurrentPosition()));
            seekBar.setProgress(musicService.mediaPlayer.getCurrentPosition());
            seekBar.setMax(musicService.mediaPlayer.getDuration());
            musicTotal.setText(time.format(musicService.mediaPlayer.getDuration()));
            handler.postDelayed(runnable, 200);

        }
    };

    private void findViewById() {
        musicTime = (TextView) findViewById(R.id.MusicTime);
        musicTotal = (TextView) findViewById(R.id.MusicTotal);
        seekBar = (SeekBar) findViewById(R.id.MusicSeekBar);
        btnPlayOrPause = (ImageView) findViewById(R.id.BtnPlayorPause);
        btnStop = (ImageView) findViewById(R.id.BtnStop);
        btnQuit = (ImageView) findViewById(R.id.BtnQuit);
        musicStatus = (TextView) findViewById(R.id.MusicStatus);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById();
        bindServiceConnection();
        //myListener();


        ImageView imageView = (ImageView) findViewById(R.id.Image);
        final ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360.0f);
        animator.setDuration(10000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(-1);



        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser == true) {
                    musicService.mediaPlayer.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mArc = (ArcDemo) findViewById(R.id.view_arc);
        mArc.setOnSubItemClickListener(new ArcDemo.onSubItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(position==5){
                    if (musicService.mediaPlayer != null) {
                        seekBar.setProgress(musicService.mediaPlayer.getCurrentPosition());
                        seekBar.setMax(musicService.mediaPlayer.getDuration());
                    }
                    //  由tag的变换来控制事件的调用
                    if (musicService.tag != true) {
                        //btnPlayOrPause.setText("PAUSE");
                        musicStatus.setText("正在播放..");
                        musicService.playOrPause();
                        musicService.tag = true;
                        btnPlayOrPause.setImageDrawable(getResources().getDrawable(R.drawable.pause));
                        if (tag1 == false) {
                            animator.start();
                            tag1 = true;
                        }
                        else {
                            animator.resume();
                        }
                    }
                    else {
                        //btnPlayOrPause.setText("PLAY");
                        musicStatus.setText("暂停播放..");
                        musicService.playOrPause();
                        animator.pause();
                        btnPlayOrPause.setImageDrawable(getResources().getDrawable(R.drawable.play));
                        musicService.tag = false;
                    }
                    if (tag2 == false) {
                        handler.post(runnable);
                        tag2 = true;
                    }
                }
                else if(position==6){
                    musicStatus.setText("停止播放..");
                    //  btnPlayOrPause.setText("PLAY");
                    musicService.stop();
                    btnPlayOrPause.setImageDrawable(getResources().getDrawable(R.drawable.play));
                    animator.pause();
                    musicService.tag = false;
                }
                else if(position==7) exit();
            }

        });
    }


    // 停止服务时,必须解除绑定,写入btnQuit按钮中
    public void exit(){
        handler.removeCallbacks(runnable);
        unbindService(serviceConnection);
        Intent intent = new Intent(MainActivity.this, MusicService.class);
        stopService(intent);
        try {
            MainActivity.this.finish();
        } catch (Exception e) {}
    }


    //  获取并设置返回键的点击事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}



 

activity_main.xml

1<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:background="@drawable/bg1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#00000000"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp">
            <ImageView
                android:id="@+id/Image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/img" />
            <!--显示歌曲状态-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/MusicStatus"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                android:gravity="center_horizontal">
                <!--显示当前进度-->
                <TextView
                    android:id="@+id/MusicTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="00:00" />
                <SeekBar
                    android:layout_width="230sp"
                    android:layout_height="wrap_content"
                    android:id="@+id/MusicSeekBar"
                    android:layout_weight="1"
                    android:max="100"
                    android:layout_toRightOf="@+id/MusicTime"/>
                <!--显示总进度-->
                <TextView
                    android:id="@+id/MusicTotal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="00:00"
                    android:layout_toRightOf="@+id/MusicSeekBar"/>
            </RelativeLayout>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:background="#00000000"
            android:layout_height="match_parent">
            <com.example.slyarh.text.ArcDemo
                android:background="#00000000"
                android:id="@+id/view_arc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true">
                <RelativeLayout
                    android:id="@+id/main_menu"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/menu">
                </RelativeLayout>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:id="@+id/BtnPlayorPause"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/play" />

                <ImageView
                    android:id="@+id/BtnStop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/stop" />

                <ImageView
                    android:id="@+id/BtnQuit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/exit" />
            </com.example.slyarh.text.ArcDemo>
        </RelativeLayout>
    </LinearLayout>
</FrameLayout>

 

MusicService.java

1package com.example.slyarh.text;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class MusicService extends Service {
    public MediaPlayer mediaPlayer=null;
    public boolean tag = false;

    public MusicService() {
        mediaPlayer = new MediaPlayer();

        try {
            mediaPlayer.reset();
            mediaPlayer = MediaPlayer.create(this, R.raw.lucky);
            mediaPlayer.seekTo(0);
            mediaPlayer.setLooping(true);
            stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //  通过 Binder 来保持 Activity 和 Service 的通信
    public MyBinder binder = new MyBinder();
    public class MyBinder extends Binder {
        MusicService getService() {
            return MusicService.this;
        }
    }
    public void playOrPause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.setLooping(true);
            mediaPlayer.pause();

        } else {
            mediaPlayer.setLooping(true);
            mediaPlayer.start();

        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.setLooping(true);
            try {
                mediaPlayer.reset();
                mediaPlayer = MediaPlayer.create(this, R.raw.lucky);
                mediaPlayer.seekTo(0);
                mediaPlayer.setLooping(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
}

 

ArcDemo.java

package com.example.slyarh.text;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;


public class ArcDemo extends ViewGroup {
    private View mButton;
    private BStatus mBStatus = BStatus.STATUS_CLOSE;
    private onSubItemClickListener onListener;
    public enum BStatus {
        STATUS_OPEN, STATUS_CLOSE
    }

    //子菜单点击接口
    public interface onSubItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnSubItemClickListener(onSubItemClickListener mListener) {
        this.onListener = mListener;
    }

    public ArcDemo(Context context) {
        super(context);
//        this(context, null);
    }

    public ArcDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
//        this(context, attrs, 0);
    }

    public ArcDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //添加布局,就是所要显示的控件View
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed) {
            //主菜单按钮
            onMainButton();
            //子菜单按钮
            onSubItemButton();
        }
    }

    //获取主菜单按钮
    private void onMainButton() {
        mButton = getChildAt(0);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //主菜单动画
                Animation rotateAnim = AnimationUtils.loadAnimation(getContext(), R.anim.anim);
                mButton.startAnimation(rotateAnim);

                //子菜单动画
                subItemAnim();
            }
        });
        int l, t, r = 0, b = 0;
        int mWidth = mButton.getMeasuredWidth();
        int mHeight = mButton.getMeasuredHeight();

        l = getMeasuredWidth() - mWidth;
        t = getMeasuredHeight() - mHeight;

        mButton.layout(l, t, getMeasuredWidth(), getMeasuredHeight());
    }

    //获取子菜单按钮
    private void onSubItemButton() {
        int count = getChildCount();
        for (int i = 0; i < count - 1; i++) {
            View childView = getChildAt(i + 1);

            //开始时不呈现子菜单
            childView.setVisibility(View.GONE);

            int radius = 350;
            int cl, ct, cr, cb;

            cr = (int) (radius * Math.sin(Math.PI / 2 / (count - 2) * i));
            cb = (int) (radius * Math.cos(Math.PI / 2 / (count - 2) * i));

            int cWidth = childView.getMeasuredWidth();
            int cHeight = childView.getMeasuredHeight();

            cl = getMeasuredWidth() - cWidth - cr;
            ct = getMeasuredHeight() - cHeight - cb;

            //layout(l,t,r,b);前两参数决定位置,后两参数决定大小
            //参数(1,t)为View控件的左上角坐标
            // (r-l,b-t)为View控件大小,r-l为控件宽度,b-t为控件高度
            childView.layout(cl, ct, getMeasuredWidth() - cr, getMeasuredHeight() - cb);
        }
    }

    //子菜单散开回笼动画
    public void subItemAnim() {
        int count = getChildCount();
        for (int i = 0; i < count - 1; i++) {
            final View cView = getChildAt(i + 1);

            //点击主菜单后,子菜单就立刻呈现,否则后面的动画无法完成
            cView.setVisibility(VISIBLE);

            int radius = 350;
            int l, t, r, d;

            r = (int) (radius * Math.sin(Math.PI / 2 / (count - 2) * i));
            d = (int) (radius * Math.cos(Math.PI / 2 / (count - 2) * i));

//            int cWidth = cView.getMeasuredWidth();
//            int cHeight = cView.getMeasuredHeight();
//
//            l = getMeasuredWidth() - cWidth - r;
//            t = getMeasuredHeight() - cHeight - d;

            AnimationSet set = new AnimationSet(true);
            Animation tranAnim = null;
            if (mBStatus == BStatus.STATUS_CLOSE) {
                //散开动画
                tranAnim = new TranslateAnimation(r, 0, d, 0);
                cView.setClickable(true);
                cView.setFocusable(true);
            } else {
                //回笼动画
                tranAnim = new TranslateAnimation(0, r, 0, d);
                cView.setClickable(false);
                cView.setFocusable(false);
            }
            tranAnim.setDuration(300);
//            tranAnim.setFillAfter(true);  //让最后一帧的动画不消失
            tranAnim.setStartOffset(100 * i / count);
            tranAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if (mBStatus == BStatus.STATUS_CLOSE) {
                        cView.setVisibility(GONE);
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });


            Animation rotateAnim = new RotateAnimation(
                    0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnim.setDuration(300);
//            rotateAnim.setFillAfter(false);

            set.addAnimation(rotateAnim);
            set.addAnimation(tranAnim);
            cView.startAnimation(set);

            //散开后子菜单的点击监听事件
            final int pos = i + 1;
            cView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onListener != null) {
                        onListener.onItemClick(cView, pos);
                    }
                    //散开后点击子菜单动画
                    subItemClickAnim(pos - 1);
                    changStatus();
                }
            });
        }
        changStatus();
    }

    //监听子菜单状态改变
    private void changStatus() {
        mBStatus = (mBStatus == BStatus.STATUS_CLOSE ? BStatus.STATUS_OPEN : BStatus.STATUS_CLOSE);
    }

    //散开后点击子菜单动画
    private void subItemClickAnim(int pos) {
        int count = getChildCount();
        for (int i = 0;i<count-1;i++) {
            View cView = getChildAt(i+1);

            if(i == pos) {
                //变大,变透明
                cView.startAnimation(toBig());
            }
            else {
                //变小,变透明
                cView.startAnimation(toSmall());
            }
            cView.setClickable(false);
            cView.setFocusable(false);
        }
    }

    //变大,变透明
    private Animation toBig(){
        Animation big = AnimationUtils.loadAnimation(getContext(),R.anim.bigalpha);
        return big;
    }

    //变小,变透明
    private Animation toSmall(){
        Animation small = AnimationUtils.loadAnimation(getContext(),R.anim.smallalpha);
        return small;
    }

    //给ListView调用
    public boolean isOpen() {
        return mBStatus == BStatus.STATUS_OPEN;
    }

}

 


✎﹏﹏₯㎕《晴天》从出生那年就飘着...﹍﹍﹍﹍﹍﹍

  • 12
    点赞
  • 124
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
### 回答1: 制作简易音乐播放器需要了解 Android 开发基础知识,包括 Android 控件、音频播放 API 等。步骤如下: 1. 创建 Android Studio 项目 2. 在布局文件中添加音乐播放所需的控件,如播放/暂停按钮、进度条等 3. 使用 MediaPlayer 类播放音乐文件 4. 通过代码实现播放/暂停、快进/快退等功能 5. 运行并测试音乐播放器。 请注意,上述步骤仅是大致流程,详细实现需要更多代码实现。如果您对 Android 开发知识不熟悉,可以参考一些教程或视频。 ### 回答2: Android Studio是开发Android应用程序的主要开发工具。在使用Android Studio时,我们可以很方便地开发一个简易音乐播放器应用程序,下面就来简单介绍一下。 首先,我们需要使用MediaPlayer类来实现基本的音乐播放功能。MediaPlayer类提供各种方法,如start()、pause()、stop()和release()等,以控制音乐播放。我们需要在我们的应用程序中创建一个MediaPlayer对象,然后在适当的时候调用这些方法。 其次,我们需要添加用户界面组件来控制音乐播放。常用的用户界面组件包括Button、SeekBar和TextView等。Button用于播放、暂停和停止音乐,SeekBar用于控制音乐的播放进度,而TextView用于显示歌曲名称、歌手和专辑信息等。 接着,我们需要读取音频文件。我们可以在res/raw文件夹中添加音频文件,然后使用MediaPlayer的setDataSource()方法。我们可以从MediaPlayer对象中获得其持有的音频文件的信息,并将其显示在TextView上。 最后,我们需要处理用户的输入操作。通过设置Button的点击事件,我们可以实现播放、暂停和停止音乐的功能。通过SeekBar的拖动事件,我们可以实现控制音乐播放进度的功能。这些操作会触发相应的MediaPlayer方法调用,从而完成音乐播放器应用程序的开发。 因为Android Studio提供了许多便捷的工具和模板,开发一个简易音乐播放器应用程序并不需要过多的技术难度。只要了解Android开发基础知识,掌握好Java语言,再加上一定的耐心和实践,相信开发一个高质量的音乐播放器应用程序并不是难事。 ### 回答3: Android Studio是全球开发人员使用最为广泛的安卓开发工具之一,而开发一款简易音乐播放器也可以展现出学习Android开发的重要性。下面将从以下几个方面讲解如何使用Android Studio实现一个简易音乐播放器。 1.主页面设计 在主页面设计中,我们可以先添加一个背景图片,然后在页面上添加一个RecyclerView来展示音乐列表,再添加一个FloatingActionButton作为播放按钮。这样的设计可以让用户快速找到自己想要播放的音乐,通过FloatingActionButton可以让用户一键播放音乐。 2.音乐加载与展示 在音乐加载和展示中,我们可以将音乐列表存入一个List集合中,通过RecyclerView进行展示,此外在适配器中还需设置每个Item的点击事件,当用户点击某一项音乐时,就需要实现音乐播放功能。 3.音乐播放 在音乐播放中需要用到MediaPlayer类,它可以实现音乐的播放、暂停、停止等基本功能。在实现播放功能时需要先获得音乐文件的路径,通过MediaPlayer的create()方法创建一个MediaPlayer对象,并调用start()方法开始播放音乐,同时也需要添加监听器来检测播放完成事件。在暂停与播放状态之间的转换需要用到setOnCompletionListener()方法,这个方法会在音乐播放完成时调用,判断音乐播放状态并进行相应的操作。 4.其他功能 除了基本的音乐播放功能,还可以在音乐播放器中添加音量调节、歌词显示、定时停止等扩展功能,这些功能可以让音乐播放器更加实用和人性化。 总之,通过学习Android Studio,我们可以轻松开发出一款简易音乐播放器,为自己的Android开发之路打下坚实的基础。同时,也要注意在实际开发中遵循规范和良好的编码习惯,写出高效、可维护、健壮的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值