当前位置:   article > 正文

Android Studio音乐播放器(使用sqlite创建数据库)_音乐播放器android studio

音乐播放器android studio

一个简单的音乐播放器,画面做的比较简洁,适合初学者,其中使用sqlite创建数据库用于存储用户信息,同时还添加了用户信息显示功能,可查看用户信息。

Android Studio音乐播放器设计报告

姓名:阿三                              学院:信息工程学院

专业:软件工程                             学号:2111111111

任务起止日期:2023.12.27-2024.1.2

课程设计题目:基于Android Studio的音乐播放系统设计与实现

一、问题描述

音视频播放系统包括:使用sqlite创建数据库用于存储用户信息、启动效果、用户注册、用户登录、显示用户信息、音乐播放等功能。

二、功能描述

设计一个音乐播放器app,使之能够实现以下功能:

创建一个数据库用于存储用户信息。
启动效果:该App设计了启动页面,启动后倒计时3s后可进入app。
用户登录注册:该App提供用户登录和注册功能,用户可以创建新账户或使用现有账户登录。当用户注册完成后跳转回到登录页面进行用户登录。用户需要提供有效的用户名和密码进行身份验证。
显示用户信息:用户登录后可点击信息按钮查看用户信息,包括历史登录用户的用户信息。
音乐播放功能:用户登录后,可以通过该App浏览和播放音乐文件。App提供音乐列表,用户可以浏览并选择要播放的音乐。点击音乐后,将跳转到音乐播放页面,可进行音乐的播放、暂停、上一曲和下一曲以及拖动进度条来控制音乐播放。
三、功能实现

项目目录:

1.创建数据库,建立一个用户表,表结构如下:

2.实现app启动页面,页面效果如下:

3.实现用户登录功能,用户需要提供有效的用户名和密码,否侧提示用户名无效或密码无效:

4.实现用户注册功能,点击注册按钮跳转至用户注册页面,用户注册成功跳转回用户登录页面进行用户登录:

5.显示历史登录用户信息功能,登录成功后,点击信息后,跳转至用户信息显示界面,用户可查看历史登录用户信息。

6.实现音乐播放功能,登录成功后,显示音乐列表,点击音乐后,跳转至音乐播放界面,用户可通过点击播放按钮进行音乐的播放、暂停、上一曲和下一曲操作,同时用户也可以通过拖动进度条控制音乐的播放进度。

四、源代码

1.用户类定义User.java

  1. package and.yjg.music_app.Login;
  2. public class User {
  3.     public User() {
  4.     }
  5.     public User(String account, String password, String phone, String address) {
  6.         this.account = account;
  7.         this.password = password;
  8.         this.phone = phone;
  9.         this.address = address;
  10.     }
  11.     public String account;
  12.     public String password;
  13.     public String phone;
  14.     public String address;
  15.     public String getAccount() {
  16.         return account;
  17.     }
  18.     public void setAccount(String account) {
  19.         this.account = account;
  20.     }
  21.     public String getPassword() {
  22.         return password;
  23.     }
  24.     public void setPassword(String password) {
  25.         this.password = password;
  26.     }
  27.     public void setPhone(String phone) {
  28.         this.phone = phone;
  29.     }
  30.         public void setAddress(String address){
  31.             this.address = address;
  32.         }
  33.         public String toString () {
  34.             return "用户名:" + account + "\n" +
  35.                     "密码:" + password + "\n " +
  36.                     "电话:" + phone + "\n " +
  37.                     "地址:" + address + "\n";
  38.         }
  39.     }

