本文实例为大家分享了SurfaceVIEw播放视频发送d幕,并实现滚动歌词,供大家参考,具体内容如下
SurfaceVIEw使用步骤
使用SurfaceVIEw的步骤:
首先要继承SurfaceVIEw,实现SurfaceHolder.Callback接口。重写方法:surfaceChanged:surface大小或格式发生变化时触发,在surfaceCreated调用后该函数至少会被调用一次。surfaceCreated:Surface创建时触发,一般在这个函数开启绘图线程(新的线程,不要再这个线程中绘制Surface)。surfaceDestroyed:销毁时触发,一般不可见时就会销毁。利用getHolder()获取SurfaceHolder对象,调用SurfaceHolder.addCallback添加回调SurfaceHolder.lockCanvas 获取Canvas对象并锁定画布,调用Canvas绘图,SurfaceHolder.unlockCanvasAndPost 结束锁定画布,提交改变。SurfaceHolder
SurfaceHolder回调自己的三个生命周期:
surfaceCreated(SurfaceHolder holder)创建时surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height)改变时surfaceDestroyed(SurfaceHolder holder)销毁时SurfaceVIEw播放视频并可以发送d幕
需要准备两个SurfaceVIEw,一个是负责播放视频,一个负责展示d幕。d幕是展示在视频上面的,所以我们要给展示d幕的SurfaceVIEw设置两个方法:
surfaceVIEw_danmu.setZOrderOntop(true); ——置于上方
surfaceVIEw_danmuHolder.setFormat(PixelFormat.transparent); ——背景透明
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback { public static final int PROGRESS = 101; SurfaceVIEw surfaceVIEw,surfaceVIEw_danmu; SurfaceHolder surfaceHolder,surfaceVIEw_danmuHolder; MediaPlayer mediaPlayer = new MediaPlayer(); String path = "/sdcard/aa.mp4"; Seekbar seekbar; Timer timer; EditText editText; button button,bt_start,bt_stop; int x = 0; ArrayList<DanMu> List = new ArrayList<>(); Handler handler = new Handler(){ @OverrIDe public voID handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == PROGRESS){ seekbar.setProgress((Integer) msg.obj); mediaPlayer.setonSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() { @OverrIDe public voID onSeekComplete(MediaPlayer mp) { Toast.makeText(MainActivity.this,"播放完毕!",Toast.LENGTH_SHORT).show(); } }); } } }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); surfaceVIEw = findVIEwByID(R.ID.sufacevIEw); surfaceVIEw_danmu = findVIEwByID(R.ID.sufacevIEw_danmu); editText = findVIEwByID(R.ID.et); button = findVIEwByID(R.ID.bt_send); seekbar = findVIEwByID(R.ID.bar); bt_start = findVIEwByID(R.ID.bt_start); bt_stop = findVIEwByID(R.ID.bt_stop); surfaceHolder = surfaceVIEw.getHolder(); surfaceHolder.addCallback(this); surfaceVIEw_danmuHolder = surfaceVIEw_danmu.getHolder(); surfaceVIEw_danmuHolder.addCallback(this); surfaceVIEw_danmu.setZOrderOntop(true); surfaceVIEw_danmuHolder.setFormat(PixelFormat.transparent); if (mediaPlayer!=null) { try { mediaPlayer.setDataSource(path); mediaPlayer.prepareAsync(); mediaPlayer.setonPreparedListener(new MediaPlayer.OnPreparedListener() { @OverrIDe public voID onPrepared(MediaPlayer mp) { mediaPlayer.start(); int duration = mediaPlayer.getDuration(); seekbar.setMax(duration); timer = new Timer(); timer.schedule(new TimerTask() { @OverrIDe public voID run() { int currentposition = mediaPlayer.getCurrentposition(); Message message = Message.obtain(); message.what = PROGRESS; message.obj = currentposition; handler.sendMessage(message); } },1000); } }); seekbar.setonSeekbarchangelistener(new Seekbar.OnSeekbarchangelistener() { @OverrIDe public voID onProgressChanged(Seekbar seekbar,int progress,boolean fromUser) { } @OverrIDe public voID onStartTrackingtouch(Seekbar seekbar) { } @OverrIDe public voID onStopTrackingtouch(Seekbar seekbar) { int progress = seekbar.getProgress(); mediaPlayer.seekTo(progress); } }); } catch (IOException e) { e.printstacktrace(); } } button.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { String s = editText.getText().toString(); int y= (int) (Math.random()*300); List.add(new DanMu(0,y,s,color.RED)); } }); bt_start.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { mediaPlayer.start(); } }); bt_stop.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { mediaPlayer.pause(); } }); } @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { if (holder == surfaceHolder) { mediaPlayer.setdisplay(holder); }else if (holder == surfaceVIEw_danmuHolder) { new MyDanMuThread().start(); } } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int height) { } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { } class MyDanMuThread extends Thread { @OverrIDe public voID run() { super.run(); Paint paint = new Paint(); paint.setcolor(color.RED); paint.setTextSize(40); while (true) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printstacktrace(); } Canvas canvas = surfaceVIEw_danmuHolder.lockCanvas(); canvas.drawcolor(color.transparent,PorterDuff.Mode.CLEAR); for (int i=0;i<List.size();i++) { DanMu danMu = List.get(i); x = danMu.getX(); danMu.setX(x+=10); canvas.drawText(danMu.getText(),danMu.getX(),danMu.getY(),paint); } surfaceVIEw_danmuHolder.unlockCanvasAndPost(canvas); } } }}
实现滚动歌词
要准备一个装有时间和歌词的集合,准备一个Timer计时器和一个该展示第几个歌词。每隔一秒去拿该歌词的下一个歌词时间与进度匹配,如果进度>=该时间,那么就画该歌词在SurfaceVIEw上;<则不画该歌词
一定要写SD卡读写权限!!!!!
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{ SurfaceVIEw surfaceVIEw; SurfaceHolder surfaceHolder; MediaPlayer mediaPlayer = new MediaPlayer(); ArrayList<Music> List=new ArrayList<>(); Timer timer; int count = 0; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); surfaceVIEw = findVIEwByID(R.ID.sv); surfaceHolder=surfaceVIEw.getHolder(); surfaceHolder.addCallback(this); timer = new Timer(); timer.schedule(new TimerTask() { @OverrIDe public voID run() { int currentposition = mediaPlayer.getCurrentposition(); if(List.size()>0){ if(currentposition>=List.get(count+1).getTime()){ count++; } } } },500); try { mediaPlayer.setDataSource("/sdcard/凤凰传奇 - 最炫民族风(live).mp3"); mediaPlayer.prepareAsync(); mediaPlayer.setonPreparedListener(new MediaPlayer.OnPreparedListener() { @OverrIDe public voID onPrepared(MediaPlayer mp) { mediaPlayer.start(); } }); } catch (IOException e) { e.printstacktrace(); } try { StringBuffer stringBuffer = new StringBuffer(); fileinputStream fileinputStream = new fileinputStream("/sdcard/最炫民族风"); byte[] bytes = new byte[1024]; int len = 0; while ((len=fileinputStream.read(bytes))!=-1) { stringBuffer.append(new String(bytes,len)); } Gson gson = new Gson(); JavaBean javaBean = gson.fromJson(stringBuffer.toString(),JavaBean.class); JavaBean.LrcBean lrc = javaBean.getLrc(); String lyric = lrc.getLyric(); String[] split = lyric.split("\n"); for(int i=0;i<split.length;i++){ String trim = split[i].trim(); String[] split1 = trim.split("]"); if(split.length>=2){ String s = split1[0]; String substring1 = s.substring(1,3); String substring2 = s.substring(4,6); String substring3 = s.substring(7,10); long start = Integer.parseInt(substring1)*60*1000+Integer.parseInt(substring2)*1000+Integer.parseInt(substring3); String text = split1[1]; List.add(new Music(text,start)); } } } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { new MyThread().start(); } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int height) { } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { } class MyThread extends Thread { @OverrIDe public voID run() { super.run(); Paint paint = new Paint(); paint.setcolor(color.RED); paint.setTextSize(50); while (true){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printstacktrace(); } Canvas canvas = surfaceHolder.lockCanvas(); if(canvas==null){ break; } canvas.drawcolor(color.transparent,PorterDuff.Mode.CLEAR); if(List.size()>0){ canvas.drawText(List.get(count).getTitle(),100,paint); } surfaceHolder.unlockCanvasAndPost(canvas); } } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的SurfaceView播放视频发送d幕并实现滚动歌词全部内容,希望文章能够帮你解决SurfaceView播放视频发送d幕并实现滚动歌词所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)