当前位置:   article > 正文

Android Studio实现简易音乐播放器(期末作业)_android studio 简易音乐播放器作业

android studio 简易音乐播放器作业

项目演示(视频中会出现黑屏是手机录像输入密码时防止密码泄露而导致的,并不是程序问题)

video02

登录注册代码和页面在上一篇文章,地址为:http://t.csdnimg.cn/sgdba

在res目录下新建一个文件夹raw,然后自己将准备好的音频文件复制粘贴到raw文件夹下

activity_music.xml布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/layout"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. android:background="@drawable/shape_background"
  10. >
  11. <LinearLayout
  12. android:gravity="center"
  13. android:orientation="vertical"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent">
  16. <androidx.cardview.widget.CardView
  17. android:layout_width="300dp"
  18. android:layout_height="300dp"
  19. app:cardCornerRadius="200sp"
  20. android:backgroundTint="#000"
  21. android:layout_marginBottom="100dp"
  22. >
  23. <androidx.cardview.widget.CardView
  24. android:layout_width="200dp"
  25. android:layout_height="200dp"
  26. android:layout_gravity="center"
  27. app:cardCornerRadius="100dp"
  28. app:cardElevation="0dp"
  29. >
  30. <ImageView
  31. android:id="@+id/img"
  32. android:layout_width="200dp"
  33. android:layout_height="200dp"
  34. android:src="@drawable/j1"
  35. android:scaleType="fitXY"
  36. />
  37. </androidx.cardview.widget.CardView>
  38. </androidx.cardview.widget.CardView>
  39. <!-- <androidx.cardview.widget.CardView
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_marginTop="80dp"
  43. android:layout_marginBottom="12dp"
  44. android:layout_gravity="center"
  45. app:cardCornerRadius="100dp"
  46. app:cardElevation="0dp"
  47. >
  48. <ImageView
  49. android:id="@+id/img"
  50. android:layout_width="200dp"
  51. android:layout_height="190dp"
  52. android:src="@drawable/j1"
  53. android:scaleType="fitXY"
  54. />
  55. </androidx.cardview.widget.CardView>-->
  56. <RelativeLayout
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content">
  59. <!-- 当前时长 -->
  60. <TextView
  61. android:id="@+id/tv_starttime"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:layout_alignParentStart="true"
  65. android:text="@string/time"
  66. android:textColor="@color/white"
  67. />
  68. <!-- 进度条-->
  69. <SeekBar
  70. android:id="@+id/seekbar"
  71. android:layout_centerHorizontal="true"
  72. android:layout_width="320dp"
  73. android:layout_marginStart="40dp"
  74. android:layout_height="wrap_content"
  75. android:progressTint="@color/white"
  76. />
  77. <!-- 总的时长-->
  78. <TextView
  79. android:id="@+id/tv_alltime"
  80. android:textColor="@color/white"
  81. android:layout_width="wrap_content"
  82. android:layout_height="wrap_content"
  83. android:layout_alignParentEnd="true"
  84. android:text="@string/time"
  85. />
  86. </RelativeLayout>
  87. <RelativeLayout
  88. android:layout_marginTop="50dp"
  89. android:layout_gravity="center"
  90. android:layout_width="wrap_content"
  91. android:layout_height="wrap_content">
  92. <ImageView
  93. android:id="@+id/img_before"
  94. android:layout_width="wrap_content"
  95. android:layout_height="wrap_content"
  96. android:layout_marginEnd="100dp"
  97. android:src="@drawable/vedio_houtui"
  98. />
  99. <ImageView
  100. android:id="@+id/img_start"
  101. android:layout_width="wrap_content"
  102. android:layout_height="wrap_content"
  103. android:layout_marginStart="80dp"
  104. android:src="@drawable/vedio_zanting"
  105. />
  106. <ImageView
  107. android:id="@+id/img_end"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:layout_marginLeft="80dp"
  111. android:src="@drawable/video_kaishi"
  112. android:visibility="gone"
  113. />
  114. <ImageView
  115. android:id="@+id/img_after"
  116. android:layout_width="wrap_content"
  117. android:layout_height="wrap_content"
  118. android:layout_marginStart="160dp"
  119. android:src="@drawable/vedio_qianjin"
  120. />
  121. </RelativeLayout>
  122. </LinearLayout>
  123. </LinearLayout>

