在android中创建音频文件的波形

在android中创建音频文件的波形,第1张

概述我想在 android中创建音频文件波形.任何人都可以提供示例源代码吗? 创建类并扩展View.然后为您的类创建Listener接口. public class WaveformCls extends View { public interface WaveformListener { public void waveformTouchStart(float x); 我想在 android中创建音频文件的波形.任何人都可以提供示例源代码吗?解决方法 创建类并扩展VIEw.然后为您的类创建Listener接口.
public class WaveformCls extends VIEw {    public interface WaveformListener {        public voID waveformtouchstart(float x);        public voID waveformtouchmove(float x);        public voID waveformtouchend();        public voID waveformFling(float x);        public voID waveformDraw();    };

根据您的要求创建Paint对象.
创建一个初始化所有Paint对象的方法.

public WaveformVIEw(Context context,AttributeSet attrs) {        super(context,attrs);        // We don't want keys,the markers get these        setFocusable(false);        mGrIDPaint = new Paint();        mGrIDPaint.setAntiAlias(false);        mGrIDPaint.setcolor(            getResources().getcolor(R.drawable.grID_line));        mSelectedlinePaint = new Paint();        mSelectedlinePaint.setAntiAlias(false);        mSelectedlinePaint.setcolor(            getResources().getcolor(R.drawable.waveform_selected));        mUnselectedlinePaint = new Paint();        mUnselectedlinePaint.setAntiAlias(false);        mUnselectedlinePaint.setcolor(            getResources().getcolor(R.drawable.waveform_unselected));        mUnselectedBkgndlinePaint = new Paint();        mUnselectedBkgndlinePaint.setAntiAlias(false);        mUnselectedBkgndlinePaint.setcolor(            getResources().getcolor(                R.drawable.waveform_unselected_bkgnd_overlay));        mborderlinePaint = new Paint();        mborderlinePaint.setAntiAlias(true);        mborderlinePaint.setstrokeWIDth(1.5f);        mborderlinePaint.setPathEffect(            new DashPathEffect(new float[] { 3.0f,2.0f },0.0f));        mborderlinePaint.setcolor(            getResources().getcolor(R.drawable.selection_border));        mPlaybacklinePaint = new Paint();        mPlaybacklinePaint.setAntiAlias(false);        mPlaybacklinePaint.setcolor(            getResources().getcolor(R.drawable.playback_indicator));        mTimecodePaint = new Paint();        mTimecodePaint.setTextSize(12);        mTimecodePaint.setAntiAlias(true);        mTimecodePaint.setcolor(            getResources().getcolor(R.drawable.timecode));        mTimecodePaint.setShadowLayer(            2,1,getResources().getcolor(R.drawable.timecode_shadow));    mGestureDetector = new GestureDetector(            context,new GestureDetector.SimpleOnGestureListener() {            public boolean onFling(                    MotionEvent e1,MotionEvent e2,float vx,float vy) {            mListener.waveformFling(vx);            return true;            }        });        mSoundfile = null;        mLenByZoomLevel = null;        mValuesByZoomLevel = null;        mHeightsAtThisZoomLevel = null;        mOffset = 0;        mPlaybackPos = -1;        mSelectionStart = 0;        mSelectionEnd = 0;        mDensity = 1.0f;        mInitialized = false;    }

您需要覆盖ontouchEvent

@OverrIDe    public boolean ontouchEvent(MotionEvent event) {    if (mGestureDetector.ontouchEvent(event)) {        return true;    }        switch(event.getAction()) {        case MotionEvent.ACTION_DOWN:            mListener.waveformtouchstart(event.getX());            break;        case MotionEvent.ACTION_MOVE:            mListener.waveformtouchmove(event.getX());            break;        case MotionEvent.ACTION_UP:            mListener.waveformtouchend();            break;        }        return true;    }public voID setSoundfile(CheapSoundfile soundfile) {    mSoundfile = soundfile;    mSampleRate = mSoundfile.getSampleRate();    mSamplesPerFrame = mSoundfile.getSamplesPerFrame();    computeDoublesForAllZoomLevels();    mHeightsAtThisZoomLevel = null;}

覆盖在屏幕上绘制波形的绘制方法.

@OverrIDe    protected voID onDraw(Canvas canvas) {        super.onDraw(canvas);        if (mSoundfile == null)            return;        if (mHeightsAtThisZoomLevel == null)            computeIntsForThisZoomLevel();        // Draw waveform        int measureDWIDth = getMeasureDWIDth();        int measuredHeight = getMeasuredHeight();        int start = mOffset;        int wIDth = mHeightsAtThisZoomLevel.length - start;        int ctr = measuredHeight / 2;        if (wIDth > measureDWIDth)            wIDth = measureDWIDth;        // Draw grID        double onePixelinSecs = pixelsToSeconds(1);        boolean onlyEveryFiveSecs = (onePixelinSecs > 1.0 / 50.0);        double fractionalSecs = mOffset * onePixelinSecs;        int integerSecs = (int) fractionalSecs;        int i = 0;        while (i < wIDth) {            i++;            fractionalSecs += onePixelinSecs;            int integerSecsNew = (int) fractionalSecs;            if (integerSecsNew != integerSecs) {                integerSecs = integerSecsNew;                if (!onlyEveryFiveSecs || 0 == (integerSecs % 5)) {                    canvas.drawline(i,i,measuredHeight,mGrIDPaint);                }            }        }        // Draw waveform        for (i = 0; i < wIDth; i++) {            Paint paint;            if (i + start >= mSelectionStart &&                i + start < mSelectionEnd) {                paint = mSelectedlinePaint;            } else {                drawWaveformline(canvas,mUnselectedBkgndlinePaint);                paint = mUnselectedlinePaint;            }            drawWaveformline(                canvas,ctr - mHeightsAtThisZoomLevel[start + i],ctr + 1 + mHeightsAtThisZoomLevel[start + i],paint);            if (i + start == mPlaybackPos) {                canvas.drawline(i,mPlaybacklinePaint);            }        }        // If we can see the right edge of the waveform,draw the        // non-waveform area to the right as unselected        for (i = wIDth; i < measureDWIDth; i++) {            drawWaveformline(canvas,mUnselectedBkgndlinePaint);                    }        // Draw borders        canvas.drawline(            mSelectionStart - mOffset + 0.5f,30,mSelectionStart - mOffset + 0.5f,mborderlinePaint);        canvas.drawline(            mSelectionEnd - mOffset + 0.5f,mSelectionEnd - mOffset + 0.5f,measuredHeight - 30,mborderlinePaint);        // Draw timecode        double timecodeIntervalSecs = 1.0;        if (timecodeIntervalSecs / onePixelinSecs < 50) {            timecodeIntervalSecs = 5.0;        }        if (timecodeIntervalSecs / onePixelinSecs < 50) {            timecodeIntervalSecs = 15.0;        }        // Draw grID        fractionalSecs = mOffset * onePixelinSecs;        int integerTimecode = (int) (fractionalSecs / timecodeIntervalSecs);        i = 0;        while (i < wIDth) {            i++;            fractionalSecs += onePixelinSecs;            integerSecs = (int) fractionalSecs;            int integerTimecodeNew = (int) (fractionalSecs /                                            timecodeIntervalSecs);            if (integerTimecodeNew != integerTimecode) {                integerTimecode = integerTimecodeNew;                // Turn,e.g. 67 seconds into "1:07"                String timecodeMinutes = "" + (integerSecs / 60);                String timecodeSeconds = "" + (integerSecs % 60);                if ((integerSecs % 60) < 10) {                    timecodeSeconds = "0" + timecodeSeconds;                }                String timecodeStr = timecodeMinutes + ":" + timecodeSeconds;                float offset = (float) (                    0.5 * mTimecodePaint.measureText(timecodeStr));                canvas.drawText(timecodeStr,i - offset,(int)(12 * mDensity),mTimecodePaint);            }        }        if (mListener != null) {            mListener.waveformDraw();        }    }

以下是RindroID的完整源代码,对您有用
Source code for waveform

总结

以上是内存溢出为你收集整理的在android中创建音频文件的波形全部内容,希望文章能够帮你解决在android中创建音频文件的波形所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131311.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存