您当前的位置:首页 > 毕业设计

毕业设计- 基于Android的本地音乐播放器

时间:2022-03-10 07:14:26 阅读数:46,041人阅读
版权声明:转载请注明出处,谢谢!
—— 木叶飞舞之处,火亦生生不息。

项目介绍

本系统支持扫瞄本地音乐播放、暂停、上一首、下一首,点击播放栏跳转到播放页面,可查看当前播放列表,设置播放模式如顺序播放、随机播放,标记为我的喜欢,可以创建歌单。

1. 本地音乐列表 首次进入系统会提示是否扫瞄手机上音乐文件,扫瞄后显示在本地音乐列表中,点击即可播放;

2. 主题中心 可切换不同颜色风格的主题;

3. 夜间模式 可切换日间模式和夜间模式;

项目截图

实现原理

布局文件activity_main.xml:

			<?xml version="1.0" encoding="utf-8"?>
			<androidx.drawerlayout.widget.DrawerLayout
				xmlns:android="http://schemas.android.com/apk/res/android"
				xmlns:app="http://schemas.android.com/apk/res-auto"
				android:id="@+id/drawer_layout"
				android:layout_width="match_parent"
				android:layout_height="match_parent">

				<RelativeLayout
					android:id="@+id/activity_main"
					android:layout_width="match_parent"
					android:layout_height="match_parent"
					android:background="?attr/appbg">

					<androidx.appcompat.widget.Toolbar
						android:id="@+id/home_activity_toolbar"
						android:layout_width="match_parent"
						android:layout_height="?attr/actionBarSize"
						android:background="?attr/colorPrimary"
						android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

					<LinearLayout
						android:id="@+id/fix_list_ll"
						android:layout_width="match_parent"
						android:layout_height="wrap_content"
						android:layout_below="@id/home_activity_toolbar"
						android:orientation="vertical">

						<LinearLayout
							android:id="@+id/home_recently_music_ll"
							android:layout_width="match_parent"
							android:layout_height="50dp"
							android:background="@drawable/selector_layout"
							android:gravity="center_vertical"
							android:orientation="horizontal">

							<LinearLayout
								android:layout_width="match_parent"
								android:layout_height="match_parent"
								android:orientation="vertical">

								<RelativeLayout
									android:layout_width="match_parent"
									android:layout_height="49.5dp"
									android:orientation="horizontal">

									<TextView
										android:id="@+id/home_recently_music_tv"
										android:layout_width="wrap_content"
										android:layout_height="match_parent"
										android:gravity="center_vertical"
										android:text="最近音乐" />

									<TextView
										android:id="@+id/home_recently_music_count_tv"
										android:layout_width="wrap_content"
										android:layout_height="match_parent"
										android:layout_alignParentRight="true"
										android:layout_marginRight="30dp"
										android:gravity="center_vertical"
										android:textColor="@color/grey500"
										android:textSize="12sp" />

									<ImageView
										android:layout_width="10dp"
										android:layout_height="match_parent"
										android:layout_alignParentRight="true"
										android:layout_gravity="center_vertical"
										android:layout_marginRight="16dp"
										android:src="@drawable/arrow_right" />
								</RelativeLayout>

								<View
									android:layout_width="match_parent"
									android:layout_height="0.5dp"
									android:background="@color/colorDividerGray"/>
							</LinearLayout>
						</LinearLayout>
					</LinearLayout>
				</RelativeLayout>

				<com.google.android.material.navigation.NavigationView
					android:id="@+id/nav_view"
					android:layout_width="match_parent"
					android:layout_height="match_parent"
					android:layout_gravity="start"
					app:headerLayout="@layout/nav_header"
					app:menu="@menu/nav_menu" />
			</androidx.drawerlayout.widget.DrawerLayout>
	  
	       /**
			 * APP主页面
			 */
			public class HomeActivity extends PlayBarBaseActivity {
				/**
				 * 判断app是否是第一次启动
				 */
				private boolean isFirst = true;
				/**
				 * 添加歌单+
				 */
				private ImageView myPlayListAddImage;
				/**
				 * 歌单列表view
				 */
				private ListView listView;
				private HomeListViewAdapter adapter;
				/**
				 * 歌单列表
				 */
				private List<PlayListInfo> playListInfo;
				private int count;
				private long exitTime = 0;
				private boolean isStartTheme = false;

				@Override
				protected void onCreate(Bundle savedInstanceState) {
					super.onCreate(savedInstanceState);
					setContentView(R.layout.activity_home);
					isFirst = MyMusicUtil.getIsFirst();
					if (isFirst == true) {  //app是第一次启动
						final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
						dialog.setTitle("MyMusic");
						dialog.setMessage("第一次启动MyMusic,是否先扫描本地音乐");
						dialog.setCancelable(true);
						dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {  //跳转到扫描activity
							@Override
							public void onClick(DialogInterface dialogInterface, int i) {
								Intent intent = new Intent(HomeActivity.this, ScanActivity.class);
								startActivity(intent);
							}
						});
						dialog.setNegativeButton("否", new DialogInterface.OnClickListener() {  //直接消失
							@Override
							public void onClick(DialogInterface dialogInterface, int i) {
							}
						});
						dialog.show();
						isFirst = false;
						MyMusicUtil.setIsFirst(isFirst);  //存储状态
					}
					dbManager = DBManager.getInstance(HomeActivity.this);
					toolbar = (Toolbar) findViewById(R.id.home_activity_toolbar);
					setSupportActionBar(toolbar);
					refreshNightModeTitle();
					navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
						@Override
						public boolean onNavigationItemSelected(MenuItem item) {
							drawerLayout.closeDrawers();
							switch (item.getItemId()) {
								case R.id.nav_theme:  //主题中心
									isStartTheme = true;
									Intent intentTheme = new Intent(HomeActivity.this, ThemeActivity.class);
									startActivity(intentTheme);
									break;
								case R.id.nav_night_mode:  //夜间模式
									if (MyMusicUtil.getNightMode(HomeActivity.this)) {   //当前为夜间模式,则恢复之前的主题
										MyMusicUtil.setNightMode(HomeActivity.this, false);  //点击之后是日间模式
										int preTheme = MyMusicUtil.getPreTheme(HomeActivity.this);  //得到夜间模式之前的日间模式的颜色主题
										MyMusicUtil.setTheme(HomeActivity.this, preTheme);
									} else {  //当前为日间模式,则切换到夜间模式
										MyMusicUtil.setNightMode(HomeActivity.this, true);
										MyMusicUtil.setTheme(HomeActivity.this, ThemeActivity.THEME_SIZE - 1);
									}
									recreate();  //重新加载之后才能切换成功
									refreshNightModeTitle();
									break;
								case R.id.nav_about_me:  //关于
									Intent aboutTheme = new Intent(HomeActivity.this, AboutActivity.class);
									startActivity(aboutTheme);
									break;
								case R.id.nav_logout:  //退出
									System.exit(0);
									finish();
									break;
							}
							return true;
						}
					});
					init();
					Intent startIntent = new Intent(HomeActivity.this, MusicPlayerService.class);  //开启后台服务
					startService(startIntent);
				}

				/**
				 * 滑动窗口显示的文字
				 */
				private void refreshNightModeTitle() {
					if (MyMusicUtil.getNightMode(HomeActivity.this)) {
						navView.getMenu().findItem(R.id.nav_night_mode).setTitle("日间模式");
					} else {
						navView.getMenu().findItem(R.id.nav_night_mode).setTitle("夜间模式");
					}
				}

				/**
				 * 初始化控件
				 */
				private void init() {
					//本地音乐
					localMusicLayout.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(HomeActivity.this, LocalMusicActivity.class);
							startActivity(intent);
						}
					});
					//最近音乐
					lastPlayLayout.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(HomeActivity.this, LastMyloveActivity.class);
							intent.putExtra(Constant.LABEL, Constant.LABEL_LAST);
							startActivity(intent);
						}
					});
					//我的喜爱
					myLoveLayout.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(HomeActivity.this, LastMyloveActivity.class);
							intent.putExtra(Constant.LABEL, Constant.LABEL_MYLOVE);
							startActivity(intent);
						}
					});
					myPlayListAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									String name = playlistEdit.getText().toString();
									if (TextUtils.isEmpty(name)) {
										Toast.makeText(HomeActivity.this, "请输入歌单名", Toast.LENGTH_SHORT).show();
										return;
									}
								}
							});
							builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							});
							builder.show();//配置好后再builder show
						}
					});
				}

				/**
				 * 更新歌单的数量
				 */
				public void updatePlaylistCount() {
					count = dbManager.getMusicCount(Constant.LIST_MYPLAY);
					myPlayListCountText.setText("(" + count + ")");
				}

				@Override
				protected void onPause() {
					super.onPause();
					//如果去设置主题
					if (isStartTheme) {
						HomeActivity.this.finish();  //让活动结束,否则主题切换不成功
					}
					isStartTheme = false;
				}

			}
	  

购买套餐

权益 套餐A 套餐B
完整代码
远程调试 不支持
代码答疑 不支持
价 格 ¥180 ¥330

------转载请注明出处,感谢您对原创作者的支持------

有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业

Android学习小站

Q Q:1095817610

微信:jx-helu

邮箱:1095817610@qq.com

添加请备注"Android学习小站"