0%

媒体播放

记录媒体播放控件的使用。

VedioView

实现流程

获取权限

保持屏幕常亮

xml布局文件

xml布局文件中添加VideoView

初始化配置

添加播放控制条

问题

configChanges属性

screenOrientation属性

全屏

音乐播放

MediaPlayer

Raw下的资源

SD卡下的资源

案例

实现封面旋转效果

新建Android Resource Directory,安卓资源文件夹。

类型,选择“anim”。文件夹名自定义,这里取名为“anim”。

在新建的资源文件夹内新建一个资源文件。鼠标点击新建的资源文件夹,做点击鼠标右键选择新建Android Resource File,安卓资源文件。(上上图的第二个)。

Root element选择”rotate”,文件名自定义。

资源文件,配置如下:

旋转360度。

时间2秒。

循环次数为无限(-1)。

x、y轴偏移50%(实现中心旋转效果)。

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%"
></rotate>
</rotate>

源码

布局xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/musicimg"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/musicimg"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/musictext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/musicimg"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:hint="歌唱祖国"/>
<Button
android:id="@+id/musicbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="START-开始播放"
android:layout_alignParentBottom="true"/>



</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private MediaPlayer mediaPlayer;//媒体控制器
private Button musicbtn;//按钮
private Animation animation;//动画
private ImageView CDimg;//封面
private TextView musictext;//歌曲信息

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

musictext = (TextView)findViewById(R.id.musictext);
CDimg = (ImageView)findViewById(R.id.musicimg);
musicbtn = (Button)findViewById(R.id.musicbtn);
musicbtn.setOnClickListener(this);
mediaPlayer = new MediaPlayer();
//初始化MediaPlayer,实例化 --》 加载raw文件夹下的音频资源
int resid = R.raw.gechangzuguo;//应用内的音频文件
mediaPlayer = MediaPlayer.create(this,resid);//加载音频资源
// try {//加载sd卡中的文件
// String resPath = "/storage/emulated/0/Pictures/歌唱祖国.mp3";
// musictext.setText(resPath.substring(resPath.lastIndexOf("/") + 1,resPath.lastIndexOf(".")));//文件名
// mediaPlayer.setDataSource(resPath);//‪C:\Users\admin\Nox_share\music\歌唱祖国.mp3
// mediaPlayer.prepareAsync();//音频资源的异步缓存
// } catch (IOException e) {
// e.printStackTrace();
// }
//animation与动作文件加载
animation = AnimationUtils.loadAnimation(this,R.anim.rotate_anim);
animation.setInterpolator(new LinearInterpolator());//匀速


}
private void RotateImg(int state){//1,旋转。0,暂停
if (animation != null){
switch (state){
case 1:
CDimg.setAnimation(animation);
CDimg.startAnimation(animation);
break;
default:
CDimg.clearAnimation();//暂停旋转
// animation.cancel();//这样也可以暂停
break;
}
}
}

@Override
public void onClick(View view) {
if (view.getId() == R.id.musicbtn){
if (mediaPlayer.isPlaying()){//是否正在播放
mediaPlayer.pause();//暂停
musicbtn.setText("START-开始播放");
RotateImg(0);
}else{
mediaPlayer.start();//播放
musicbtn.setText("PAUSE-暂停播放");
RotateImg(1);//封面旋转
}
}
}
}

若图片不能正常显示,请在浏览器中打开

欢迎关注我的其它发布渠道