安卓课设-音乐播放器

时间:2024-04-15 15:53:16

目录

一 需求分析... 1

1.1用例分析... 1

二 系统设计... 2

三 系统测试... 3

3.1 测试用例... 3

四 系统实现... 4

附件... 11

一 课设题目... 11

二 技术说明... 11

三 UML类图... 12

四 代码... 12

4.1 清单文件... 12

4.2 布局文件... 13

4.2.2 源文件... 14

4.3 Java代码... 22

 

一 需求分析

智能手机需要音乐播放器才能播放手机内的音频文件,本软件具备音乐播放器的播放歌曲、歌曲列表、拖动进度等基本功能,同时本播放器界面简洁美观,操作简单便捷。

1.1用例分析

 

 

(1)  注册

拥有账号才能登录。

(2)  登录

输入注册的账号和密码登录。

(3)  播放器主页

进入播放页面点击列表中的歌曲或者点击播放按钮都可以实现播放。

 

二 系统设计

 

 

音乐播放器由欢迎页面、注册页面、登录页面、播放器主页、退出登录页面组成。

欢迎页面由一个haunying.xml设置background加一个button组成,在haunying.java中用intent跳转登录页面(运用SharedPreferneces存储在下次打开登录页面可以显示保存的账号和密码)。

登录页面由activity_main.xml设置TextView注册,在MainActivity.java设置跳转注册页面。

注册页面由zhuce.xml设置的Button注册按钮,在MainActivity2.java中设置传回注册的账号,然后在登录页面直接显示传回的账号,输入正确密码即可登录。

播放器主页由zhuactivity_main.xml设置了四个Button设置上一曲、播放/暂停、下一曲、退出登录,两个ListView设置当前时间、全部时间,一个Seekbar设置播放进度条。在ZHUMainActivity.java中调用自定义MyAdaper.java方法在Music.Utils.java用SQLite获取到本地数据库的歌曲,然后填充到ListView中,利用安卓MediaPlayer实现歌曲的播放/暂停、上一曲、下一曲的切换。

退出登录页面由exittis.xml设计自定义对话框,在exittis.java设置退出。

三 系统测试

3.1 测试用例

 

 

四 系统实现

 

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_account//账号输入框
   
private EditText et_password; //密码输入框
   
private Button btn_login;     //登录按钮
   
private TextView textView;    //注册按钮
   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //通过SharedPreferences方法获取账号密码
       
Map<String,String> userInfo=Password.getUserInfo(this);
        if (userInfo!=null){
            //将获取的账号显示到界面上
           
et_account.setText(userInfo.get("account"));
            //将获取的密码显示到界面上
           
et_password.setText(userInfo.get("password"));
        }
    }
    private void initView(){
        et_account=(EditText)findViewById(R.id.et_number);
        et_password=(EditText)findViewById(R.id.et_password);
        btn_login=(Button)findViewById(R.id.btn_login);
        textView=(TextView)findViewById(R.id.textview);
        btn_login.setOnClickListener(this);
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_login:
                String ZH="admin";
                String MM="admin";
                //点击登录按钮,获取界面上的账号和密码
               
String account1=et_account.getText().toString().trim();
                String password1=et_password.getText().toString();
                //检测账号是否为空
               
if (TextUtils.isEmpty(account1)){
                    Toast.makeText(this,"请输入账号", Toast.LENGTH_LONG).show();
                    return;
                }
                //检测密码是否为空
               
if (TextUtils.isEmpty(password1)){
                    if (ZH.equals(account1)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_LONG).show();}
                    else{
                        Toast.makeText(this,"无效账号,请先注册",Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
                //检测输入的账号和密码是否正确
               
if (ZH.equals(account1)&&MM.equals(password1)){
                    Intent intent1=new Intent(MainActivity.this,ZHUMainActivity.class);
                    startActivity(intent1);
                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    public void  click(View view){
                    Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                    startActivityForResult(intent,1);
        }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode==1&&resultCode==2){
            String acquiredData=data.getStringExtra("zczh");
            et_account.setText(acquiredData);
        }
    }
}

 

 private Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            // 展示给进度条和当前时间
           
int progress = mediaPlayer.getCurrentPosition();
            mSeekBar.setProgress(progress);
            mCurrentTimeTv.setText(parseTime(progress));
            // 继续定时发送数据
           
updateProgress();
            return true;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuactivity_main);
        mediaPlayer = new MediaPlayer();
        mPreviousBtn= (Button) findViewById(R.id.mPreviousBtn);
        mPlayBtn= (Button) findViewById(R.id.mPlayBtn);
        mNextBtn= (Button) findViewById(R.id.mNextBtn);
        bt1=(Button)findViewById(R.id.bt1);
        mCurrentTimeTv= (TextView) findViewById(R.id.current_time_tv);
        mTotalTimeTv= (TextView) findViewById(R.id.total_time_tv);
        mSeekBar= (SeekBar) findViewById(R.id.seek_bar);
        mSeekBar.setOnSeekBarChangeListener(this);
        mPreviousBtn.setOnClickListener(this);
        mPlayBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
        initView();
    }
    /**
     * 初始化view
     */
   