2.UserDao.java

  1. package and.yjg.music_app.Login;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteException;
  7. import android.util.Log;
  8. import and.yjg.music_app.DataBaseHelper;
  9. public class UserDao {
  10.     private Context context;       
  11.     private DataBaseHelper dbHelper;
  12.     private SQLiteDatabase db;   
  13.     public UserDao(Context context) {
  14.         this.context = context;
  15.     }
  16.     public void open() throws SQLiteException {
  17.         dbHelper = new DataBaseHelper(context);
  18.         try {
  19.             db = dbHelper.getWritableDatabase();
  20.         } catch (SQLiteException exception) {
  21.             db = dbHelper.getReadableDatabase();
  22.         }
  23.     }
  24.     public void close() {
  25.         if (db != null) {
  26.             db.close();
  27.             db = null;
  28.         }
  29.     }
  30.     public void addUser(User user) {
  31.         ContentValues values = new ContentValues();
  32.         values.put("account", user.account);
  33.         values.put("password", user.password);
  34.         values.put("phone", user.phone);
  35.         values.put("address", user.address);
  36.         db.insert("user", null, values);
  37.     }
  38.     public void deleteUser(User user) {
  39.         db.delete("user", "account = ?", new String[]{user.account});
  40.     }
  41.     public void update(User user) {
  42.         ContentValues values = new ContentValues();
  43.         values.put("password", user.password);
  44.         db.update("user", values, "account = ?", new String[]{user.account});
  45.     }
  46.     public boolean find(User user) {
  47.         Cursor cursor = db.query("user", null, "account = ?", new String[]{user.account}, null, null, null);
  48.         if (cursor == null || cursor.getCount() < 1) {
  49.             return false;
  50.         }
  51.         if (cursor.moveToFirst()) {
  52.             do {
  53.                 String acc = cursor.getString(cursor.getColumnIndex("account"));
  54.                 String pass = cursor.getString(cursor.getColumnIndex("password"));
  55.                 String pho = cursor.getString(cursor.getColumnIndex("phone"));
  56.                 String addr = cursor.getString(cursor.getColumnIndex("address"));
  57.                 Log.d("UserDao", "user account is" + acc);
  58.                 Log.d("UserDao", "user password is " + pass);
  59.                 Log.d("UserDao", "user phone is " + pho);
  60.                 Log.d("UserDao", "user address is " + addr);
  61.             } while (cursor.moveToNext());
  62.         }
  63.         cursor.close();
  64.         return true;
  65.     }
  66.     public boolean isExist(String account) {
  67.         Cursor cursor = db.query("user", null, "account = ?", new String[] {account}, null, null, null);
  68.         return cursor != null && cursor.getCount() > 0;
  69.     }
  70.     public String getPassword(String account) {
  71.         Cursor cursor = db.query("user", null, "account = ?", new String[] {account}, null, null, null);
  72.         cursor.moveToFirst();
  73.         String password = cursor.getString(cursor.getColumnIndex("password"));
  74.         return password;
  75.     }

3.启动效果WelcomeActivity.java

  1. package and.yjg.music_app; 
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.widget.TextView;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import Login.LoginActivity;
  8. public class WelcomeActivity extends AppCompatActivity {
  9.     private TextView tvCountdown;
  10.     private CountDownTimer countDownTimer;
  11.     private long timeLeftInMillis = 3000;
  12.     protected void onCreate(Bundle savedInstanceState){
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_welcome);
  15.         tvCountdown = findViewById(R.id.tv_countdown);
  16.         startCountdown();
  17.     }
  18.     private void startCountdown() {
  19.         countDownTimer = new CountDownTimer(timeLeftInMillis,1000){
  20.             public void onTick(long millisUntilFinished){
  21.                 timeLeftInMillis = millisUntilFinished;
  22.                 int secondsRemaining = (int) (millisUntilFinished/1000);
  23.                 tvCountdown.setText(secondsRemaining+"s");
  24.             }
  25.             public void onFinish(){
  26.                 startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
  27.                 finish();
  28.             }
  29.         }.start();
  30.     }
  31. protected  void onDestroy(){
  32.         super.onDestroy();
  33.         if (countDownTimer != null){
  34.             countDownTimer.cancel();
  35.         }
  36.     }
  37. }