实现代码MusciActivity.java 代码上都有注解解释代码,不懂的自行查阅

  1. import android.media.MediaPlayer;
  2. import android.os.Build;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.view.View;
  7. import android.view.animation.LinearInterpolator;
  8. import android.view.animation.RotateAnimation;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.SeekBar;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import androidx.annotation.NonNull;
  15. import androidx.appcompat.app.AppCompatActivity;
  16. import java.io.IOException;
  17. public class MusicActivity extends AppCompatActivity implements View.OnClickListener {
  18. private static final int VOICE_RECOGNITION_REQUEST_CODE = 100;
  19. private ImageView img;
  20. private ImageView img_start;
  21. private ImageView img_after;
  22. private ImageView img_before;
  23. private ImageView img_end;
  24. private LinearLayout layout;
  25. private RotateAnimation rotateAnimation = null;
  26. private LinearInterpolator linearInterpolator = null;
  27. private MediaPlayer player;
  28. private TextView tv_alltime;
  29. private TextView tv_starttime;
  30. private android.widget.SeekBar seekbar;
  31. private final int updata = 1;
  32. private Handler handler;
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. setStatusColor(R.color.transparent);
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_music);
  38. initView();
  39. try {
  40. InitAndio();
  41. } catch (IOException e) {
  42. throw new RuntimeException(e);
  43. }
  44. seekbar.setProgress(0);
  45. setMusicData();
  46. ViewClick();
  47. }
  48. private void setMusicData() {
  49. // 设置进度条的监听
  50. seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  51. // 进度条改变时调用
  52. @Override
  53. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  54. if (!player.isPlaying()) {
  55. player.seekTo(seekbar.getProgress());
  56. tv_starttime.setText(new Vedio().JinDuTime(seekbar.getProgress() / 1000));
  57. }
  58. }
  59. // 开始拉动进度条时调用
  60. @Override
  61. public void onStartTrackingTouch(SeekBar seekBar) {
  62. handler.removeMessages(updata);
  63. //player.stop();
  64. player.pause();
  65. }
  66. // 停止拉动进度条时调用
  67. @Override
  68. public void onStopTrackingTouch(SeekBar seekBar) {
  69. handler.sendEmptyMessage(updata);
  70. player.start();
  71. }
  72. });
  73. handler = new Handler(new Handler.Callback() {
  74. @Override
  75. public boolean handleMessage(@NonNull Message message) {
  76. switch (message.what) {
  77. case updata: {
  78. // 设置进度条的最大值
  79. seekbar.setMax(player.getDuration());
  80. // 设置当前进度条长度(即监听得到的时间)
  81. seekbar.setProgress(player.getCurrentPosition());
  82. // 设置当前音乐播放的位置
  83. tv_starttime.setText(new Vedio().JinDuTime(player.getCurrentPosition() / 1000));
  84. // 每隔1000毫秒刷新一次进度条的值
  85. handler.sendEmptyMessageDelayed(updata, 1000);
  86. }
  87. }
  88. return true;
  89. }
  90. });
  91. }
  92. /**
  93. * 设置状态栏颜色
  94. */
  95. public void setStatusColor(int color) {
  96. if (Build.VERSION.SDK_INT >= 21) {
  97. View view = MusicActivity.this.getWindow().getDecorView();
  98. view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  99. MusicActivity.this.getWindow().setStatusBarColor(MusicActivity.this.getResources().getColor(color));
  100. }
  101. }
  102. /**
  103. * 初始化控件
  104. */
  105. public void initView() {
  106. img_start = findViewById(R.id.img_start);
  107. img_after = findViewById(R.id.img_after);
  108. img_before = findViewById(R.id.img_before);
  109. img_end = findViewById(R.id.img_end);
  110. layout = findViewById(R.id.layout);
  111. tv_alltime = findViewById(R.id.tv_alltime);
  112. tv_starttime = findViewById(R.id.tv_starttime);
  113. seekbar = findViewById(R.id.seekbar);
  114. }
  115. /**
  116. * 控件点击事件
  117. */
  118. public void ViewClick() {
  119. img_start.setOnClickListener(this);
  120. img_before.setOnClickListener(this);
  121. img_after.setOnClickListener(this);
  122. img_end.setOnClickListener(this);
  123. }
  124. /**
  125. * 初始化音频数据
  126. */
  127. public void InitAndio() throws IOException {
  128. player = new MediaPlayer();
  129. // 获取音频文件
  130. player = MediaPlayer.create(getApplicationContext(), R.raw.music01);
  131. // 获取音频的总时间
  132. int time = player.getDuration() / 1000;
  133. tv_alltime.setText(new Vedio().JinDuTime(time));
  134. }
  135. @Override
  136. public void onClick(View view) {
  137. if (view.getId() == R.id.img_start) { // 开始播放按钮
  138. // 点击开始播放按钮并隐藏,img_end显示
  139. img_start.setVisibility(View.GONE);
  140. img_end.setVisibility(View.VISIBLE);
  141. // 开始音乐播放
  142. player.start();
  143. // 添加监听
  144. handler.sendEmptyMessage(updata);
  145. } else if (view.getId() == R.id.img_after) {
  146. Toast.makeText(this, "下一曲", Toast.LENGTH_SHORT).show();
  147. } else if (view.getId() == R.id.img_before) {
  148. Toast.makeText(this, "上一曲", Toast.LENGTH_SHORT).show();
  149. } else if (view.getId() == R.id.img_end) {
  150. // 暂停音乐播放
  151. player.pause();
  152. // 点击暂停按钮并隐藏,img_start显示
  153. img_end.setVisibility(View.GONE);
  154. img_start.setVisibility(View.VISIBLE);
  155. // 移除监听
  156. handler.removeMessages(updata);
  157. }
  158. }
  159. }

时间换算工具类 Video.java

  1. import java.io.Serializable;
  2. public class Vedio {
  3. public String JinDuTime(int time){
  4. int miao=time%60;
  5. int fen=time/60;
  6. int shi=time/60/60;
  7. Serializable getfen = fen < 10 ? 0 + "" + fen : fen;
  8. Serializable getmiao = miao < 10 ? 0 + "" + miao : miao;
  9. return getfen+":"+getmiao;
  10. }
  11. }

至此简易音乐播放器就已完成了,更多功能待你扩展,代码只是提供参考,希望你能从中获取有用的技术价值

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号