diff --git a/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java b/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java index 05b33507d2284fd66613d344268c44ba9732dbfd..62a0c996811cd5dd50ad5c10ff093f19e9f4e8e4 100644 --- a/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java @@ -13,43 +13,57 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde; +package com.chibde; import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Paint; import ohos.agp.utils.Color; import ohos.app.Context; -import ohos.media.audio.*; - - -import java.util.Arrays; +import ohos.media.audio.AudioWaver; /** * Base class that contains common implementation for all * visualizers. * Created by gautam chibde on 28/10/17. */ -abstract public class BaseVisualizer extends Component { +public abstract class BaseVisualizer extends Component { protected byte[] bytes; protected Paint paint; protected int color; private AudioWaver audioWaver; + /** + * constructor + * + * @param context + */ public BaseVisualizer(Context context) { super(context); init(null); init(); } + /** + * constructor + * + * @param context + * @param attrs + */ public BaseVisualizer(Context context, AttrSet attrs) { super(context, attrs); init(attrs); init(); } - + /** + * constructor + * + * @param context + * @param attrs + * @param defStyleAttr + */ public BaseVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); @@ -60,6 +74,11 @@ abstract public class BaseVisualizer extends Component { paint = new Paint(); } + /** + * init + */ + protected abstract void init(); + /** * Set color to visualizer with color resource id. * @@ -70,17 +89,20 @@ abstract public class BaseVisualizer extends Component { this.paint.setColor(new Color(color)); } + /** + * setPlayer + * + * @param sessionid + * @param name + */ public void setPlayer(int sessionid, String name) { - audioWaver = new AudioWaver(sessionid, name); audioWaver.setActivated(false); audioWaver.setDataSize(AudioWaver.getMaxDataSize()); audioWaver.setWaveDataObserver(new AudioWaver.WaveDataObserver() { @Override public void onWaveData(byte[] bytes, int i) { - BaseVisualizer.this.bytes = bytes; - //刷新 getContext().getUITaskDispatcher().asyncDispatch(new Runnable() { @Override public void run() { @@ -90,27 +112,22 @@ abstract public class BaseVisualizer extends Component { } }, AudioWaver.getMinInterval()); audioWaver.setActivated(true); - - } @Override public void release() { super.release(); - //will be null if setPlayer hasn't yet been called - if (audioWaver == null) + + // will be null if setPlayer hasn't yet been called + if (audioWaver == null) { return; + } audioWaver.release(); bytes = null; invalidate(); } - public AudioWaver getVisualizer() { return audioWaver; } - - protected abstract void init(); - - } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/BarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/BarVisualizer.java index a5e605aa153236e9ff3dac32ef4b1a715c488a30..4db5a082f95a0279a528f6c9d468164f6f1c5882 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/BarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/BarVisualizer.java @@ -1,22 +1,23 @@ /* -* Copyright (C) 2017 Gautam Chibde -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package com.chibde.visualizer; + * Copyright (C) 2017 Gautam Chibde + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.chibde.visualizer; import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -26,25 +27,34 @@ import ohos.app.Context; /** * Custom component that creates a Bar visualizer effect for - * + *

* Created by gautam chibde on 28/10/17. */ - public class BarVisualizer extends BaseVisualizer implements Component.DrawTask { private float density = 50; private int gap; + /** + * constructor + * + * @param context cn + */ public BarVisualizer(Context context) { super(context); addDrawTask(this); } + /** + * constructor + * + * @param context + * @param attrs + */ public BarVisualizer(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this); } - @Override protected void init() { this.density = 50; @@ -67,6 +77,7 @@ public class BarVisualizer extends BaseVisualizer implements Component.DrawTask this.density = 10; } } + @Override public void onDraw(Component component, Canvas canvas) { if (bytes != null) { @@ -75,16 +86,13 @@ public class BarVisualizer extends BaseVisualizer implements Component.DrawTask paint.setStrokeWidth(barWidth - gap); for (int i = 0; i < density; i++) { int bytePosition = (int) Math.ceil(i * div); - int top = getHeight() + - ((byte) (Math.abs(bytes[bytePosition]) + 128)) * getHeight() / 128; + int top = getHeight() + ((byte) (Math.abs(bytes[bytePosition]) + 128)) * getHeight() / 128; float barX = (i * barWidth) + (barWidth / 2); canvas.drawLine(barX, getHeight(), barX, top, paint); - - Point points =new Point(barX,getHeight()); - Point pointss =new Point(barX,top); - canvas.drawLine(points,pointss,paint); + Point points = new Point(barX, getHeight()); + Point pointss = new Point(barX, top); + canvas.drawLine(points, pointss, paint); } - //super.onDraw(component,canvas); } } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/BlazingColorVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/BlazingColorVisualizer.java index 3355e67d5286b4484cf8edc4757dcce923b8a72c..ae8bd4af49d8db1ce4b79d2b5517ea928c4137ab 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/BlazingColorVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/BlazingColorVisualizer.java @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.chibde.visualizer; import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -26,29 +28,38 @@ import ohos.agp.utils.Color; import ohos.agp.utils.Point; import ohos.app.Context; -/** - * TODO - * Created by gautam chibd e on 29/10/17. - */ - -class BlazingColorVisualizer extends BaseVisualizer implements Component.DrawTask { +public class BlazingColorVisualizer extends BaseVisualizer implements Component.DrawTask { private Shader shader; - // private ShapeElement shapeElement; + /** + * constructor + * + * @param context + */ public BlazingColorVisualizer(Context context) { super(context); addDrawTask(this); } - public BlazingColorVisualizer(Context context, - AttrSet attrs) { + /** + * constructor + * + * @param context + * @param attrs + */ + public BlazingColorVisualizer(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this); } - public BlazingColorVisualizer(Context context, - AttrSet attrs, - int defStyleAttr) { + /** + * constructor + * + * @param context + * @param attrs + * @param defStyleAttr + */ + public BlazingColorVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); addDrawTask(this); } @@ -62,14 +73,12 @@ class BlazingColorVisualizer extends BaseVisualizer implements Component.DrawTas shader = new LinearShader(point, point1, colors, Shader.TileMode.MIRROR_TILEMODE); } - @Override public void onDraw(Component component, Canvas canvas) { if (bytes != null) { paint.setShader(shader, Paint.ShaderType.LINEAR_SHADER); for (int i = 0, k = 0; i < (bytes.length - 1) && k < bytes.length; i++, k++) { - int top = getHeight() + - ((byte) (Math.abs(bytes[k]) + 128)) * getHeight() / 128; + int top = getHeight() + ((byte) (Math.abs(bytes[k]) + 128)) * getHeight() / 128; Point points = new Point(i, getHeight()); Point pointss = new Point(i, top); canvas.drawLine(points, pointss, paint); diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java index 50db97a9e354d21049360084613e9d9c5a6e936c..92fafe82fe391b295e249e3045612ce09dd2548b 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java @@ -1,21 +1,23 @@ /* -* Copyright (C) 2017 Gautam Chibde -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright (C) 2017 Gautam Chibde + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.chibde.visualizer; + import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -25,26 +27,43 @@ import ohos.app.Context; /** * Custom component that creates a Circle and Bar visualizer effect for - * + *

* Created by gautam chibde on 20/11/17. */ - public class CircleBarVisualizer extends BaseVisualizer implements Component.DrawTask { private float[] points; private Paint circlePaint; private int radius; + /** + * constructor + * + * @param context + */ public CircleBarVisualizer(Context context) { super(context); addDrawTask(this::onDraw); } + /** + * constructor + * + * @param context + * @param attrs + */ public CircleBarVisualizer(Context context, - AttrSet attrs) { + AttrSet attrs) { super(context, attrs); addDrawTask(this::onDraw); } + /** + * constructor + * + * @param context + * @param attrs + * @param defStyleAttr + */ public CircleBarVisualizer(Context context, AttrSet attrs, int defStyleAttr) { @@ -76,30 +95,15 @@ public class CircleBarVisualizer extends BaseVisualizer implements Component.Dra points = new float[bytes.length * 4]; } double angle = 0; - for (int i = 0; i < 120; i++, angle += 3) { int x = (int) Math.ceil(i * 8.5); int t = ((byte) (-Math.abs(bytes[x]) + 128)) * (getHeight() / 4) / 128; - - points[i * 4] = (float) (getWidth() / 2 - + radius - * Math.cos(Math.toRadians(angle))); - - points[i * 4 + 1] = (float) (getHeight() / 2 - + radius - * Math.sin(Math.toRadians(angle))); - - points[i * 4 + 2] = (float) (getWidth() / 2 - + (radius + t) - * Math.cos(Math.toRadians(angle))); - - points[i * 4 + 3] = (float) (getHeight() / 2 - + (radius + t) - * Math.sin(Math.toRadians(angle))); + points[i * 4] = (float) (getWidth() / 2 + radius * Math.cos(Math.toRadians(angle))); + points[i * 4 + 1] = (float) (getHeight() / 2 + radius * Math.sin(Math.toRadians(angle))); + points[i * 4 + 2] = (float) (getWidth() / 2 + (radius + t) * Math.cos(Math.toRadians(angle))); + points[i * 4 + 3] = (float) (getHeight() / 2 + (radius + t) * Math.sin(Math.toRadians(angle))); } - canvas.drawLines(points, paint); } - } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java index b261cee6a087dcb6b67c062fc76543b0dee124d0..e9e2b5973e104baa816a0354fd19a0092e0ce2dd 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java @@ -15,7 +15,9 @@ */ package com.chibde.visualizer; + import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -30,19 +32,24 @@ import java.util.Map; *

* Created by gautam chibde on 20/11/17. Smooth effect added by Ali heidari */ - public class CircleBarVisualizerSmooth extends BaseVisualizer implements Component.DrawTask { - private final static float _StepsCount = 2; - private final static int _BarCount = 120; - private final static float _AngleStep = 360f / _BarCount; + private static final float _StepsCount = 2; + private static final int _BarCount = 120; + private static final float _AngleStep = 360f / _BarCount; private float[] points; private float[] endPoints; private float[] diffs; + // Stores radius and step-counter which every invoking of "onDraw" requires them private Map configs = null; - public CircleBarVisualizerSmooth(Context context, - AttrSet attrs) { + /** + * constructor + * + * @param context + * @param attrs + */ + public CircleBarVisualizerSmooth(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this); } @@ -54,46 +61,55 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone /* * Returns the value of given configuration-key with handling - * @see java.lang#NullPointerException + * + * @see java.lang#NullPointerException */ private int getConfig(String key) { Object obj = configs.get(key); - if (obj != null) + if (obj != null) { return (int) obj; - else + } else { return 0; + } } /* - *set new value of given configuration-key + * set new value of given configuration-key */ private void setConfig(String key, int value) { configs.put(key, value); } /* - *Get smaller dimension of visualizer + * Get smaller dimension of visualizer */ private int getSmallerDimen() { - if (getHeight() < getWidth()) return getHeight(); - else return getWidth(); + if (getHeight() < getWidth()) { + return getHeight(); + } else { + return getWidth(); + } } /* * Fill the initial configurations */ private void fillConfigs() { - if (configs != null) + if (configs != null) { return; + } configs = new HashMap<>(); + // Calculates the radius of center circle. // Formula disclaimer : 0.65 = 3.14 * 0.02 int radius = (int) (getSmallerDimen() * 0.65 / 2) * 6 / 10; + // Width of each bar double circumference = 1.5 * Math.PI * radius; paint.setStrokeWidth((float) (circumference / _BarCount)); - // Store initial configs - configs.put("needsInit", 1);//0 = false, 1 = true + + // 0 = false, 1 = true + configs.put("needsInit", 1); configs.put("radius", radius); configs.put("stepCounter", 0); } @@ -108,14 +124,15 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone // It needs to multiply by 4 because for every byte should be // StartX,StartY,EndX,EndY points = new float[bytes.length * 4]; + // It needs to multiply by 4 because for every byte should be EndX,EndY,OldEndX,OldEndY endPoints = new float[bytes.length * 4]; + // It needs to multiply by 2 because there are X and Y differences diffs = new float[bytes.length * 2]; } } - /* * Fill the points for end of each bar. * Only needs to calculate the end of bar-line, because starting is not changing @@ -123,10 +140,12 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone private void fillPoints(int round, int i) { int indexM2 = i * 2; int indexM4 = i * 4; + // Increase/Decrease the length of bar so oldEnd can match with ends if (round <= _StepsCount) { // Find endX to be drawn points[indexM4 + 2] = endPoints[indexM4 + 2] + diffs[indexM2] * round; + // Find endX to be drawn points[indexM4 + 3] = endPoints[indexM4 + 3] + diffs[indexM2 + 1] * round; } @@ -139,8 +158,10 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone // Set the old ends before assign new value the ends endPoints[i * 4 + 2] = endPoints[i * 4]; endPoints[i * 4 + 3] = endPoints[i * 4 + 1]; + // Find endX endPoints[i * 4] = newX; + // Find endY endPoints[i * 4 + 1] = newY; @@ -148,6 +169,7 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone if (getConfig("needsInit") == 0) { // Find differences of Xs diffs[i * 2] = (endPoints[i * 4] - endPoints[i * 4 + 2]) / _StepsCount; + // Find differences of Ys diffs[i * 2 + 1] = (endPoints[i * 4 + 1] - endPoints[i * 4 + 3]) / _StepsCount; } else { @@ -157,35 +179,16 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone } } - /* - * Calculates the points of each round. Round represents amount of decrease/increase the length of bar - */ - private void calcRound(int i, double angle) { - // Calculates ceiling regarded to bytes length. The ceiling is a coefficient for - // byte indexer. - // Because we have 120 bars, so the buffer should be filtered and only 120 bytes - // from the buffer will have chosen to be shown. - // Get length of bar - int t = getBarLength(i, (bytes.length - bytes.length % 4) / _BarCount); - // Find the round by - int round = (int) (getConfig("stepCounter") % _StepsCount); - if (round == 0) { - float radius_p_t = getConfig("radius") + t; - //Fill the endPoints and differences - this.fillEndPointsAndDiffs(i, (float) (getWidth() / 2 + radius_p_t * Math.cos(angle)), (float) (getHeight() / 2 + radius_p_t * Math.sin(angle))); - } - // Fill points - this.fillPoints(round, i); - } - /* * Calculates the legth of bar */ private int getBarLength(int i, float ceiling) { // Find the index of byte inside buffer int x = (int) Math.ceil(i * ceiling); + // Change the sign of byte byte a = (byte) (-Math.abs(bytes[x]) + 128); + // Gets the length of the line return a * (getHeight() / 4) / 128; } @@ -195,15 +198,15 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone */ private void fillStartingPoints(int i, double angle) { int indexM4 = i * 4; + // First time calculates the startX and startY for every byte if (getConfig("needsInit") == 1) { // Find startX points[indexM4] = (float) (this.getWidth() / 2 + getConfig("radius") * Math.cos(angle)); + // Find startY points[indexM4 + 1] = (float) (this.getHeight() / 2 + getConfig("radius") * Math.sin(angle)); } - // Calculates points for current round - } /* @@ -215,24 +218,25 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone * Differences / 3 Finally when OldEnd(s) matched to End(s) Need to set End with * OldEnd value And the action will be repeated until visualizer is running. */ - - /* * Reset configs */ private void resetConfigs() { // The stepCounter increases setConfig("stepCounter", getConfig("stepCounter") + 1); + // Initialized, no longer need initializing - if (getConfig("needsInit") == 1) + if (getConfig("needsInit") == 1) { setConfig("needsInit", 0); + } } @Override public void onDraw(Component component, Canvas canvas) { // Check if bytes initiated before - if (bytes == null) + if (bytes == null) { return; + } // Init configs fillConfigs(); @@ -242,18 +246,16 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer implements Compone // We start with angle 0 and go against clock's direction double angle = 0; + // Calculates every points and iterate along increasing angle for (int i = 0; i < _BarCount; i++, angle += _AngleStep) { - // Convert to radians double radianAngle = Math.toRadians(angle); - this.fillStartingPoints(i, radianAngle); } - if (getConfig("needsInit") == 0) + if (getConfig("needsInit") == 0) { canvas.drawLines(points, paint); - - //super.onDraw(canvas); + } // Resets configurations variable for next calling of onDraw this.resetConfigs(); diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java index 46a534cd6b4d8bff6c5b0f50525afe6bb20a7a95..4fac6b4081aa67db7a16bc40b89c10fe3cb4e702 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.chibde.visualizer; import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -31,16 +33,34 @@ public class CircleVisualizer extends BaseVisualizer implements Component.DrawTa private float radiusMultiplier = 1; private float strokeWidth = 0.005f; + /** + * constructor + * + * @param context + */ public CircleVisualizer(Context context) { super(context); addDrawTask(this); } + /** + * constructor + * + * @param context + * @param attrs + */ public CircleVisualizer(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this); } + /** + * constructor + * + * @param context + * @param attrs + * @param defStyleAttr + */ public CircleVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); addDrawTask(this); diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java index 9d481de7cf48b91fd11596f904d18ff1b3d7c68f..b89e2adebb529a7ac4701238cec1d253e1dd5e46 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java @@ -1,20 +1,23 @@ /* -* Copyright (C) 2017 Gautam Chibde -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright (C) 2017 Gautam Chibde + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.chibde.visualizer; + import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -28,22 +31,39 @@ import ohos.app.Context; *

* Created by gautam chibde on 22/11/17. */ - public class LineBarVisualizer extends BaseVisualizer implements Component.DrawTask { private Paint middleLine; private float density; private int gap; + /** + * constructor + * + * @param context + */ public LineBarVisualizer(Context context) { super(context); addDrawTask(this::onDraw); } + /** + * constructor + * + * @param context + * @param attrs + */ public LineBarVisualizer(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this::onDraw); } + /** + * constructor + * + * @param context + * @param attrs + * @param defStyleAttr + */ public LineBarVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); addDrawTask(this::onDraw); @@ -80,7 +100,6 @@ public class LineBarVisualizer extends BaseVisualizer implements Component.DrawT } } - @Override public void onDraw(Component component, Canvas canvas) { if (middleLine.getColor() != Color.BLUE) { @@ -89,9 +108,9 @@ public class LineBarVisualizer extends BaseVisualizer implements Component.DrawT if (bytes != null) { float barWidth = getWidth() / density; float div = bytes.length / density; - Point point=new Point(0,getHeight()/2); - Point point11=new Point(getWidth(),getHeight()/2); - canvas.drawLine(point,point11,middleLine); + Point point = new Point(0, getHeight() / 2); + Point point11 = new Point(getWidth(), getHeight() / 2); + canvas.drawLine(point, point11, middleLine); paint.setStrokeWidth(barWidth - gap); for (int i = 0; i < density; i++) { @@ -105,14 +124,13 @@ public class LineBarVisualizer extends BaseVisualizer implements Component.DrawT * (getHeight() / 2) / 128; float barX = (i * barWidth) + (barWidth / 2); - Point points =new Point(barX,bottom); - Point pointss =new Point(barX,getHeight()/2); - canvas.drawLine(points,pointss, paint); - Point point1 =new Point(barX,top); - Point point2 =new Point(barX,getHeight()/2); - canvas.drawLine(point1,point2, paint); + Point points = new Point(barX, bottom); + Point pointss = new Point(barX, getHeight() / 2); + canvas.drawLine(points, pointss, paint); + Point point1 = new Point(barX, top); + Point point2 = new Point(barX, getHeight() / 2); + canvas.drawLine(point1, point2, paint); } - // super.onDraw(canvas); } } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/LineVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/LineVisualizer.java index 476f0b6d96ae67a850877bd7b6e9e837d2a3150b..06d18328c5a3e207d221c5ad06c0bd55fa00d478 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/LineVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/LineVisualizer.java @@ -1,20 +1,23 @@ /* -* Copyright (C) 2017 Gautam Chibde -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright (C) 2017 Gautam Chibde + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.chibde.visualizer; + import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -23,26 +26,43 @@ import ohos.app.Context; /** * Custom component that creates a Bar visualizer effect fo - * + *

* Created by gautam chibde on 28/10/17. */ - public class LineVisualizer extends BaseVisualizer implements Component.DrawTask { private float[] points; private Rect rect = new Rect(); private float strokeWidth = 0.005f; + /** + * constructor + * + * @param context context + */ public LineVisualizer(Context context) { super(context); addDrawTask(this::onDraw); } + /** + * constructor + * + * @param context context + * @param attrs attrs + */ public LineVisualizer(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this::onDraw); } + /** + * constructor + * + * @param context context + * @param attrs attrs + * @param defStyleAttr defStyleAttr + */ public LineVisualizer(Context context, AttrSet attrs, int defStyleAttr) { diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java index 1bc0d91bba5d5dc3f0625c107d73ede2496aef61..73d28ee6a1f1d3c2a18e1ad69caa3fcffb2f8a3c 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.chibde.visualizer; import com.chibde.BaseVisualizer; + import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.render.Canvas; @@ -28,26 +30,40 @@ import ohos.app.Context; *

* Created by gautam chibde on 28/10/17. */ - public class SquareBarVisualizer extends BaseVisualizer implements Component.DrawTask { - private float density = 16; private int gap; + /** + * 构造函数 + * + * @param context context + */ public SquareBarVisualizer(Context context) { super(context); addDrawTask(this); } + /** + * 构造函数 + * + * @param context context + * @param attrs attrs + */ public SquareBarVisualizer(Context context, AttrSet attrs) { super(context, attrs); addDrawTask(this); } - public SquareBarVisualizer(Context context, - AttrSet attrs, - int defStyleAttr) { + /** + * 构造函数 + * + * @param context context + * @param attrs attrs + * @param defStyleAttr defStyleAttr + */ + public SquareBarVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); addDrawTask(this); } @@ -87,7 +103,6 @@ public class SquareBarVisualizer extends BaseVisualizer implements Component.Dra @Override public void onDraw(Component component, Canvas canvas) { - if (bytes != null) { float barWidth = getWidth() / density; float div = bytes.length / density; diff --git a/entry/build.gradle b/entry/build.gradle index b0bc307d1bab87fdc6f81a14df17e003740d4c33..436507197f82f8112f9d702d02f1372bc1ebc1ff 100644 --- a/entry/build.gradle +++ b/entry/build.gradle @@ -2,7 +2,6 @@ apply plugin: 'com.huawei.ohos.hap' apply plugin: 'com.huawei.ohos.decctest' ohos { - compileSdkVersion 5 defaultConfig { compatibleSdkVersion 5 @@ -15,7 +14,4 @@ dependencies { compile project(path: ':audiovisualizer') ohosTestImplementation 'com.huawei.ohos.testkit:runner:1.0.0.100' implementation 'io.openharmony.tpc.thirdlib:XXPermissions:1.0.2' - - - } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/MainAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/MainAbility.java deleted file mode 100644 index ca063d0d9611ed014776c0d990a0d4c9f5c77ae5..0000000000000000000000000000000000000000 --- a/entry/src/main/java/com/chibde/audiovisualizer/MainAbility.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.chibde.audiovisualizer; - -import com.chibde.audiovisualizer.slice.MainAbilitySlice; -import ohos.aafwk.ability.Ability; -import ohos.aafwk.content.Intent; - -public class MainAbility extends Ability { - @Override - public void onStart(Intent intent) { - super.onStart(intent); - super.setMainRoute(MainAbilitySlice.class.getName()); - } -} diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java index d10cd02fc6ed6934619f295fa9184f0dbda63191..c0ca504a5b761d7631c0911723ffc8e1c6324657 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java @@ -13,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample; +package com.chibde.audiovisualizer.sample; import com.chibde.audiovisualizer.util.PlaySoundUtil; import com.hjq.permissions.OnPermission; import com.hjq.permissions.XXPermissions; - import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.agp.utils.Color; @@ -34,96 +33,78 @@ import java.util.List; *

* Created by gautam chibde on 18/11/17. */ -abstract public class BaseAbility extends Ability { - +public abstract class BaseAbility extends Ability { protected PlaySoundUtil playSoundUtil; @Override protected void onStart(Intent intent) { super.onStart(intent); - if (getLayout() != 0) { setUIContent(getLayout()); - } else { - throw new NullPointerException("Provide layout file for the "); } WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#303F9F")); - //权限 - getPermissions(); + // 权限 + getPermissions(); } private void getPermissions() { - XXPermissions.with(BaseAbility.this) - .permission("ohos.permission.MICROPHONE") - .request(new OnPermission() { - @Override - public void hasPermission(List list, boolean b) { - if (b) { - - initialize(); - } else { - terminateAbility(); - } - } + XXPermissions.with(BaseAbility.this).permission("ohos.permission.MICROPHONE").request(new OnPermission() { + @Override + public void hasPermission(List list, boolean b) { + if (b) { + initialize(); + } else { + terminateAbility(); + } + } - @Override - public void noPermission(List list, boolean b) { - if (b) { - //toast("被永久拒绝授权,请手动授予拍照权限"); - // 如果是被永久拒绝就跳转到应用权限系统设置页面 - String[] permissions = new String[list.size()]; - list.toArray(permissions); - XXPermissions.startPermissionActivity(BaseAbility.this, permissions, this); - } else { - //toast("获取拍照权限失败"); - new ToastDialog(getContext()).setText("获取权限失败").show(); - terminateAbility(); - } - } - }); + @Override + public void noPermission(List list, boolean b) { + if (b) { + // 如果是被永久拒绝就跳转到应用权限系统设置页面 + String[] permissions = new String[list.size()]; + list.toArray(permissions); + XXPermissions.startPermissionActivity(BaseAbility.this, permissions, this); + } else { + new ToastDialog(getContext()).setText("获取权限失败").show(); + terminateAbility(); + } + } + }); } private void initialize() { setPlayer(); } - //播放音乐的方法 + // 播放音乐的方法 private void setPlayer() { try { playSoundUtil = new PlaySoundUtil(); - } catch (IOException e) { - e.printStackTrace(); - + e.fillInStackTrace(); } - init(); } - @Override protected void onStop() { super.onStop(); - if (playSoundUtil != null) { - if (playSoundUtil.getAudioRenderer() != null) { - if (playSoundUtil.getAudioRenderer().getState() != null) { - playSoundUtil.getAudioRenderer().stop();//暂停 - playSoundUtil.getAudioRenderer().release();//释放 - } - - } + if (playSoundUtil != null + && playSoundUtil.getAudioRenderer() != null + && playSoundUtil.getAudioRenderer().getState() != null) { + playSoundUtil.getAudioRenderer().stop(); + playSoundUtil.getAudioRenderer().release(); } - - } - protected int getLayout() { return 0; } + /** + * init + */ public abstract void init(); - - } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/MainAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/MainAbility.java index 11325f25f710232cac22a7a7e93696107ed6e3ff..2178128fe8dcff7cc79ba9208990c8439c8f60c1 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/MainAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/MainAbility.java @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample; +package com.chibde.audiovisualizer.sample; import com.chibde.audiovisualizer.sample.visualizer.BarVisualizerAbility; import com.chibde.audiovisualizer.sample.visualizer.CircleBarVisualizerAbility; @@ -22,7 +22,8 @@ import com.chibde.audiovisualizer.sample.visualizer.CircleVisualizerAbility; import com.chibde.audiovisualizer.sample.visualizer.LineBarVisualizerAbility; import com.chibde.audiovisualizer.sample.visualizer.LineVisualizerAbility; import com.chibde.audiovisualizer.sample.visualizer.SquareBarVisualizerAbility; -import com.chibde.visualizer.ResourceTable;; +import com.chibde.visualizer.ResourceTable; + import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.aafwk.content.Operation; @@ -31,11 +32,8 @@ import ohos.agp.components.Component; import ohos.agp.components.Text; import ohos.agp.utils.Color; import ohos.agp.window.service.WindowManager; -import ohos.media.audio.AudioWaver; public class MainAbility extends Ability { - private AudioWaver audioWaver; - @Override public void onStart(Intent intent) { super.onStart(intent); @@ -46,7 +44,7 @@ public class MainAbility extends Ability { title_name.setText("AudioVisualizer"); back.setVisibility(Component.HIDE); - //获取资源ID + // 获取资源ID Button line = (Button) findComponentById(ResourceTable.Id_line); line.setClickedListener(new Component.ClickedListener() { @Override @@ -96,11 +94,14 @@ public class MainAbility extends Ability { square(component); } }); - } + /** + * line + * + * @param component + */ public void line(Component component) { -// startAbility(LineVisualizerAbility.class); Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() .withBundleName(getBundleName()) @@ -110,6 +111,11 @@ public class MainAbility extends Ability { startAbility(intent); } + /** + * bar + * + * @param component + */ public void bar(Component component) { Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() @@ -120,8 +126,12 @@ public class MainAbility extends Ability { startAbility(intent); } + /** + * circle + * + * @param component + */ public void circle(Component component) { - Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() .withBundleName(getBundleName()) @@ -131,8 +141,12 @@ public class MainAbility extends Ability { startAbility(intent); } + /** + * circleBar + * + * @param component + */ public void circleBar(Component component) { - // startAbility(CircleBarVisualizerAbility.class); Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() .withBundleName(getBundleName()) @@ -142,8 +156,12 @@ public class MainAbility extends Ability { startAbility(intent); } + /** + * lineBar + * + * @param component + */ public void lineBar(Component component) { - Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() .withBundleName(getBundleName()) @@ -153,8 +171,12 @@ public class MainAbility extends Ability { startAbility(intent); } + /** + * service + * + * @param component + */ public void service(Component component) { - //startAbility(ServiceExampleAbility.class); Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() .withBundleName(getBundleName()) @@ -164,8 +186,12 @@ public class MainAbility extends Ability { startAbility(intent); } + /** + * square + * + * @param component + */ public void square(Component component) { - // startAbility(SquareBarVisualizerAbility.class); Intent intent = new Intent(); Operation build = new Intent.OperationBuilder() .withBundleName(getBundleName()) @@ -174,5 +200,4 @@ public class MainAbility extends Ability { intent.setOperation(build); startAbility(intent); } - } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/MediaPlayerService.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/MediaPlayerService.java deleted file mode 100644 index 3aafb1a90c98ff63d5073aece850a8270eb72901..0000000000000000000000000000000000000000 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/MediaPlayerService.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.chibde.audiovisualizer.sample; - -import com.chibde.visualizer.ResourceTable; -import ohos.aafwk.ability.Ability; -import ohos.aafwk.content.Intent; -import ohos.media.audio.AudioRenderer; -import ohos.media.audio.AudioRendererInfo; -import ohos.media.audio.AudioStreamInfo; -import ohos.media.common.Source; -import ohos.media.player.Player; -import ohos.rpc.IRemoteObject; -import ohos.rpc.RemoteObject; - -public class MediaPlayerService extends Ability { - - public static final String INTENT_FILTER = "MediaPlayerServiceIntentFilter"; - public static final String INTENT_AUDIO_SESSION_ID = "intent_audio_session_id"; - private IRemoteObject mediaPlayerServiceBinder = new MediaPlayerServiceBinder("MediaPlayerService"); - private AudioRenderer mediaPlayer; - protected AudioStreamInfo audioStreamInfo; - protected AudioRendererInfo audioRendererInfo; - @Override - protected void onStart(Intent intent) { - super.onStart(intent); - -// mediaPlayer = new Player(getContext()); -// Source source = new Source(); -// source.setRecorderAudioSource(ResourceTable.Profile_red_e); -// mediaPlayer.setSource(source); -// mediaPlayer.enableSingleLooping(false); - - String str=""+ResourceTable.Profile_red_e; - byte[] bytes=str.getBytes(); - mediaPlayer= new AudioRenderer(getAudioRendererInfo(), AudioRenderer.PlayMode.MODE_STREAM); - mediaPlayer.write(bytes,0,bytes.length); - mediaPlayer.start();//播放音乐 - -//// Intent intent = new Intent(INTENT_FILTER); //put the same message as in the filter you used in the when registering the receiver -// intent.putExtra(INTENT_AUDIO_SESSION_ID, mediaPlayer.getAudioSessionId()); -// // Send audio session id through -// LocalBroadcastManager.getInstance(this).sendBroadcast(intent); - } - - @Override - protected IRemoteObject onConnect(Intent intent) { - return super.onConnect(intent); - } - - public void replay() { - if (mediaPlayer != null) { - mediaPlayer.setPosition(0); - } - } - - @Override - protected void onDisconnect(Intent intent) { - super.onDisconnect(intent); - } - - @Override - protected void onStop() { - super.onStop(); - if(mediaPlayer!=null){ - mediaPlayer.release();//釋放資源 - } - } - - public boolean isPlaying() { - return mediaPlayer != null && mediaPlayer.getState()!=null; - } - - public void pause() { - if (mediaPlayer != null) { - mediaPlayer.pause(); - } - } - - public void start() { - if (mediaPlayer != null) { - mediaPlayer.start(); - } - } - - public class MediaPlayerServiceBinder extends RemoteObject { - public MediaPlayerServiceBinder(String descriptor) { - super(descriptor); - } - - MediaPlayerService getService() { - return MediaPlayerService.this; - } - } - - public AudioRendererInfo getAudioRendererInfo() { - audioStreamInfo = new AudioStreamInfo.Builder() - .sampleRate(AudioStreamInfo.SAMPLE_RATE_UNSPECIFIED) - .audioStreamFlag(AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_NONE) - .encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_INVALID) - .channelMask(AudioStreamInfo.ChannelMask.CHANNEL_INVALID) - .streamUsage(AudioStreamInfo.StreamUsage.STREAM_USAGE_UNKNOWN) - .build(); - audioRendererInfo = new AudioRendererInfo.Builder().audioStreamInfo(audioStreamInfo) - .audioStreamOutputFlag(AudioRendererInfo.AudioStreamOutputFlag.AUDIO_STREAM_OUTPUT_FLAG_NONE) - .bufferSizeInBytes(0) - .isOffload(false) - .sessionID(AudioRendererInfo.SESSION_ID_UNSPECIFIED) - .build(); - return audioRendererInfo; - } -} diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/ServiceExampleAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/ServiceExampleAbility.java index 91b38f03e5ea08b774643d50b87d7da598ab367e..1db243a87e3642fdc4e909326bc5ac9587f19a36 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/ServiceExampleAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/ServiceExampleAbility.java @@ -2,6 +2,7 @@ package com.chibde.audiovisualizer.sample; import com.chibde.visualizer.BarVisualizer; import com.chibde.visualizer.ResourceTable; + import ohos.agp.components.Component; import ohos.agp.components.Image; import ohos.agp.components.Text; @@ -11,14 +12,14 @@ import ohos.media.audio.AudioRenderer; public class ServiceExampleAbility extends BaseAbility { private BarVisualizer barVisualizer; private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_pause; private Image im_start; private Image im_reset; @Override public void init() { - //获取资源ID + // 获取资源ID barVisualizer = (BarVisualizer) findComponentById(ResourceTable.Id_visualizer); if (barVisualizer != null) { barVisualizer.setColor(Color.BLUE.getValue()); @@ -40,31 +41,22 @@ public class ServiceExampleAbility extends BaseAbility { im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @Override public void run() { - - playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition()); playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); - - } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - } }); im_pause.setClickedListener(new Component.ClickedListener() { @@ -79,15 +71,12 @@ public class ServiceExampleAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_reset.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (isfirst != true) { - + if (!isfirst) { if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { return; } @@ -100,12 +89,11 @@ public class ServiceExampleAbility extends BaseAbility { playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); } catch (InterruptedException e) { - e.printStackTrace(); + e.fillInStackTrace(); } } }).start(); } - } }); } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/BarVisualizerAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/BarVisualizerAbility.java index ceef5437414c98dd4eb65b4526d0c86cac1dd873..9ab84e18aa5028b130a123d5239f30ae68140eec 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/BarVisualizerAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/BarVisualizerAbility.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.chibde.audiovisualizer.sample.visualizer; import com.chibde.audiovisualizer.sample.BaseAbility; @@ -26,25 +27,22 @@ import ohos.media.audio.AudioRenderer; public class BarVisualizerAbility extends BaseAbility { private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_pause; @Override public void init() { - - // BarVisualizer barVisualizer = findViewById(R.id.visualizer); BarVisualizer barVisualizer = (BarVisualizer) findComponentById(ResourceTable.Id_visualizer); // set custom color to the line. - if (null != barVisualizer) { + if (barVisualizer != null) { barVisualizer.setColor(Color.BLUE.getValue()); barVisualizer.setDensity(40); barVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); - // barVisualizer.setPlayer(mediaPlayer.getRendererSessionId()); } - // define custom number of bars you want in the visualizer between (10 - 256). - // Set your media player to the visualizer. + // define custom number of bars you want in the visualizer between (10 - 256). + // Set your media player to the visualizer. Image im_start = (Image) findComponentById(ResourceTable.Id_im_start); Image im_reset = (Image) findComponentById(ResourceTable.Id_im_reset); im_pause = (Image) findComponentById(ResourceTable.Id_im_pause); @@ -69,69 +67,55 @@ public class BarVisualizerAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @Override public void run() { - - playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition()); playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); - - } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - } }); im_reset.setClickedListener(new Component.ClickedListener() { - @Override - public void onClick(Component component) { - if (isfirst != true) { - - if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { - return; - } - new Thread(new Runnable() { - @Override - public void run() { - playSoundUtil.getAudioRenderer().pause(); - try { - Thread.sleep(100); - playSoundUtil.getAudioRenderer().start(); - playSoundUtil.loadSound("Ring10.wav"); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - }).start(); - } - - } - } + @Override + public void onClick(Component component) { + if (!isfirst) { + if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { + return; + } + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + try { + Thread.sleep(100); + playSoundUtil.getAudioRenderer().start(); + playSoundUtil.loadSound("Ring10.wav"); + } catch (InterruptedException e) { + e.fillInStackTrace(); + } + } + }).start(); + } + } + } ); } - @Override protected int getLayout() { return ResourceTable.Layout_ability_bar_visualizer; diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleBarVisualizerAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleBarVisualizerAbility.java index 7d88b16ff0c50526f1fff61de3ce02835c63da32..a952a711b93c12cc2a6d2d61814bacfa8878aa54 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleBarVisualizerAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleBarVisualizerAbility.java @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample.visualizer; +package com.chibde.audiovisualizer.sample.visualizer; import com.chibde.audiovisualizer.sample.BaseAbility; import com.chibde.visualizer.CircleBarVisualizer; @@ -27,21 +27,16 @@ import ohos.media.audio.AudioRenderer; public class CircleBarVisualizerAbility extends BaseAbility { private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_pause; @Override public void init() { - CircleBarVisualizer circleBarVisualizer = (CircleBarVisualizer) findComponentById(ResourceTable.Id_visualizer); - //判断一下 if (circleBarVisualizer != null) { circleBarVisualizer.setColor(Color.BLUE.getValue()); - circleBarVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); } - - // zanting Image im_start = (Image) findComponentById(ResourceTable.Id_im_start); Image im_reset = (Image) findComponentById(ResourceTable.Id_im_reset); im_pause = (Image) findComponentById(ResourceTable.Id_im_pause); @@ -57,31 +52,22 @@ public class CircleBarVisualizerAbility extends BaseAbility { im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @Override public void run() { - - playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition()); playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); - - } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - } }); im_pause.setClickedListener(new Component.ClickedListener() { @@ -96,15 +82,12 @@ public class CircleBarVisualizerAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_reset.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (isfirst != true) { - + if (!isfirst) { if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { return; } @@ -117,18 +100,15 @@ public class CircleBarVisualizerAbility extends BaseAbility { playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); } catch (InterruptedException e) { - e.printStackTrace(); + e.fillInStackTrace(); } } }).start(); } - } }); - } - @Override protected int getLayout() { return ResourceTable.Layout_ability_circle_bar_visualizer; diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleVisualizerAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleVisualizerAbility.java index dbe9b43c2218b2dc14d2729306c25c17854c04dd..017804e9ae881c8b656378f16c5b549b3afe971b 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleVisualizerAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleVisualizerAbility.java @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample.visualizer; +package com.chibde.audiovisualizer.sample.visualizer; import com.chibde.audiovisualizer.sample.BaseAbility; import com.chibde.visualizer.CircleVisualizer; @@ -27,7 +27,7 @@ import ohos.media.audio.AudioRenderer; public class CircleVisualizerAbility extends BaseAbility { private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_pause; @Override @@ -38,17 +38,12 @@ public class CircleVisualizerAbility extends BaseAbility { @Override public void init() { CircleVisualizer circleVisualizer = (CircleVisualizer) findComponentById(ResourceTable.Id_visualizer); - // set custom color to the line. if (circleVisualizer != null) { circleVisualizer.setColor(Color.BLUE.getValue()); - // Customize the size of the circle. by defalut multipliers is 1. circleVisualizer.setRadiusMultiplier(2f); - // set the line with for the visualizer between 1-10 default 1. circleVisualizer.setStrokeWidth(1); circleVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); } - // Set your media player to the visualizer. - // zanting Image im_start = (Image) findComponentById(ResourceTable.Id_im_start); Image im_reset = (Image) findComponentById(ResourceTable.Id_im_reset); Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); @@ -64,31 +59,22 @@ public class CircleVisualizerAbility extends BaseAbility { im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @Override public void run() { - - playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition()); playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); - - } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - } }); im_pause.setClickedListener(new Component.ClickedListener() { @@ -103,15 +89,12 @@ public class CircleVisualizerAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_reset.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (isfirst != true) { - + if (!isfirst) { if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { return; } @@ -124,12 +107,11 @@ public class CircleVisualizerAbility extends BaseAbility { playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); } catch (InterruptedException e) { - e.printStackTrace(); + e.fillInStackTrace(); } } }).start(); } - } }); } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineBarVisualizerAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineBarVisualizerAbility.java index c871d8d6b4702f7d6b174481aa449b1c1a686ba9..10077d4fbf3c70e9ad8d47147e69d3d78a3adedc 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineBarVisualizerAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineBarVisualizerAbility.java @@ -13,24 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample.visualizer; +package com.chibde.audiovisualizer.sample.visualizer; import com.chibde.audiovisualizer.sample.BaseAbility; import com.chibde.visualizer.LineBarVisualizer; import com.chibde.visualizer.ResourceTable; + import ohos.agp.components.Component; import ohos.agp.components.Image; import ohos.agp.components.Text; import ohos.agp.utils.Color; - import ohos.media.audio.AudioRenderer; public class LineBarVisualizerAbility extends BaseAbility { - - private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_start; private Image im_reset; private Image im_pause; @@ -43,14 +41,11 @@ public class LineBarVisualizerAbility extends BaseAbility { @Override public void init() { LineBarVisualizer lineBarVisualizer = (LineBarVisualizer) findComponentById(ResourceTable.Id_visualizer); - // set custom color to the line. if (lineBarVisualizer != null) { lineBarVisualizer.setColor(Color.BLUE.getValue()); lineBarVisualizer.setDensity(90f); lineBarVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); } - - // zanting Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); title_name.setText("Line Bar Visualizer"); Component back = findComponentById(ResourceTable.Id_im_back); @@ -75,46 +70,33 @@ public class LineBarVisualizerAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @Override public void run() { - - playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition()); playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); - - } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - } }); - im_reset.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (isfirst != true) { - + if (!isfirst) { if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { return; } @@ -127,12 +109,11 @@ public class LineBarVisualizerAbility extends BaseAbility { playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); } catch (InterruptedException e) { - e.printStackTrace(); + e.fillInStackTrace(); } } }).start(); } - } }); } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineVisualizerAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineVisualizerAbility.java index 1b6c5aa8a1e4b85fae7dd3450c10c5c63785c736..1b96753eac9ec7f2c509c2259e16e5073e6819a9 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineVisualizerAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineVisualizerAbility.java @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample.visualizer; +package com.chibde.audiovisualizer.sample.visualizer; import com.chibde.audiovisualizer.sample.BaseAbility; import com.chibde.visualizer.LineVisualizer; import com.chibde.visualizer.ResourceTable; + import ohos.agp.components.Component; import ohos.agp.components.Image; import ohos.agp.components.Text; @@ -26,9 +27,8 @@ import ohos.agp.utils.Color; import ohos.media.audio.AudioRenderer; public class LineVisualizerAbility extends BaseAbility { - private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_start; private Image im_pause; private Image im_reset; @@ -70,17 +70,13 @@ public class LineVisualizerAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @@ -91,23 +87,16 @@ public class LineVisualizerAbility extends BaseAbility { playSoundUtil.loadSound("Ring10.wav"); } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - - } }); - - im_reset.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (isfirst != true) { - + if (!isfirst) { if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { return; } @@ -120,34 +109,13 @@ public class LineVisualizerAbility extends BaseAbility { playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); } catch (InterruptedException e) { - e.printStackTrace(); + e.fillInStackTrace(); } } }).start(); } } }); - // handler(); } - - private int i = 0; -// EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner()); -// -// public void handler() { -// eventHandler.postTask(new Runnable() { -// @Override -// public void run() { -// if (i < Integer.MAX_VALUE) { -// System.out.println("播放state===" + playSoundUtil.getAudioRenderer().getState()); -// if (playSoundUtil.getAudioRenderer().getState() == AudioRenderer.State.STATE_PLAYING) { -// im_start.setVisibility(Component.VISIBLE); -// im_pause.setVisibility(Component.HIDE); -// } -// handler(); -// } -// } -// }, 1000); -// -// } } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/SquareBarVisualizerAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/SquareBarVisualizerAbility.java index eee4c1380fe75b8aa72472f597080fc71df7211e..50ca2c03f932224fd49dc4fda0177e0cf29a1b4c 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/SquareBarVisualizerAbility.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/SquareBarVisualizerAbility.java @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.chibde.audiovisualizer.sample.visualizer; +package com.chibde.audiovisualizer.sample.visualizer; import com.chibde.audiovisualizer.sample.BaseAbility; import com.chibde.visualizer.ResourceTable; @@ -27,10 +27,9 @@ import ohos.media.audio.AudioRenderer; public class SquareBarVisualizerAbility extends BaseAbility { private boolean isfirst = true; - private long currentClick = 0; + private long currentClick = 0L; private Image im_pause; - //初始化 @Override public void init() { SquareBarVisualizer squareBarVisualizer = (SquareBarVisualizer) findComponentById(ResourceTable.Id_visualizer); @@ -40,6 +39,7 @@ public class SquareBarVisualizerAbility extends BaseAbility { squareBarVisualizer.setGap(2); squareBarVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); } + // zanting Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); title_name.setText("Square Bar Visualizer"); @@ -56,10 +56,8 @@ public class SquareBarVisualizerAbility extends BaseAbility { im_start.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (currentClick != 0) { - if (System.currentTimeMillis() - currentClick <= 1000) { - return; - } + if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) { + return; } currentClick = System.currentTimeMillis(); Thread thread = new Thread(new Runnable() { @@ -68,16 +66,12 @@ public class SquareBarVisualizerAbility extends BaseAbility { playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition()); playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); - } }); - thread.start(); - isfirst = false; im_start.setVisibility(Component.HIDE); im_pause.setVisibility(Component.VISIBLE); - } }); im_pause.setClickedListener(new Component.ClickedListener() { @@ -92,15 +86,12 @@ public class SquareBarVisualizerAbility extends BaseAbility { isfirst = true; im_start.setVisibility(Component.VISIBLE); im_pause.setVisibility(Component.HIDE); - - } }); im_reset.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { - if (isfirst != true) { - + if (!isfirst) { if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) { return; } @@ -113,21 +104,17 @@ public class SquareBarVisualizerAbility extends BaseAbility { playSoundUtil.getAudioRenderer().start(); playSoundUtil.loadSound("Ring10.wav"); } catch (InterruptedException e) { - e.printStackTrace(); + e.fillInStackTrace(); } } }).start(); } } }); - } - @Override protected int getLayout() { return ResourceTable.Layout_ability_square_bar_visualizer; } - - } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/slice/MainAbilitySlice.java b/entry/src/main/java/com/chibde/audiovisualizer/slice/MainAbilitySlice.java index 527791e76b33c478ba6ba7dae464331be176ab99..117d1f615fdf3976b0d356904b2441890a5be222 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/slice/MainAbilitySlice.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/slice/MainAbilitySlice.java @@ -1,6 +1,7 @@ package com.chibde.audiovisualizer.slice; import com.chibde.visualizer.ResourceTable; + import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; @@ -9,16 +10,5 @@ public class MainAbilitySlice extends AbilitySlice { public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); - - } - - @Override - public void onActive() { - super.onActive(); - } - - @Override - public void onForeground(Intent intent) { - super.onForeground(intent); } } diff --git a/entry/src/main/java/com/chibde/audiovisualizer/util/PlaySoundUtil.java b/entry/src/main/java/com/chibde/audiovisualizer/util/PlaySoundUtil.java index bcb75f1d484714bffbba03130a2177dfefa695d1..68a5ec1b8890722731af1a263101c1af7b8acb98 100644 --- a/entry/src/main/java/com/chibde/audiovisualizer/util/PlaySoundUtil.java +++ b/entry/src/main/java/com/chibde/audiovisualizer/util/PlaySoundUtil.java @@ -1,23 +1,25 @@ package com.chibde.audiovisualizer.util; -import ohos.media.audio.*; +import ohos.media.audio.AudioRenderer; +import ohos.media.audio.AudioRendererInfo; +import ohos.media.audio.AudioStreamInfo; import java.io.IOException; import java.io.InputStream; public class PlaySoundUtil { - private AudioStreamInfo audioStreamInfo = null; private AudioRendererInfo audioRendererInfo = null; private AudioRenderer.PlayMode playMode = AudioRenderer.PlayMode.MODE_STREAM; private AudioRenderer audioRenderer = null; - private AudioManager audioManager = null; - private AudioInterrupt audioInterrupt = null; private InputStream soundInputStream = null; - private String fileName = null; + /** + * constructor + * + * @throws IOException + */ public PlaySoundUtil() throws IOException { - System.out.println("音乐播放初始化"); audioStreamInfo = new AudioStreamInfo.Builder().sampleRate(44100) .audioStreamFlag(AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_MAY_DUCK) .encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT) @@ -30,31 +32,26 @@ public class PlaySoundUtil { .isOffload(true) // false表示分段传输buffer并播放,true表示整个音频流一次性传输到HAL层播放 .build(); audioRenderer = new AudioRenderer(audioRendererInfo, playMode); - - } + /** + * loadSound + * + * @param fileName + */ public void loadSound(String fileName) { - this.fileName = fileName; String filePath = String.format("assets/entry/resources/rawfile/%s", fileName); - soundInputStream = this.getClass().getClassLoader().getResourceAsStream(filePath); - - int bufSize = audioRenderer.getBufferFrameSize(); - byte[] buffer = new byte[1024]; int len; - try { - while ((len = soundInputStream.read(buffer, 0, buffer.length)) != -1) { audioRenderer.write(buffer, 0, buffer.length); } soundInputStream.close(); - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + e.fillInStackTrace(); } - } public AudioRenderer getAudioRenderer() {