视频中添加背景音乐,需要截取出和视频相同时长的音频,这是我们仿抖音截取音频的例子。
先来看看效果把:
1、首先是布局文件:
<?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:gravity="center"
android:background="#D3D3D3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/my80dp">
<com.kkcast.audiocutview.AudioCutView
android:id="@+id/audio_cut_view"
android:layout_marginLeft="@dimen/my20dp"
android:layout_marginRight="@dimen/my20dp"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<!--截取-->
<LinearLayout
android:layout_marginTop="@dimen/my10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/my40dp">
<TextView
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/my15dp"
android:text="截取"
android:textSize="@dimen/my14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/seekbar"
android:layout_marginLeft="@dimen/my10dp"
android:layout_marginRight="@dimen/my15dp"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
自定义的view和普通view一样引入
2.看AudioCutView的代码
package com.kkcast.audiocutview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import java.util.Random;
/**
* @author kcast
* @date 2018/09/19
*/
public class AudioCutView extends View {
private Paint paint;
private float maxProgress=100.0f;
/**
* 当前进度,100进制的
*/
private float currentProgress=0.0f;
private float viewCurrentProgress=0;
private int measuredHeight;
private int measuredWidth;
private float videoProgressWidth=100;
private Paint progressPaint;
private float videoWidthRate;
private Paint wavePaint;
private float[] smoothedGains;
public AudioCutView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
progressPaint = new Paint();
progressPaint.setAntiAlias(true);
progressPaint.setStyle(Paint.Style.STROKE);
wavePaint = new Paint();
wavePaint.setColor(Color.parseColor("#FF7701"));
wavePaint.setStrokeWidth(dpToPx(3));
wavePaint.setAntiAlias(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measuredHeight = getMeasuredHeight();
measuredWidth = getMeasuredWidth();
computeSmoothedGains();
}
@Override
protected void onDraw(Canvas canvas) {
/**
* 绘制一条条线
*/
float ctr = measuredHeight / 2f;
for (int i = 0; i < measuredWidth; i+=15) {
canvas.drawLine(i, ctr-smoothedGains[i]/2, i, ctr+smoothedGains[i]/2, wavePaint);
}
/**
* 绘制阴影
*/
videoProgressWidth=measuredWidth*videoWidthRate;
paint.setStrokeWidth(getHeight());
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.parseColor("#87654321"));
viewCurrentProgress=(measuredWidth-videoProgressWidth)*currentProgress/maxProgress;
canvas.drawLine(viewCurrentProgress,measuredHeight/2,viewCurrentProgress+videoProgressWidth,measuredHeight/2,paint);
}
/**
* 设置视频占音频的比例
* @param videoWidthRate
*/
public void setVideoProgressWidth(float videoWidthRate){
this.videoWidthRate=videoWidthRate;
}
/**
* 设置最大进度
* @param maxProgress
*/
public void setMaxProgress(float maxProgress){
this.maxProgress=maxProgress;
}
/**
* 设置当前进度
* @param currentNum
*/
public void setCurrentProgress(float currentNum){
currentProgress=currentNum;
postInvalidate();
}
/**
* 生成一些随机数据
*/
public void computeSmoothedGains() {
smoothedGains = new float[measuredWidth];
Random rand = new Random();
int MAX=180;
int MIN=80;
for (int i=0;i<measuredWidth;i++){
int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
smoothedGains[i]=randNumber;
}
}
public float dpToPx(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
}
3.看在activity中的使用
package com.kkcast.audiocutview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private AudioCutView audio_cut_view;
private SeekBar seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
audio_cut_view = findViewById(R.id.audio_cut_view);
seekbar = findViewById(R.id.seekbar);
audio_cut_view.setVideoProgressWidth(0.3f);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audio_cut_view.setCurrentProgress(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}