private void initView() {
        mListView = (ListView) findViewById(R.id.main_listview);
        list = new ArrayList<>();
        //把扫描到的音乐赋值给list
       
list = MusicUtils.getMusicData(this);
        adapter = new MyAdapter(this,list);
        mListView.setAdapter(adapter);
        mediaPlayer.setOnCompletionListener(this);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mCurrentPosition=i;
                changeMusic(mCurrentPosition);
            }
        });
    }
    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.mPreviousBtn){//上一曲
           
changeMusic(--mCurrentPosition);
        }else if (view.getId()==R.id.mPlayBtn){//暂停/播放
            // 首次点击播放按钮,默认播放第0首
           
if (mediaPlayer==null){
                changeMusic(0);
            }else {
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }else {
                    mediaPlayer.start();
                }
            }
        }else if (view.getId()==R.id.mNextBtn){// 下一首
           
changeMusic(++mCurrentPosition);
        }
    }

    // 基本数据类型和String在方法调用中传递的是值
    // 其他数据类型在方法调用中传递的是引用

    //切歌
   
private void changeMusic(int position){
        if (position<0){
            mCurrentPosition = position =list.size()-1;
        }else if (position>list.size()-1){
            mCurrentPosition = position=0;
        }

        if (mediaPlayer==null){
            mediaPlayer=new MediaPlayer();
        }

        try {
            // 切歌之前先重置,释放掉之前的资源
           
mediaPlayer.reset();
            // 设置播放源
           
mediaPlayer.setDataSource(list.get(position).path);
            // 开始播放前的准备工作,加载多媒体资源,获取相关信息
           
mediaPlayer.prepare();

            // 开始播放
           
mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 切歌时重置进度条并展示歌曲时长
       
mSeekBar.setProgress(0);
        mSeekBar.setMax(mediaPlayer.getDuration());
        mTotalTimeTv.setText(parseTime(mediaPlayer.getDuration()));

        updateProgress();
    }

    private void updateProgress() {
        // 使用Handler每间隔1s发送一次空消息,通知进度条更新
       
Message msg = Message.obtain();// 获取一个现成的消息
        // 使用MediaPlayer获取当前播放时间除以总时间的进度
       
int progress = mediaPlayer.getCurrentPosition();
        msg.arg1 = progress;
        mHandler.sendMessageDelayed(msg, INTERNAL_TIME);
    }

    private String parseTime(int oldTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");// 时间格式
       
String newTime = sdf.format(new Date(oldTime));
        return newTime;
    }

    private static final int INTERNAL_TIME = 1000;// 音乐进度间隔时间
   
@Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }
    // 当手停止拖拽进度条时执行该方法
    // 获取拖拽进度
    // 将进度对应设置给MediaPlayer
   
@Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        mediaPlayer.seekTo(progress);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        changeMusic(++mCurrentPosition);
    }
    public void clickk(View view) {
        final exitthis dialog = new exitthis(ZHUMainActivity.this);
        dialog.setTitle("提示");
        dialog.setMessage("是否退出登录?");
        dialog.setNegtive("取消");
        dialog.setPositive("确定");
        dialog.setOnClickBottomListener(new exitthis.OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                dialog.dismiss();
                ZHUMainActivity.this.finish();
            }

            @Override
            public void onNegtiveClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

附件

一 课设题目

   安卓音乐播放器

二 技术说明

  1. 欢迎页、注册页、登录页、播放器主页、退出登录页五个UI界面
  2. 实现登录和注册互跳转,登录页跳转到播放器页,退出登陆页跳转登录页。
  3. 播放器使用mediaplayer实现播放/暂停、上一曲、下一曲
  4. 在登录页面使用SharedPreferences存储实现记住账号和密码功能。
  5. 获取的歌曲列表用ListView显示。
  6. seekbar歌曲进度条的功能需求有两部分:一是手动拖动进度条,使播放器从当前位置播放并更新当前时间,这里用进度条OnSeekBarChangeListener监听;二是实现自动更新进度条进度,要求进度条进度与播放器进度一致,实现联动,这里用线程或者广播消息实现。

三 UML类图

 

 

四 代码

4.1 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="cn.itcase.dmtplayer">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <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=".huanying">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity2">
        </activity>
        <activity android:name=".MainActivity">
        </activity>
        /activity>
        <activity android:name=".ZHUMainActivity">
        </activity>
    </application>
</manifest>

4.2 布局文件

4.2.1截图

 

 

 

 

 

 

 

 

 

 

4.2.2 源文件

欢迎页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:background="@drawable/tt"
   
tools:context=".MainActivity">
    <Button
       
android:id="@+id/huan"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="450dp"
       
android:onClick="cllick"
       
android:text="立即体验"
       
android:textSize="25sp"
       
android:layout_centerHorizontal="true"
       
/>


</RelativeLayout>

 

登录页面

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

    <ImageView
       
android:id="@+id/iv_head"
       
android:layout_width="80dp"
       
android:layout_height="80dp"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="160dp"
       
android:src="@drawable/tx"/>

    <LinearLayout
       
android:id="@+id/layout"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/iv_head"
       
android:layout_margin="10dp"
       
android:background="#ffffff"
       
android:orientation="vertical" >

        <RelativeLayout
           
android:id="@+id/rl_username"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_margin="10dp" >

            <TextView
               
android:id="@+id/tv_name"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:layout_centerVertical="true"
               
android:text="账号" />

            <EditText
               
android:id="@+id/et_number"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:layout_marginLeft="5dp"
               
android:layout_toRightOf="@+id/tv_name"
               
android:background="@null" />
        </RelativeLayout>

        <View
           
android:layout_width="match_parent"
           
android:layout_height="2dp"
           
android:background="#E6E6E6" />

        <RelativeLayout
           
android:id="@+id/rl_userpsw"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_margin="10dp" >

            <TextView
               
android:id="@+id/tv_psw"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
                
android:layout_centerVertical="true"
               
android:text="密码" />

            <EditText
               
android:id="@+id/et_password"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
                
android:layout_marginLeft="5dp"
               
android:layout_toRightOf="@+id/tv_psw"
               
android:inputType="textPassword"
               
android:background="@null" />
        </RelativeLayout>
    </LinearLayout>

    <Button
       
android:id="@+id/btn_login"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/layout"
       
android:layout_centerHorizontal="true"
       
android:layout_marginLeft="10dp"
       
android:layout_marginRight="10dp"
       
android:layout_marginTop="20dp"
       
android:background="#3C8DC4"
       
android:text="登录"
       
android:textColor="#ffffff" />
    <TextView
       
android:id="@+id/textview"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="注册"
       
android:textColor="#3e82bf"
       
android:layout_below="@id/btn_login"
       
android:layout_marginTop="60dp"
       
android:layout_centerHorizontal="true"
       
android:onClick="click"
       
>
    </TextView>

</RelativeLayout>

注册页面

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

    <ImageView
       
android:id="@+id/iv_head"
       
android:layout_width="80dp"
       
android:layout_height="80dp"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="160dp"
       
android:src="@drawable/tx"/>

    <LinearLayout
       
android:id="@+id/layout"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/iv_head"
       
android:layout_margin="10dp"
       
android:background="#ffffff"
       
android:orientation="vertical" >

        <RelativeLayout
           
android:id="@+id/rl_username"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_margin="10dp" >

            <TextView
               
android:id="@+id/tv_name"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:layout_centerVertical="true"
               
android:text="账号" />

            <EditText
               
android:id="@+id/et_number"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:layout_marginLeft="5dp"
               
android:layout_toRightOf="@+id/tv_name"
               
android:background="@null" />
        </RelativeLayout>

        <View
           
android:layout_width="match_parent"
           
android:layout_height="2dp"
           
android:background="#E6E6E6" />

        <RelativeLayout
           
android:id="@+id/rl_userpsw"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_margin="10dp" >

            <TextView
               
android:id="@+id/tv_psw"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:layout_centerVertical="true"
               
android:text="密码" />

            <EditText
               
android:id="@+id/et_password"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:layout_marginLeft="5dp"
               
android:layout_toRightOf="@+id/tv_psw"
               
android:inputType="textPassword"
               
android:background="@null" />
        </RelativeLayout>
    </LinearLayout>

    <Button
       
android:id="@+id/btn_login"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/layout"
       
android:layout_centerHorizontal="true"
       
android:layout_marginLeft="10dp"
       
android:layout_marginRight="10dp"
       
android:layout_marginTop="20dp"
       
android:background="#3C8DC4"
       
android:text="注册"
       
android:textColor="#ffffff" />

</RelativeLayout>

播放器主页

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ZHUMainActivity">

    <LinearLayout
       
android:orientation="horizontal"
       
android:id="@+id/main_textview"
       
android:layout_width="match_parent"
       
android:layout_height="40dp">
        <Button
           
android:id="@+id/mPreviousBtn"
           
android:text="上一曲"
           
android:layout_width="0dp"
           
android:layout_weight="1"
           
android:layout_height="match_parent" />
        <Button
           
android:id="@+id/mPlayBtn"
           
android:text="播放/暂停"
           
android:layout_width="0dp"
           
android:layout_weight="1"
           
android:layout_height="match_parent" />
        <Button
           
android:id="@+id/mNextBtn"
           
android:text="下一曲"
           
android:layout_width="0dp"
           
android:layout_weight="1"
           
android:layout_height="match_parent" />
    </LinearLayout>

    <RelativeLayout
       
android:id="@+id/main_textview1"
       
android:layout_below="@id/main_textview"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">

        <TextView
           
android:id="@+id/current_time_tv"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="当前时间"/>

        <TextView
           
android:id="@+id/total_time_tv"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:layout_alignParentRight="true"
           
android:text="全部时间"/>

        <SeekBar
           
android:id="@+id/seek_bar"
           
style="?android:progressBarStyleHorizontal"
           
android:layout_width="match_parent"
           
android:layout_height="50dp"
           
android:layout_toLeftOf="@id/total_time_tv"
           
android:layout_toRightOf="@id/current_time_tv"/>

    </RelativeLayout>
<LinearLayout
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:orientation="vertical"
   
android:layout_marginTop="80dp"
   
>

    <ListView
       
android:id="@+id/main_listview"
       
android:layout_width="match_parent"
       
android:layout_height="0dp"
       
android:layout_below="@+id/main_textview1"
       
android:layout_weight="9"
       
/>

    <Button
       
android:id="@+id/bt1"
       
android:onClick="clickk"
       
android:layout_width="match_parent"
       
android:layout_height="0dp"
       
android:layout_weight="2"
       
android:text="退出登录"
       
/>
</LinearLayout>
</RelativeLayout>

退出登录页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
>
    <LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:paddingTop="16dp"
       
android:orientation="vertical"
       
>
        <TextView
           
android:id="@+id/title"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:gravity="center"
           
android:visibility="visible"
           
android:textColor="#333333"
           
android:textSize="18sp"
           
android:layout_marginBottom="16dp"
           
/>
        <TextView
            
android:id="@+id/message"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:gravity="center"
           
android:layout_marginLeft="20dp"
           
android:layout_marginRight="20dp"
           
android:textColor="#999999"
           
android:textSize="14sp"
           
/>
    </LinearLayout>
<LinearLayout
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:orientation="horizontal"
   
>
    <Button
       
android:id="@+id/netive"
       
android:layout_width="0dp"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="10dp"
       
android:paddingTop="16dp"
       
android:paddingBottom="16dp"
       
android:layout_weight="1"
       
android:gravity="center"
       
android:textColor="#999999"
       
android:textSize="16sp"
       
/>
    <Button
   
android:id="@+id/positive"
   
android:layout_width="0dp"
   
android:layout_height="wrap_content"
   
android:layout_marginLeft="10dp"
   
android:paddingTop="16dp"
   
android:paddingBottom="16dp"
   
android:layout_weight="1"
   
android:gravity="center"
   
android:textColor="#38ADFF"
   
android:textSize="16sp"
   
/>
</LinearLayout>

</LinearLayout>

4.3 Java代码

Mainactivity.java

package cn.itcase.dmtplayer;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_account//账号输入框
   
private EditText et_password; //密码输入框
   
private Button btn_login;     //登录按钮
   
private TextView textView;    //注册按钮
   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //通过SharedPreferences方法获取账号密码
       
Map<String,String> userInfo=Password.getUserInfo(this);
        if (userInfo!=null){
            //将获取的账号显示到界面上
           
et_account.setText(userInfo.get("account"));
            //将获取的密码显示到界面上
           
et_password.setText(userInfo.get("password"));
        }
    }
    private void initView(){
        et_account=(EditText)findViewById(R.id.et_number);
        et_password=(EditText)findViewById(R.id.et_password);
        btn_login=(Button)findViewById(R.id.btn_login);
        textView=(TextView)findViewById(R.id.textview);
        btn_login.setOnClickListener(this);
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_login:
                String ZH="admin";
                String MM="admin";
                //点击登录按钮,获取界面上的账号和密码
               
String account1=et_account.getText().toString().trim();
                String password1=et_password.getText().toString();
                //检测账号是否为空
               
if (TextUtils.isEmpty(account1)){
                    Toast.makeText(this,"请输入账号", Toast.LENGTH_LONG).show();
                    return;
                }
                //检测密码是否为空
               
if (TextUtils.isEmpty(password1)){
                    if (ZH.equals(account1)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_LONG).show();}
                    else{
                        Toast.makeText(this,"无效账号,请先注册",Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
                //检测输入的账号和密码是否正确
               
if (ZH.equals(account1)&&MM.equals(password1)){
                    Intent intent1=new Intent(MainActivity.this,ZHUMainActivity.class);
                    startActivity(intent1);
                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    public void  click(View view){
                    Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                    startActivityForResult(intent,1);
        }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode==1&&resultCode==2){
            String acquiredData=data.getStringExtra("zczh");
            et_account.setText(acquiredData);
        }
    }
}

Mainactivity2.java

package cn.itcase.dmtplayer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
    private EditText et_account//账号输入框
   
private EditText et_password; //密码输入框
   
private Button btn_login;     //注册按钮
   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce);
        initView();
        //通过SharedPreferences方法获取账号密码
       
