android實(shí)現(xiàn)錄屏小功能
本文實(shí)例為大家分享了android實(shí)現(xiàn)錄屏小功能的具體代碼,供大家參考,具體內(nèi)容如下
思路
android實(shí)現(xiàn)錄屏功能有兩種方案,一種是直接使用android自帶的MediaProjectionManager實(shí)現(xiàn)錄屏功能,第二種是是只錄語音,用戶的操作通過某種方式進(jìn)行記錄保存,最后通過某種協(xié)議進(jìn)行播放。
兩種方案各有各的優(yōu)缺點(diǎn),前者實(shí)現(xiàn)方式簡單,但無法只錄制特定區(qū)域的畫面,并且生成的視頻文件一般都比較大。后者實(shí)現(xiàn)較為繁瑣,音頻錄制android7.0之前沒有暫停方法,只能生成多個文件,然后對音頻進(jìn)行合成。用戶的操作需要自己進(jìn)行保存,播放時還原。播放器需要自定義生成。但后者的好處是可擴(kuò)展性高,支持特定區(qū)域錄制,并且生成的音頻文件比較小。
需求
錄制畫板,畫板要求可以更改顏色粗細(xì),可以擦除。畫板底部可以是白板,圖片。圖片要求是相機(jī)拍攝或者本地圖片??梢圆シ配浿苾?nèi)容;需要上傳,所以文件要小,所有只能選擇第二種方式。
github地址
整個項(xiàng)目生成的是一個文件夾,文件夾中包含一個MP3文件,一個cw協(xié)議文件(存儲用戶的操作),圖片。整個畫板是一個recyclerView,item中包含一個涂鴉畫板,圖片控件。播放時讀取cw協(xié)議文件,按照時間一個個繪制,協(xié)議內(nèi)容包含畫板各個頁的內(nèi)容是空白畫板還是圖片,時間點(diǎn),操作(切換圖片/畫線)。
音頻
//開始錄音 if (mMediaRecorder == null) { mMediaRecorder = new MediaRecorder(); } mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); mMediaRecorder.setOutputFile(mRecordFilePath); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//amr_nb格式頭部有6個字節(jié)的頭信息 try { mMediaRecorder.prepare(); mMediaRecorder.start(); isRunning = true; AudioUtil.startAudio(); mHandler.sendEmptyMessageDelayed(MSG_TYPE_COUNT_DOWN, 1000); } catch (IOException e) { e.printStackTrace(); }
/** * 合成amr_nb編碼的音頻 * @param partsPaths * @param unitedFilePath */ public static void uniteAMRFile(List<String> partsPaths, String unitedFilePath) { try { File unitedFile = new File(unitedFilePath); FileOutputStream fos = new FileOutputStream(unitedFile); RandomAccessFile ra = null; for (int i = 0; i < partsPaths.size(); i++) { ra = new RandomAccessFile(partsPaths.get(i), "rw"); if (i != 0) { ra.seek(6); } byte[] buffer = new byte[1024 * 8]; int len = 0; while ((len = ra.read(buffer)) != -1) { fos.write(buffer,0,len); } File file = new File(partsPaths.get(i)); if(file.exists()){ file.delete(); } } if(ra!=null){ ra.close(); } fos.close(); } catch (Exception e) { e.printStackTrace(); } }
音頻播放
mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(path); mediaPlayer.prepare(); mediaPlayer.start();
recyclerView
是否禁止滑動
public class ForbitLayoutManager extends LinearLayoutManager { private boolean canScrollHorizon = true; private boolean canScrollVertical = true; public ForbitLayoutManager(Context context) { super(context); } public ForbitLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public ForbitLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public void setCanScrollHorizon(boolean canScrollHorizon) { this.canScrollHorizon = canScrollHorizon; } public void setCanScrollVertical(boolean canScrollVertical) { this.canScrollVertical = canScrollVertical; } @Override public boolean canScrollHorizontally() { return canScrollHorizon && super.canScrollHorizontally(); } @Override public boolean canScrollVertically() { return canScrollVertical && super.canScrollVertically(); } }
滑動時只滑動一頁類似viewPage
mPagerSnapHelper = new PagerSnapHelper(); mPagerSnapHelper.attachToRecyclerView(recyclerView);
獲得當(dāng)前是第幾頁,類似viewPage的pageSelect
public class RecyclerViewPageChangeListenerHelper extends RecyclerView.OnScrollListener { private SnapHelper snapHelper; private OnPageChangeListener onPageChangeListener; private int oldPosition = -1;//防止同一Position多次觸發(fā) public RecyclerViewPageChangeListenerHelper(SnapHelper snapHelper, OnPageChangeListener onPageChangeListener) { this.snapHelper = snapHelper; this.onPageChangeListener = onPageChangeListener; } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (onPageChangeListener != null) { onPageChangeListener.onScrolled(recyclerView, dx, dy); } } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); int position = 0; RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); //獲取當(dāng)前選中的itemView View view = snapHelper.findSnapView(layoutManager); if (view != null) { //獲取itemView的position position = layoutManager.getPosition(view); } if (onPageChangeListener != null) { onPageChangeListener.onScrollStateChanged(recyclerView, newState); //newState == RecyclerView.SCROLL_STATE_IDLE 當(dāng)滾動停止時觸發(fā)防止在滾動過程中不停觸發(fā) if (newState == RecyclerView.SCROLL_STATE_IDLE && oldPosition != position) { oldPosition = position; onPageChangeListener.onPageSelected(position); } } } public interface OnPageChangeListener { void onScrollStateChanged(RecyclerView recyclerView, int newState); void onScrolled(RecyclerView recyclerView, int dx, int dy); void onPageSelected(int position); } }
獲得當(dāng)前選擇的item(只能獲得可視頁面item)
View view = forbitLayoutManager.findViewByPosition(position); //有時會獲取到null,是因?yàn)轫撁孢€沒有渲染完成,可以使用 recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver .OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //會多次調(diào)用,執(zhí)行完邏輯之后取消監(jiān)聽 recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } });
根據(jù)時間進(jìn)行播放
private void convertCWACT(CW cw, int seconds,boolean isSeek) { List<CWACT> cwacts = cw.getACT(); //如何是播放器跳轉(zhuǎn),先回到首頁,清空所有item中的畫板,防止從高時間跳轉(zhuǎn)到低時間出現(xiàn)錯誤 if(isSeek){ position =0; forbitLayoutManager.scrollToPosition(position); forbitLayoutManager.setStackFromEnd(true); for(int i=0;i<recyclerViewList.size();i++){ View view = recyclerViewList.get(i); if(view!=null){ SimpleDoodleView doodleView = view.findViewById(R.id.doodleView); doodleView.clear(); } } } for (CWACT cwact : cwacts) { int time = cwact.getTime(); if(isSeek?time > seconds:time != seconds){ continue; } if ("switch".equals(cwact.getAction())) {//切換頁面 position = cwact.getCwSwitch().getIndex(); forbitLayoutManager.scrollToPosition(position); forbitLayoutManager.setStackFromEnd(true); } else if ("line".equals(cwact.getAction())) {//劃線 if(position>recyclerViewList.size()-1){ continue; } View view = recyclerViewList.get(position); if(view!=null){ SimpleDoodleView doodleView = view.findViewById(R.id.doodleView); doodleView.setDrawPath(cwact.getLine()); } } else if ("clear".equals(cwact.getAction())) {//清屏 if(position>recyclerViewList.size()-1){ continue; } View view = recyclerViewList.get(position); if(view!=null){ SimpleDoodleView doodleView = view.findViewById(R.id.doodleView); doodleView.clear(); } } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
- Android錄屏的三種解決方案
- 淺析Android錄屏 MediaRecorder
- android實(shí)現(xiàn)錄屏功能
- android設(shè)置adb自帶screenrecord錄屏命令
- android桌面懸浮窗顯示錄屏?xí)r間控制效果
- Android錄屏功能的實(shí)現(xiàn)
- Android5.0以上版本錄屏實(shí)現(xiàn)代碼(完整代碼)
- android視頻截屏&手機(jī)錄屏實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)錄屏小功能
相關(guān)文章
解析Android中Animation動畫的編寫要點(diǎn)
這篇文章主要介紹了Android中Animation動畫的編寫要點(diǎn),講解了Animation的幾個常用標(biāo)簽的用法,需要的朋友可以參考下2016-04-04Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar
這篇文章主要給大家介紹了關(guān)于Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12解決Android studio 3.6.1 出現(xiàn)Cause: unable to find valid certifi
這篇文章主要介紹了Android studio 3.6.1 出現(xiàn)Cause: unable to find valid certification path to requested target 報(bào)錯的問題及解決方法,需要的朋友可以參考下2020-03-03Android Http協(xié)議訪問網(wǎng)絡(luò)實(shí)例(3種)
本篇文章主要介紹了Android Http協(xié)議訪問網(wǎng)絡(luò)實(shí)例(3種),具有一定的參考價值,有興趣的可以了解一下2017-07-07android初學(xué)者必須掌握的Activity狀態(tài)的四大知識點(diǎn)(必讀)
本篇文章主要介紹了android activity的四種狀態(tài),詳細(xì)的介紹了四種狀態(tài),包括Running狀態(tài)、Paused狀態(tài)、Stopped狀態(tài)、Killed狀態(tài),有興趣的可以了解一下。2016-11-11Android實(shí)現(xiàn)文字動態(tài)高亮讀取進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文字動態(tài)高亮讀取進(jìn)度效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05Android利用二階貝塞爾曲線實(shí)現(xiàn)添加購物車動畫詳解
這篇文章主要給大家介紹了關(guān)于Android利用二階貝塞爾曲線實(shí)現(xiàn)添加購物車動畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08