欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SurfaceView

程序员文章站 2022-03-29 19:30:08
...

package com.example.homework;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.SeekBar;

import com.example.homework.bean.Music;
import com.example.homework.bean.MusicBean;
import com.google.gson.Gson;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Main2Activity extends AppCompatActivity implements SurfaceHolder.Callback{

private static final String TAG = "MainActivity";
private SurfaceView mSurfaceView;
private SeekBar mSeekBar;
private MediaPlayer mediaPlayer;
private SurfaceHolder holder;
private List<Music> list=new ArrayList<>();
private Timer timer=new Timer();
private int position;

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

    initermission();

    initView();

    initData();

    initPlay();

    initSeekBarOnClick();
}

private void initSeekBarOnClick() {
    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if(b){
                list.get(i).getText();
                mediaPlayer.seekTo(i);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

private void initData() {

    try {
        FileInputStream is = new FileInputStream("/sdcard/Music/最炫民族风");
        int len=0;
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();

        while ((len=is.read(bytes)) != -1) {

            sb.append(new String(bytes,0,len));

        }
        MusicBean musicBean = new Gson().fromJson(sb.toString(), MusicBean.class);
        String lyric = musicBean.getLrc().getLyric();

        String[] strings = lyric.split("\n");
        for (int i = 1; i < strings.length; i++) {
            String replace = strings[i].replace("[", "")
                    .replace(":","@")
                    .replace(".","@")
                    .replace("]","@");
            Log.i(TAG, "initData: "+replace);
            String[] split = replace.split("@");
            long  time=Integer.parseInt(split[0])*60*1000+Integer.parseInt(split[1])*1000+Integer.parseInt(split[2]);
            if (split.length>=4) {
            list.add(new Music(split[3],time));
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void initermission() {
    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
        requestPermissions(new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        },120);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode==120&grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.i(TAG, "onRequestPermissionsResult: 授权成功!");
    }else{
        Log.i(TAG, "onRequestPermissionsResult: 授权失败!");
    }
}

private void initPlay() {
    if(mediaPlayer==null){
        mediaPlayer=new MediaPlayer();
    }
    try {
        mediaPlayer.setDataSource("/mnt/sdcard/Music/凤凰传奇 - 最炫民族风(Live).mp3");
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mediaPlayer) {
                mediaPlayer.start();
                mSeekBar.setMax(mediaPlayer.getDuration());
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if(list.size()>=0&&position+1<list.size()){
                        int currentPosition = mediaPlayer.getCurrentPosition();
                        if(currentPosition>=list.get(position+1).getTime()){
                            position++;
                        }
                        mSeekBar.setProgress(currentPosition);
                        }
                    }
                },0,1000);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void initView() {
    mSurfaceView = findViewById(R.id.surfaceView);
    mSeekBar = findViewById(R.id.seekBar);
    holder = mSurfaceView.getHolder();
    holder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {

    new MyThread().start();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

}
class MyThread extends Thread{
    @Override
    public void run() {
        super.run();
        Paint paint = new Paint();
        paint.setTextSize(60);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        while(true){
            Canvas canvas = holder.lockCanvas();

            canvas.drawColor(Color.WHITE);
            canvas.drawText(list.get(position).getText(),200,400,paint);

            holder.unlockCanvasAndPost(canvas);
        }
    }
}

}