Map<String,String> userInfo=Password.getUserInfo(this);
        if (userInfo!=null){
            //将获取的账号显示到界面上
           
et_account.setText(userInfo.get("account"));
            //将获取的密码显示到界面上
           
et_password.setText(userInfo.get("password"));
        }
    }
    private void initView(){
        et_account=(EditText)findViewById(R.id.et_number);
        et_password=(EditText)findViewById(R.id.et_password);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_login:
                //点击注册按钮,获取界面上的账号和密码
               
String account=et_account.getText().toString().trim();
                String password=et_password.getText().toString();
                //检测账号是否为空
               
if (TextUtils.isEmpty(account)){
                    Toast.makeText(this,"请输入账号", Toast.LENGTH_LONG).show();
                    return;
                }
                //检测密码是否为空
               
if (TextUtils.isEmpty(password)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_LONG).show();
                    return;
                }
                //保存注册的账号和密码
                    
boolean isSaveSuccess=Password.saveUserInfor(this,account,password);
                    if (isSaveSuccess){
                        Toast.makeText(this,"注册成功",Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(this,"注册失败",Toast.LENGTH_LONG).show();
                    }
                        Intent intent=new Intent();
                    intent.putExtra("zczh",account);
                    setResult(2,intent);
                    finish();
                    break;
        }
    }
}