4.用户登录LoginActivity.java

  1. package Login;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.Toast;
  8. import androidx.appcompat.app.ActionBar;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import and.yjg.music_app.MainActivity;
  11. import and.yjg.music_app.R;
  12. public class LoginActivity extends AppCompatActivity {
  13.     private Button btn_login;    
  14.     private Button btn_register;
  15.     private EditText et_account;
  16.     private EditText et_password;
  17.     private UserDao dao;        
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_login);
  22.         initView();
  23.     }
  24.     public void initView() {
  25.         ActionBar actionBar = getSupportActionBar();
  26.         if(actionBar != null){
  27.             actionBar.hide();
  28.         }
  29.         btn_login = findViewById(R.id.btn_login);
  30.         btn_register = findViewById(R.id.btn_register);
  31.         et_account = findViewById(R.id.et_account);
  32.         et_password = findViewById(R.id.et_password);
  33.         btn_login.setOnClickListener(new View.OnClickListener() {
  34.             @Override
  35.             public void onClick(View v) {
  36.                 String acc = et_account.getText().toString().trim();
  37.                 String pass = et_password.getText().toString().trim();
  38.                 dao = new UserDao(getApplicationContext());
  39.                 dao.open();
  40.                 if (dao.isExist(acc) == false) {
  41.                     Toast.makeText(LoginActivity.this,"账号不存在,请重新输入!", Toast.LENGTH_SHORT).show();
  42.                 } else {
  43.                     if (dao.getPassword(acc).equals(pass)) {
  44.                         Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  45.                         startActivity(intent);
  46.                         finish();
  47.                     } else {
  48.                         Toast.makeText(LoginActivity.this, "密码错误,请重新输入!", Toast.LENGTH_SHORT).show();
  49.                     }
  50.                 }
  51.                 dao.close();
  52.             }
  53.         });
  54.         btn_register.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View v) {
  57.                 Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  58.                 startActivityForResult(intent,1);
  59.             }
  60.         });
  61.     }
  62.     @Override
  63.     protected void onActivityResult(int requestCode, int resultCode, Intent data){
  64.         super.onActivityResult(requestCode, resultCode, data);
  65.         if(data != null){
  66.             if(requestCode == 1 && resultCode == 1){
  67.                 String name = data.getStringExtra("acc");
  68.                 String password = data.getStringExtra("pass");
  69.                 et_account.setText(name);
  70.                 et_password.setText(password);
  71.             }
  72.         }

五、各类布局文件

  activity_welcom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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.     tools:context=".WelcomeActivity">
  8.     <TextView
  9.         android:id="@+id/tv_countdown"
  10.         android:layout_width="100dp"
  11.         android:layout_height="60dp"
  12.         android:layout_alignParentRight="true"
  13.         android:layout_alignParentBottom="true"
  14.         android:layout_marginRight="6dp"
  15.         android:layout_marginBottom="89dp"
  16.         android:background="@color/Blue"
  17.         android:gravity="center"
  18.         android:text="3s"
  19.         android:textSize="30dp" />
  20.     <ImageView
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content"
  23.         android:src="@mipmap/action" />
  24. </RelativeLayout>

  activity_login.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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:background="@color/White"
  8.     android:orientation="vertical">
  9.     <ImageView
  10.         android:id='@+id/iv'
  11.         android:layout_width="match_parent"
  12.         android:layout_height="30dp"
  13.         android:layout_centerHorizontal="true"
  14.         android:layout_marginTop="0dp"
  15.         android:background="@color/Black"/>
  16.     <LinearLayout
  17.         android:id="@+id/account"
  18.         android:layout_width="match_parent"
  19.         android:layout_height="wrap_content"
  20.         android:layout_below="@id/iv"
  21.         android:layout_centerVertical="true"
  22.         android:layout_marginBottom="5dp"
  23.         android:layout_marginLeft="10dp"
  24.         android:layout_marginRight="10dp"
  25.         android:layout_marginTop="40dp"
  26.         android:orientation="horizontal">
  27.         <TextView
  28.             android:id="@+id/tv_account"
  29.             android:layout_width="wrap_content"
  30.             android:layout_height="wrap_content"
  31.             android:padding="10dp"
  32.             android:text="账 号"
  33.             android:textColor="#000"
  34.             android:background="@drawable/text_style"
  35.             android:textSize="25sp" />
  36.         <View
  37.             android:layout_width="1dp"
  38.             android:layout_height="match_parent"
  39.             android:background="@color/Gray"/>
  40.         <EditText
  41.             android:id="@+id/et_account"
  42.             android:layout_width="match_parent"
  43.             android:layout_height="match_parent"
  44.             android:background="@drawable/edit_style"
  45.             android:hint="请输入账号"
  46.             android:textColor="@color/Gray"
  47.             android:textSize="20sp"
  48.             android:gravity="center"
  49.             android:inputType="text"
  50.             android:padding="10dp" />
  51.     </LinearLayout>
  52.     <LinearLayout
  53.         android:id="@+id/password"
  54.         android:layout_width="match_parent"
  55.         android:layout_height="wrap_content"
  56.         android:layout_below="@+id/account"
  57.         android:layout_centerVertical="true"
  58.         android:layout_marginLeft="10dp"
  59.         android:layout_marginRight="10dp"
  60.         android:orientation="horizontal">
  61.         <TextView
  62.             android:id="@+id/tv_password"
  63.             android:layout_width="wrap_content"
  64.             android:layout_height="wrap_content"
  65.             android:padding="10dp"
  66.             android:text="密 码"
  67.             android:background="@drawable/text_style"
  68.             android:textColor="#000"
  69.             android:textSize="25sp"/>
  70.         <View
  71.             android:layout_width="1dp"
  72.             android:layout_height="match_parent"
  73.             android:background="@color/Gray"/>
  74.         <EditText
  75.             android:id="@+id/et_password"
  76.             android:layout_width="match_parent"
  77.             android:layout_height="match_parent"
  78.             android:layout_toRightOf="@id/tv_password"
  79.             android:hint="请输入密码"
  80.             android:textColor="@color/Gray"
  81.             android:textSize="20sp"
  82.             android:gravity="center"
  83.             android:background="@drawable/edit_style"
  84.             android:inputType="textPassword"
  85.             android:padding="10dp"/>
  86.     </LinearLayout>
  87.     <Button
  88.         android:id="@+id/btn_login"
  89.         android:layout_width="match_parent"
  90.         android:layout_height="wrap_content"
  91.         android:layout_marginTop="40dp"
  92.         android:layout_marginLeft="10dp"
  93.         android:layout_marginRight="10dp"
  94.         android:background="@drawable/button_style"
  95.         android:gravity="center"
  96.         android:text="登录"
  97.         android:textColor="#ffffff"
  98.         android:textSize="30sp"
  99.         android:layout_below="@+id/password"/>
  100.     <Button
  101.         android:id="@+id/btn_register"
  102.         android:layout_width="match_parent"
  103.         android:layout_height="wrap_content"
  104.         android:layout_marginTop="20dp"
  105.         android:layout_marginLeft="10dp"
  106.         android:layout_marginRight="10dp"
  107.         android:background="@drawable/button_style"
  108.         android:gravity="center"
  109.         android:text="注册"
  110.         android:textColor="#ffffff"
  111.         android:textSize="30sp"
  112.         android:layout_below="@+id/btn_login" />
  113. </RelativeLayout>

总结:在这次基于Android Studio的音视频播放系统设计与实现的课程设计中,我不仅掌握了一系列关键技术和工具,还对整个项目开发流程有了更深入的理解。:熟悉并掌握了Android Studio的开发环境,包括如何设置、配置项目,以及使用内置的工具如模拟器进行测试。深入了解了Android SDK和相关API,特别是与音视频播放相关的部分,如MediaPlayer、VideoView等。

此外,这次实验中,我还遇到一些数据传递中断问题,所以我还查看了一些使用断电调试的方法,也是得我掌握了一些关于断点调试的有关用法,这也让我意识到断点调试与良好的代码组织和注释习惯是相辅相成的。一个清晰、有良好注释的代码结构使得断点调试更为有效,因为你可以快速了解每一部分代码的作用和相互关系。

通过这次课程设计,我不仅提高了技术能力,更重要的是学会了如何综合运用这些技术来解决实际问题。

由于篇幅限制,这里只有部分代码,有需要完整代码的小伙伴可以可以访问这里https://download.csdn.net/download/weixin_74924162/89248876

如果有小伙伴想要源码但是又没有会员的可以评论在下方,找我白嫖。

(因为有多篇文章源码,所以最好是在本文章里评论,我能第一时间知道你要的是哪一个源码,同时也可以点赞收藏加评论啥的给卑微的咱增加点微薄的积分吧,就当是报酬了,提前写过各位看官啦。)

另外,在另一篇文章中在这个项目的基础上增加了视频播放功能,实现了音频视频播放,有需要的链接奉上:Android Studio音频视频播放器课程设计-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/775836
推荐阅读
相关标签
  

闽ICP备14008679号