赞
踩
项目演示(视频中会出现黑屏是手机录像输入密码时防止密码泄露而导致的,并不是程序问题)
video02
登录注册代码和页面在上一篇文章,地址为:http://t.csdnimg.cn/sgdba
在res目录下新建一个文件夹raw,然后自己将准备好的音频文件复制粘贴到raw文件夹下 activity_music.xml布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:id="@+id/layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@drawable/shape_background"
- >
-
-
- <LinearLayout
- android:gravity="center"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <androidx.cardview.widget.CardView
- android:layout_width="300dp"
- android:layout_height="300dp"
- app:cardCornerRadius="200sp"
- android:backgroundTint="#000"
- android:layout_marginBottom="100dp"
- >
-
- <androidx.cardview.widget.CardView
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:layout_gravity="center"
- app:cardCornerRadius="100dp"
- app:cardElevation="0dp"
- >
-
- <ImageView
- android:id="@+id/img"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:src="@drawable/j1"
- android:scaleType="fitXY"
- />
-
-
- </androidx.cardview.widget.CardView>
-
- </androidx.cardview.widget.CardView>
-
-
- <!-- <androidx.cardview.widget.CardView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="80dp"
- android:layout_marginBottom="12dp"
- android:layout_gravity="center"
- app:cardCornerRadius="100dp"
- app:cardElevation="0dp"
- >
-
- <ImageView
- android:id="@+id/img"
- android:layout_width="200dp"
- android:layout_height="190dp"
- android:src="@drawable/j1"
- android:scaleType="fitXY"
- />
-
-
- </androidx.cardview.widget.CardView>-->
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <!-- 当前时长 -->
- <TextView
- android:id="@+id/tv_starttime"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:text="@string/time"
- android:textColor="@color/white"
- />
- <!-- 进度条-->
- <SeekBar
- android:id="@+id/seekbar"
- android:layout_centerHorizontal="true"
- android:layout_width="320dp"
- android:layout_marginStart="40dp"
- android:layout_height="wrap_content"
- android:progressTint="@color/white"
- />
- <!-- 总的时长-->
- <TextView
- android:id="@+id/tv_alltime"
- android:textColor="@color/white"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:text="@string/time"
- />
-
- </RelativeLayout>
-
- <RelativeLayout
- android:layout_marginTop="50dp"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
-
- <ImageView
- android:id="@+id/img_before"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="100dp"
- android:src="@drawable/vedio_houtui"
- />
-
- <ImageView
- android:id="@+id/img_start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="80dp"
- android:src="@drawable/vedio_zanting"
- />
- <ImageView
- android:id="@+id/img_end"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="80dp"
- android:src="@drawable/video_kaishi"
- android:visibility="gone"
- />
-
- <ImageView
- android:id="@+id/img_after"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="160dp"
- android:src="@drawable/vedio_qianjin"
- />
-
- </RelativeLayout>
-
- </LinearLayout>
-
- </LinearLayout>
实现代码MusciActivity.java 代码上都有注解解释代码,不懂的自行查阅
- import android.media.MediaPlayer;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
-
-
- import java.io.IOException;
-
- public class MusicActivity extends AppCompatActivity implements View.OnClickListener {
-
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 100;
- private ImageView img;
- private ImageView img_start;
- private ImageView img_after;
- private ImageView img_before;
- private ImageView img_end;
- private LinearLayout layout;
- private RotateAnimation rotateAnimation = null;
- private LinearInterpolator linearInterpolator = null;
- private MediaPlayer player;
- private TextView tv_alltime;
- private TextView tv_starttime;
- private android.widget.SeekBar seekbar;
- private final int updata = 1;
- private Handler handler;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setStatusColor(R.color.transparent);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_music);
-
- initView();
- try {
- InitAndio();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- seekbar.setProgress(0);
- setMusicData();
- ViewClick();
-
- }
-
- private void setMusicData() {
- // 设置进度条的监听
- seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- // 进度条改变时调用
- @Override
- public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
- if (!player.isPlaying()) {
- player.seekTo(seekbar.getProgress());
- tv_starttime.setText(new Vedio().JinDuTime(seekbar.getProgress() / 1000));
- }
- }
-
- // 开始拉动进度条时调用
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- handler.removeMessages(updata);
- //player.stop();
- player.pause();
- }
-
- // 停止拉动进度条时调用
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- handler.sendEmptyMessage(updata);
- player.start();
- }
- });
-
- handler = new Handler(new Handler.Callback() {
- @Override
- public boolean handleMessage(@NonNull Message message) {
- switch (message.what) {
- case updata: {
- // 设置进度条的最大值
- seekbar.setMax(player.getDuration());
- // 设置当前进度条长度(即监听得到的时间)
- seekbar.setProgress(player.getCurrentPosition());
- // 设置当前音乐播放的位置
- tv_starttime.setText(new Vedio().JinDuTime(player.getCurrentPosition() / 1000));
- // 每隔1000毫秒刷新一次进度条的值
- handler.sendEmptyMessageDelayed(updata, 1000);
- }
- }
- return true;
- }
- });
- }
-
- /**
- * 设置状态栏颜色
- */
- public void setStatusColor(int color) {
- if (Build.VERSION.SDK_INT >= 21) {
- View view = MusicActivity.this.getWindow().getDecorView();
- view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- MusicActivity.this.getWindow().setStatusBarColor(MusicActivity.this.getResources().getColor(color));
- }
- }
-
- /**
- * 初始化控件
- */
- public void initView() {
- img_start = findViewById(R.id.img_start);
- img_after = findViewById(R.id.img_after);
- img_before = findViewById(R.id.img_before);
- img_end = findViewById(R.id.img_end);
- layout = findViewById(R.id.layout);
- tv_alltime = findViewById(R.id.tv_alltime);
- tv_starttime = findViewById(R.id.tv_starttime);
- seekbar = findViewById(R.id.seekbar);
-
- }
-
- /**
- * 控件点击事件
- */
- public void ViewClick() {
- img_start.setOnClickListener(this);
- img_before.setOnClickListener(this);
- img_after.setOnClickListener(this);
- img_end.setOnClickListener(this);
- }
-
- /**
- * 初始化音频数据
- */
- public void InitAndio() throws IOException {
- player = new MediaPlayer();
- // 获取音频文件
- player = MediaPlayer.create(getApplicationContext(), R.raw.music01);
- // 获取音频的总时间
- int time = player.getDuration() / 1000;
- tv_alltime.setText(new Vedio().JinDuTime(time));
-
- }
-
- @Override
- public void onClick(View view) {
- if (view.getId() == R.id.img_start) { // 开始播放按钮
- // 点击开始播放按钮并隐藏,img_end显示
- img_start.setVisibility(View.GONE);
- img_end.setVisibility(View.VISIBLE);
- // 开始音乐播放
- player.start();
- // 添加监听
- handler.sendEmptyMessage(updata);
- } else if (view.getId() == R.id.img_after) {
- Toast.makeText(this, "下一曲", Toast.LENGTH_SHORT).show();
- } else if (view.getId() == R.id.img_before) {
- Toast.makeText(this, "上一曲", Toast.LENGTH_SHORT).show();
- } else if (view.getId() == R.id.img_end) {
- // 暂停音乐播放
- player.pause();
- // 点击暂停按钮并隐藏,img_start显示
- img_end.setVisibility(View.GONE);
- img_start.setVisibility(View.VISIBLE);
- // 移除监听
- handler.removeMessages(updata);
- }
- }
- }
时间换算工具类 Video.java
- import java.io.Serializable;
-
- public class Vedio {
- public String JinDuTime(int time){
- int miao=time%60;
- int fen=time/60;
- int shi=time/60/60;
- Serializable getfen = fen < 10 ? 0 + "" + fen : fen;
- Serializable getmiao = miao < 10 ? 0 + "" + miao : miao;
- return getfen+":"+getmiao;
- }
- }
至此简易音乐播放器就已完成了,更多功能待你扩展,代码只是提供参考,希望你能从中获取有用的技术价值
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。