haunying.java

package cn.itcase.dmtplayer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class huanying extends AppCompatActivity {
private Button huan;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.huanying);
        huan=(Button)findViewById(R.id.huan);
}
public void cllick(View view){
    Intent intent=new Intent(huanying.this,MainActivity.class);
    startActivity(intent);
}
}

ZHUMainactivity.java

package cn.itcase.dmtplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ZHUMainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, MediaPlayer.OnCompletionListener {

    private MediaPlayer mediaPlayer;//播放音频的
   
private Button mPreviousBtn;
    private Button mPlayBtn;
    private Button mNextBtn;
    private Button bt1;
    private ListView mListView;
    private List<Song> list;
    private MyAdapter adapter;
    private SeekBar mSeekBar;
    private TextView mCurrentTimeTv;
    private TextView mTotalTimeTv;
    // 记录当前播放歌曲的位置
   
private int mCurrentPosition;

    private Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            // 展示给进度条和当前时间
           
int progress = mediaPlayer.getCurrentPosition();
            mSeekBar.setProgress(progress);
            mCurrentTimeTv.setText(parseTime(progress));
            // 继续定时发送数据
           
updateProgress();
            return true;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuactivity_main);
        mediaPlayer = new MediaPlayer();
        mPreviousBtn= (Button) findViewById(R.id.mPreviousBtn);
        mPlayBtn= (Button) findViewById(R.id.mPlayBtn);
        mNextBtn= (Button) findViewById(R.id.mNextBtn);
        bt1=(Button)findViewById(R.id.bt1);
        mCurrentTimeTv= (TextView) findViewById(R.id.current_time_tv);
        mTotalTimeTv= (TextView) findViewById(R.id.total_time_tv);
        mSeekBar= (SeekBar) findViewById(R.id.seek_bar);
        mSeekBar.setOnSeekBarChangeListener(this);
        mPreviousBtn.setOnClickListener(this);
        mPlayBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
        initView();
    }
    /**
     *
初始化view
     */
   
