记录媒体播放控件的使用。
VedioView
实现流程
获取权限
保持屏幕常亮
xml布局文件
xml布局文件中添加VideoView
初始化配置
添加播放控制条
问题
configChanges属性
screenOrientation属性
全屏
音乐播放
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(); int resid = R.raw.gechangzuguo; mediaPlayer = MediaPlayer.create(this,resid);
animation = AnimationUtils.loadAnimation(this,R.anim.rotate_anim); animation.setInterpolator(new LinearInterpolator());
} private void RotateImg(int state){ if (animation != null){ switch (state){ case 1: CDimg.setAnimation(animation); CDimg.startAnimation(animation); break; default: CDimg.clearAnimation();
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); } } } }
|