private void initView() {
        mListView = (ListView) findViewById(R.id.main_listview);
        list = new ArrayList<>();
        //把扫描到的音乐赋值给list
       
list = MusicUtils.getMusicData(this);
        adapter = new MyAdapter(this,list);
        mListView.setAdapter(adapter);
        mediaPlayer.setOnCompletionListener(this);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mCurrentPosition=i;
                changeMusic(mCurrentPosition);
            }
        });
    }
    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.mPreviousBtn){//上一曲
           
changeMusic(--mCurrentPosition);
        }else if (view.getId()==R.id.mPlayBtn){//暂停/播放
            // 首次点击播放按钮,默认播放第0首
           
if (mediaPlayer==null){
                changeMusic(0);
            }else {
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }else {
                    mediaPlayer.start();
                }
            }
        }else if (view.getId()==R.id.mNextBtn){// 下一首
           
changeMusic(++mCurrentPosition);
        }
    }

    // 基本数据类型和String在方法调用中传递的是值
    // 其他数据类型在方法调用中传递的是引用

    //切歌
   
private void changeMusic(int position){
        if (position<0){
            mCurrentPosition = position =list.size()-1;
        }else if (position>list.size()-1){
            mCurrentPosition = position=0;
        }

        if (mediaPlayer==null){
            mediaPlayer=new MediaPlayer();
        }

        try {
            // 切歌之前先重置,释放掉之前的资源
           
mediaPlayer.reset();
            // 设置播放源
           
mediaPlayer.setDataSource(list.get(position).path);
            // 开始播放前的准备工作,加载多媒体资源,获取相关信息
           
mediaPlayer.prepare();

            // 开始播放
           
mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 切歌时重置进度条并展示歌曲时长
       
mSeekBar.setProgress(0);
        mSeekBar.setMax(mediaPlayer.getDuration());
        mTotalTimeTv.setText(parseTime(mediaPlayer.getDuration()));

        updateProgress();
    }

    private void updateProgress() {
        // 使用Handler每间隔1s发送一次空消息,通知进度条更新
       
Message msg = Message.obtain();// 获取一个现成的消息
        // 使用MediaPlayer获取当前播放时间除以总时间的进度
       
int progress = mediaPlayer.getCurrentPosition();
        msg.arg1 = progress;
        mHandler.sendMessageDelayed(msg, INTERNAL_TIME);
    }

    private String parseTime(int oldTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");// 时间格式
       
String newTime = sdf.format(new Date(oldTime));
        return newTime;
    }

    private static final int INTERNAL_TIME = 1000;// 音乐进度间隔时间
   
@Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }
    // 当手停止拖拽进度条时执行该方法
    // 获取拖拽进度
    // 将进度对应设置给MediaPlayer
   
@Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        mediaPlayer.seekTo(progress);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        changeMusic(++mCurrentPosition);
    }
    public void clickk(View view) {
        final exitthis dialog = new exitthis(ZHUMainActivity.this);
        dialog.setTitle("提示");
        dialog.setMessage("是否退出登录?");
        dialog.setNegtive("取消");
        dialog.setPositive("确定");
        dialog.setOnClickBottomListener(new exitthis.OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                dialog.dismiss();
                ZHUMainActivity.this.finish();
            }

            @Override
            public void onNegtiveClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

MusicUtils.java

package cn.itcase.dmtplayer;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

/*
        *
音乐工具类,
        */
public class MusicUtils {
    /**
     *
扫描系统里面的音频文件,返回一个list集合
     */
   
public static List<Song> getMusicData(Context context) {
        List<Song> list = new ArrayList<Song>();
        // 媒体库查询语句(写一个工具类MusicUtils)
       
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                null, MediaStore.Audio.AudioColumns.IS_MUSIC);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Song song = new Song();
                song.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                if (song.size > 1000 * 800) {
                    // 注释部分是切割标题,分离出歌曲名和歌手 (本地媒体库读取的歌曲信息不规范)
                   
if (song.song.contains("-")) {
                        String[] str = song.song.split("-");
                        song.singer = str[0];
                        song.song = str[1];
                    }
                    list.add(song);
                }
            }
            // 释放资源
           
cursor.close();
        }
        return list;
    }

    /**
     *
定义一个方法用来格式化获取到的时间
     */
   
public static String formatTime(int time) {
        if (time / 1000 % 60 < 10) {
            return time / 1000 / 60 + ":0" + time / 1000 % 60;

        } else {
            return time / 1000 / 60 + ":" + time / 1000 % 60;
        }

    }
}

Password.java

package cn.itcase.dmtplayer;

import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;

public class Password{
    //保存账号和密码到data.xml文件中
   
public static  boolean saveUserInfor(Context context, String account, String password){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sp.edit();
        edit.putString("userName",account);
        edit.putString("pwd",password);
        edit.commit();
        return  true;
}
//从data.xml文件中获取存储的账号和密码
   
public static Map<String,String> getUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String account =sp.getString("userName",null);
        String password=sp.getString("pwd",null);
        Map<String,String>userMap=new HashMap<String, String>();
        userMap.put("account",account);
        userMap.put("password",password);
        return userMap;
    }
}

song.java

package cn.itcase.dmtplayer;

/**
 *
放置音乐
 */
class Song {
    /**
     *
歌手
     */
   
public String singer;
    /**
     *
歌曲名
     */
   
public String song;
    /**
     *
歌曲的地址
     */
   
public String path;
    /**
     *
歌曲长度
     */
   
public int duration;
    /**
     *
歌曲的大小
     */
   
public long size;
}

exittis.java

package cn.itcase.dmtplayer;


import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public  class exitthis extends AlertDialog {
    private TextView titleTv;
    private  TextView messageTv;
    private Button negtiveBn,positiveBn;
    public exitthis(Context context) {
        super(context);
    }
    private String message;
    private String title;
    private  String  positive,negtive;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exitthis);
        negtiveBn=(Button)findViewById(R.id.netive);
        positiveBn=(Button)findViewById(R.id.positive);
        titleTv=(TextView)findViewById(R.id.title);
        messageTv=(TextView)findViewById(R.id.message);
        initEvent();
    }
    public void refreshView(){
        if (!TextUtils.isEmpty(title)){
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        }else{
            titleTv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(message)){
            messageTv.setText(message);
        }
        if (!TextUtils.isEmpty(positive)){
            positiveBn.setText(positive);
        }else{
            positiveBn.setText("确定");
        }
        if (!TextUtils.isEmpty(negtive)){
            negtiveBn.setText(negtive);
        }else{
            negtiveBn.setText("取消");
        }
    }
    private void initEvent(){
        positiveBn.setOnClickListener(new View.OnClickListener(){
            public void  onClick(View v){
                if (onClickBottomListener!=null){
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        negtiveBn.setOnClickListener(new View.OnClickListener(){
            public void  onClick(View v){
                if (onClickBottomListener!=null){
                    onClickBottomListener.onNegtiveClick();
                }
            }
        });
    }
    public void show(){
        super.show();
        refreshView();
    }
    public interface OnClickBottomListener{
        void onPositiveClick();
        void onNegtiveClick();
    }
    public OnClickBottomListener onClickBottomListener;
    public exitthis setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        this.onClickBottomListener=onClickBottomListener;
        return this;
    }
    public exitthis setMessage(String message){
        this.message=message;
        return this;
    }
    public  exitthis setTitle(String title){
        this.title=title;
        return this;
    }
    public  exitthis setNegtive(String negtive){
        this.negtive=negtive;
        return this;
    }
    public  exitthis setPositive(String positive){
        this.positive=positive;
        return this;
    }
}