diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 36630ad35669db5d6c736db2e67c8547e9f7d1fe..0000000000000000000000000000000000000000 --- a/.gitignore +++ /dev/null @@ -1,30 +0,0 @@ -*.*~ -local/ -.idea/ -*.log -*.orig -/build -com_crashlytics_export_strings.xml -local.properties -gradle/ -.gradle/ -.signing/ -.idea/libraries/ -.idea/workspace.xml -.idea/tasks.xml -.idea/.name -.idea/compiler.xml -.idea/copyright/profiles_settings.xml -.idea/encodings.xml -.idea/misc.xml -.idea/modules.xml -.idea/scopes/scope_settings.xml -.idea/vcs.xml -*.iml -.DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -ehthumbs.db -Thumbs.db \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e2cb9db6733387a50c3e7279d63f319f2529608f..0000000000000000000000000000000000000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -dist: trusty - -language: android - -jdk: - - oraclejdk8 - -before_install: - - yes | sdkmanager "platforms;android-30" - - echo yes | android update sdk --filter extra-android-support --no-ui --force > /dev/null - - echo yes | android update sdk --filter extra-android-m2repository --no-ui --force > /dev/null - -android: - components: - - tools - - platform-tools - - build-tools-30.0.3 - - android-30 - - extra - - extra-android-m2repository - - licenses: - - android-sdk-license-.+ - -script: - ./gradlew clean build \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..d5fb8a6b2cdb571d6f413cff6535e6606649020f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.0.1-SNAPSHOT +ohos 第一个版本,完整实现了原库的全部功能 + diff --git a/README.OPENSOURCE b/README.OPENSOURCE new file mode 100644 index 0000000000000000000000000000000000000000..850fa6acd4cc5e7748b0152ed6c8a84f401fcdee --- /dev/null +++ b/README.OPENSOURCE @@ -0,0 +1,20 @@ +[ + + { + + "Name": "android-audio-visualizer", + + "License": "Apache License", + + "License File": "android-audio-visualizer/README.md", + + "Version Number": "Release v2.2.0", + + "Upstream URL": "https://hub.fastgit.org/GautamChibde/android-audio-visualizer", + + "Description": " Audio visualisation for android MediaPlayer " + + + } + +] \ No newline at end of file diff --git a/README.md b/README.md index f1094748ef674e39e9221c3b4364b32075f28915..2624efb89b2405d80cce960b8b5146e5c1cb2b4e 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,87 @@ -

+# ohos-audio-visualizer -[![Build Status](https://travis-ci.com/GautamChibde/android-audio-visualizer.svg?branch=master)](https://travis-ci.org/GautamChibde/android-audio-visualizer) [![codebeat badge](https://codebeat.co/badges/0f34e433-9e0b-44a4-90da-b53d644848b9)](https://codebeat.co/projects/github-com-gautamchibde-android-audio-visualizer-master) [ ![Download](https://api.bintray.com/packages/gautamchibde/Audio-Visualizer/audiovisualizer/images/download.svg) ](https://bintray.com/gautamchibde/Audio-Visualizer/audiovisualizer/_latestVersion) [![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14) -# Demo +#### 项目介绍 +- 项目名称:ohos-audio-visualizer +- 所属系列:openharmony的第三方组件适配移植 +- 功能:实现音频可视化 +- 项目移植状态:主功能完成 +- 调用差异:无 +- 开发版本:sdk5,DevEco Studio2.1 beta4(实时更新以当前最新版本为准) +- 基线版本:Release v2.2.0 -![Alt text](http://res.cloudinary.com/dvkxfgprc/image/upload/c_scale,w_440/v1511428471/giphy_6_usdiet.gif) ![Alt text](http://res.cloudinary.com/dvkxfgprc/image/upload/c_scale,w_440/v1511431630/giphy_10_yye0fe.gif) ![Alt text](http://res.cloudinary.com/dvkxfgprc/image/upload/c_scale,w_440/v1511429199/giphy_7_usq2vh.gif) ![Alt text](http://res.cloudinary.com/dvkxfgprc/image/upload/c_scale,w_440/v1511430406/giphy_8_ww3jdz.gif) ![Alt text](http://res.cloudinary.com/dvkxfgprc/image/upload/c_scale,w_440/v1511427632/giphy_5_vixwer.gif) ![Alt text](https://res.cloudinary.com/dvkxfgprc/image/upload/c_scale,w_440/v1565943473/Animated_GIF-downsized_large_wirzqk.gif) +#### 效果演示 + -# Importing the Library +#### 安装教程 -Add to build.gradle: -```groovy -dependencies { - compile 'com.chibde:audiovisualizer:2.2.0' +1.在项目根目录下的build.gradle文件中, + ```gradle +allprojects { + repositories { + maven { + url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' + } + } } + ``` +2.在entry模块的build.gradle文件中, + ```gradle + dependencies { + implementation('com.gitee.chinasoft_ohos:ohos-audio-visualizer:0.0.1-SNAPSHOT') + ...... + } + ``` + +在sdk5,DevEco Studio2.1 beta4下项目可直接运行 +如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, +并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下 + +#### 使用说明 + +```示例XML + ``` -Library is available in jcenter repository - -# How to use - - Refer to the [sample](https://github.com/GautamChibde/android-audio-visualizer/tree/master/sample) project on how to use visualizer or refer to [WIKI](https://github.com/GautamChibde/android-audio-visualizer/wiki) docs. - - ## Visualizers -* [LineVisualizer](https://github.com/GautamChibde/android-audio-visualizer/wiki/Line-Visualizer) -* [BarVisualizer](https://github.com/GautamChibde/android-audio-visualizer/wiki/Bar-Visualizer) -* [CircleVisualizer](https://github.com/GautamChibde/android-audio-visualizer/wiki/Circle-Visualizer) -* [Circle Bar Visualizer](https://github.com/GautamChibde/android-audio-visualizer/wiki/Circle-Bar-Visualizer) -* [Line Bar Visualizer](https://github.com/GautamChibde/android-audio-visualizer/wiki/Line-Bar-Visualizer) -* [Square Bar Visualizer](https://github.com/GautamChibde/android-audio-visualizer/wiki/SquareBar-Visualizer) - -License -======= -Copyright 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. + +```java +CircleVisualizer circleVisualizer = (CircleVisualizer) findComponentById(ResourceTable.Id_visualizer); +circleVisualizer.setColor(Color.BLUE.getValue()); +// Customize the size of the circle. by defalut multipliers is 1. +circleVisualizer.setRadiusMultiplier(3f); +// set the line with for the visualizer between 1-10 default 1. +circleVisualizer.setStrokeWidth(1); +circleVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); +``` +#### 测试信息 + +CodeCheck代码测试无异常 + +CloudTest代码测试无异常 + +火绒安全病毒安全检测通过 + +当前版本demo功能与原组件基本无差异 + + +#### 版本迭代 + +- 0.0.1-SNAPSHOT + +#### 版权和许可信息 + + Copyright 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. diff --git a/audiovisualizer/audiovisualizer.iml b/audiovisualizer/audiovisualizer.iml new file mode 100644 index 0000000000000000000000000000000000000000..4d1a1dcbc90c59fcd30dfd3ea3bfab658968a15c --- /dev/null +++ b/audiovisualizer/audiovisualizer.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/audiovisualizer/build.gradle b/audiovisualizer/build.gradle index 34e87939c400db24f7ba7690beec6c28a555d13c..312bc1c336a2b750048a182991e5872344e45dbf 100644 --- a/audiovisualizer/build.gradle +++ b/audiovisualizer/build.gradle @@ -1,54 +1,13 @@ -apply plugin: 'com.android.library' -//apply plugin: 'com.jfrog.bintray' -//apply plugin: 'maven-publish' -// -//ext { -// bintrayRepo = 'Audio-Visualizer' // your repo name -// bintrayName = 'audiovisualizer' // has to be same as your library module name -// publishedGroupId = 'com.chibde' // your module package name -// libraryName = 'audiovisualizer' -// artifact = 'audiovisualizer' // has to be same as your library module name -// libraryDescription = 'Audio visualisation for android MediaPlayer' -// siteUrl = 'https://github.com/GautamChibde/android-audio-visualizer' -// gitUrl = 'https://github.com/GautamChibde/android-audio-visualizer.git' -// libraryVersion = '2.2.0' -// developerId = 'gautamchibde' -// developerName = 'Gautam Chibde' -// developerEmail = 'gautamchibde@gmail.com' -// licenseName = 'The Apache Software License, Version 2.0' -// licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' -// allLicenses = ["Apache-2.0"] -//} - -android { - compileSdkVersion 30 - buildToolsVersion '30.0.3' - +apply plugin: 'com.huawei.ohos.library' +ohos { + compileSdkVersion 5 defaultConfig { - minSdkVersion 14 - targetSdkVersion 30 - versionCode 1 - versionName "1.0" - - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } + compatibleSdkVersion 5 } + } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1', { - exclude group: 'com.android.support', module: 'support-annotations' - }) - implementation 'androidx.appcompat:appcompat:1.0.0' - testImplementation 'junit:junit:4.12' + testCompile 'junit:junit:4.12' } - -//apply from: 'https://raw.githubusercontent.com/numetriclabz/jcenter/master/installv.gradle' -//apply from: 'https://raw.githubusercontent.com/numetriclabz/jcenter/master/bintrayv.gradle' diff --git a/audiovisualizer/proguard-rules.pro b/audiovisualizer/proguard-rules.pro deleted file mode 100644 index c8f0183af73b528949011c6924cd9ea30ea22bee..0000000000000000000000000000000000000000 --- a/audiovisualizer/proguard-rules.pro +++ /dev/null @@ -1,25 +0,0 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /home/gautam/Sdk/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} - -# Uncomment this to preserve the line number information for -# debugging stack traces. -#-keepattributes SourceFile,LineNumberTable - -# If you keep the line number information, uncomment this to -# hide the original source file name. -#-renamesourcefileattribute SourceFile diff --git a/audiovisualizer/src/androidTest/java/com/chibde/ExampleInstrumentedTest.java b/audiovisualizer/src/androidTest/java/com/chibde/ExampleInstrumentedTest.java deleted file mode 100644 index 41beab76c1a2109f2d79b5b8ebccb45c7be278d1..0000000000000000000000000000000000000000 --- a/audiovisualizer/src/androidTest/java/com/chibde/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.chibde; - -import android.content.Context; -import androidx.test.InstrumentationRegistry; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static junit.framework.Assert.assertEquals; - -/** - * Instrumentation test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() throws Exception { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("com.chibde.audiovisulaizer.test", appContext.getPackageName()); - } -} diff --git a/audiovisualizer/src/main/AndroidManifest.xml b/audiovisualizer/src/main/AndroidManifest.xml deleted file mode 100644 index 2227ccdb30bb1edfb1a2dbf68fac98736c14f11f..0000000000000000000000000000000000000000 --- a/audiovisualizer/src/main/AndroidManifest.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - diff --git a/audiovisualizer/src/main/config.json b/audiovisualizer/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..fe967a47daea9851888b383eb692ef561bd79d61 --- /dev/null +++ b/audiovisualizer/src/main/config.json @@ -0,0 +1,27 @@ +{ + "app": { + "bundleName": "com.chibde.visualizer", + "vendor": "chibde", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 5, + "target": 5, + "releaseType": "Release" + } + }, + "deviceConfig": {}, + "module": { + "package": "com.chibde.visualizer", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "audiovisualizer", + "moduleType": "har" + } + } +} \ No newline at end of file diff --git a/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java b/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java index b359aac7db4fc051237bd1df10ad4191ccf0cd6d..05b33507d2284fd66613d344268c44ba9732dbfd 100644 --- a/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java @@ -1,41 +1,41 @@ /* -* 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; -import android.content.Context; -import android.graphics.Color; -import android.graphics.Paint; -import android.media.MediaPlayer; -import android.media.audiofx.Visualizer; -import android.util.AttributeSet; -import android.view.View; -import androidx.annotation.Nullable; +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; /** * Base class that contains common implementation for all * visualizers. * Created by gautam chibde on 28/10/17. */ - -abstract public class BaseVisualizer extends View { +abstract public class BaseVisualizer extends Component { protected byte[] bytes; protected Paint paint; - protected Visualizer visualizer; - protected int color = Color.BLUE; + protected int color; + private AudioWaver audioWaver; public BaseVisualizer(Context context) { super(context); @@ -43,19 +43,20 @@ abstract public class BaseVisualizer extends View { init(); } - public BaseVisualizer(Context context, @Nullable AttributeSet attrs) { + public BaseVisualizer(Context context, AttrSet attrs) { super(context, attrs); init(attrs); init(); } - public BaseVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + + public BaseVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); init(); } - private void init(AttributeSet attributeSet) { + private void init(AttrSet attributeSet) { paint = new Paint(); } @@ -66,53 +67,50 @@ abstract public class BaseVisualizer extends View { */ public void setColor(int color) { this.color = color; - this.paint.setColor(this.color); - } - - /** - * @deprecated will be removed in next version use {@link BaseVisualizer#setPlayer(int)} instead - * @param mediaPlayer MediaPlayer - */ - @Deprecated - public void setPlayer(MediaPlayer mediaPlayer) { - setPlayer(mediaPlayer.getAudioSessionId()); + this.paint.setColor(new Color(color)); } - public void setPlayer(int audioSessionId) { - visualizer = new Visualizer(audioSessionId); - visualizer.setEnabled(false); - visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); + public void setPlayer(int sessionid, String name) { - visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() { + audioWaver = new AudioWaver(sessionid, name); + audioWaver.setActivated(false); + audioWaver.setDataSize(AudioWaver.getMaxDataSize()); + audioWaver.setWaveDataObserver(new AudioWaver.WaveDataObserver() { @Override - public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, - int samplingRate) { + public void onWaveData(byte[] bytes, int i) { + BaseVisualizer.this.bytes = bytes; - invalidate(); + //刷新 + getContext().getUITaskDispatcher().asyncDispatch(new Runnable() { + @Override + public void run() { + invalidate(); + } + }); } + }, AudioWaver.getMinInterval()); + audioWaver.setActivated(true); - @Override - public void onFftDataCapture(Visualizer visualizer, byte[] bytes, - int samplingRate) { - } - }, Visualizer.getMaxCaptureRate() / 2, true, false); - visualizer.setEnabled(true); } + @Override public void release() { + super.release(); //will be null if setPlayer hasn't yet been called - if (visualizer == null) + if (audioWaver == null) return; - - visualizer.release(); + audioWaver.release(); bytes = null; invalidate(); } - public Visualizer getVisualizer() { - return visualizer; + + 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 df6a4a0cb1f6daaf9b767b260449bbb1b5fb0df4..a5e605aa153236e9ff3dac32ef4b1a715c488a30 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/BarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/BarVisualizer.java @@ -15,46 +15,41 @@ */ package com.chibde.visualizer; -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import androidx.annotation.Nullable; -import android.util.AttributeSet; import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.render.Paint; +import ohos.agp.utils.Point; +import ohos.app.Context; /** - * Custom view that creates a Bar visualizer effect for - * the android {@link android.media.MediaPlayer} + * Custom component that creates a Bar visualizer effect for * * Created by gautam chibde on 28/10/17. */ -public class BarVisualizer extends BaseVisualizer { - +public class BarVisualizer extends BaseVisualizer implements Component.DrawTask { private float density = 50; private int gap; public BarVisualizer(Context context) { super(context); + addDrawTask(this); } - public BarVisualizer(Context context, - @Nullable AttributeSet attrs) { + public BarVisualizer(Context context, AttrSet attrs) { super(context, attrs); + addDrawTask(this); } - public BarVisualizer(Context context, - @Nullable AttributeSet attrs, - int defStyleAttr) { - super(context, attrs, defStyleAttr); - } @Override protected void init() { this.density = 50; this.gap = 4; - paint.setStyle(Paint.Style.FILL); + paint.setStyle(Paint.Style.FILL_STYLE); } /** @@ -72,22 +67,24 @@ public class BarVisualizer extends BaseVisualizer { this.density = 10; } } - @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { if (bytes != null) { float barWidth = getWidth() / density; float div = bytes.length / density; 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; 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); } - super.onDraw(canvas); + //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 ef94722e6c485e433780d5f01fc8872d8ebd161c..3355e67d5286b4484cf8edc4757dcce923b8a72c 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/BlazingColorVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/BlazingColorVisualizer.java @@ -1,75 +1,79 @@ /* -* 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 android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.LinearGradient; -import android.graphics.Shader; -import androidx.annotation.Nullable; -import android.util.AttributeSet; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.render.LinearShader; +import ohos.agp.render.Paint; +import ohos.agp.render.Shader; +import ohos.agp.utils.Color; +import ohos.agp.utils.Point; +import ohos.app.Context; /** * TODO - * - * Created by gautam chibde on 29/10/17. + * Created by gautam chibd e on 29/10/17. */ -class BlazingColorVisualizer extends BaseVisualizer { +class BlazingColorVisualizer extends BaseVisualizer implements Component.DrawTask { private Shader shader; + // private ShapeElement shapeElement; public BlazingColorVisualizer(Context context) { super(context); + addDrawTask(this); } public BlazingColorVisualizer(Context context, - @Nullable AttributeSet attrs) { + AttrSet attrs) { super(context, attrs); + addDrawTask(this); } public BlazingColorVisualizer(Context context, - @Nullable AttributeSet attrs, + AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + addDrawTask(this); } @Override protected void init() { - shader = new LinearGradient(0, - 0, - 0, - getHeight(), - Color.BLUE, - Color.GREEN, - Shader.TileMode.MIRROR /*or REPEAT*/); + Point[] point = new Point[]{new Point(0, 0)}; + float[] point1 = {0, getHeight()}; + + Color[] colors = {Color.BLUE, Color.GREEN}; + shader = new LinearShader(point, point1, colors, Shader.TileMode.MIRROR_TILEMODE); } + @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { if (bytes != null) { - paint.setShader(shader); + 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; - canvas.drawLine(i, getHeight(), i, top, paint); + Point points = new Point(i, getHeight()); + Point pointss = new Point(i, top); + canvas.drawLine(points, pointss, paint); } - super.onDraw(canvas); } } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java index c10b93d465b51fcd1e59c5518a25dfd5978d2c1e..50db97a9e354d21049360084613e9d9c5a6e936c 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizer.java @@ -15,61 +15,62 @@ */ package com.chibde.visualizer; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import androidx.annotation.Nullable; -import android.util.AttributeSet; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.render.Paint; +import ohos.agp.utils.Color; +import ohos.app.Context; /** - * Custom view that creates a Circle and Bar visualizer effect for - * the android {@link android.media.MediaPlayer} + * Custom component that creates a Circle and Bar visualizer effect for * * Created by gautam chibde on 20/11/17. */ -public class CircleBarVisualizer extends BaseVisualizer { +public class CircleBarVisualizer extends BaseVisualizer implements Component.DrawTask { private float[] points; private Paint circlePaint; private int radius; public CircleBarVisualizer(Context context) { super(context); + addDrawTask(this::onDraw); } public CircleBarVisualizer(Context context, - @Nullable AttributeSet attrs) { + AttrSet attrs) { super(context, attrs); + addDrawTask(this::onDraw); } public CircleBarVisualizer(Context context, - @Nullable AttributeSet attrs, + AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + addDrawTask(this::onDraw); } @Override protected void init() { - paint.setStyle(Paint.Style.STROKE); + paint.setStyle(Paint.Style.STROKE_STYLE); circlePaint = new Paint(); radius = -1; } @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { if (radius == -1) { radius = getHeight() < getWidth() ? getHeight() : getWidth(); radius = (int) (radius * 0.65 / 2); double circumference = 2 * Math.PI * radius; paint.setStrokeWidth((float) (circumference / 120)); - circlePaint.setStyle(Paint.Style.STROKE); + circlePaint.setStyle(Paint.Style.STROKE_STYLE); circlePaint.setStrokeWidth(4); } - circlePaint.setColor(color); - canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, circlePaint); + circlePaint.setColor(new Color(color)); + canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, circlePaint); if (bytes != null) { if (points == null || points.length < bytes.length * 4) { points = new float[bytes.length * 4]; @@ -99,6 +100,6 @@ public class CircleBarVisualizer extends BaseVisualizer { canvas.drawLines(points, paint); } - super.onDraw(canvas); + } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java index cbb112751c02dea7416a8b576ea229e97ee1bbb1..b261cee6a087dcb6b67c062fc76543b0dee124d0 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleBarVisualizerSmooth.java @@ -15,30 +15,23 @@ */ package com.chibde.visualizer; - - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; - - -import android.util.AttributeSet; - -import androidx.annotation.Nullable; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.render.Paint; +import ohos.app.Context; import java.util.HashMap; import java.util.Map; /** - * Custom view that creates a Circle and Bar visualizer effect for the android - * {@link android.media.MediaPlayer} + * Custom component that creates a Circle and Bar visualizer effect for the ohos *

* Created by gautam chibde on 20/11/17. Smooth effect added by Ali heidari */ -public class CircleBarVisualizerSmooth extends BaseVisualizer { +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; @@ -48,15 +41,15 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer { // Stores radius and step-counter which every invoking of "onDraw" requires them private Map configs = null; - public CircleBarVisualizerSmooth(Context context, - @Nullable AttributeSet attrs) { + AttrSet attrs) { super(context, attrs); + addDrawTask(this); } @Override protected void init() { - paint.setStyle(Paint.Style.STROKE); + paint.setStyle(Paint.Style.STROKE_STYLE); } /* @@ -173,7 +166,7 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer { // 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 % 4f) / _BarCount); + int t = getBarLength(i, (bytes.length - bytes.length % 4) / _BarCount); // Find the round by int round = (int) (getConfig("stepCounter") % _StepsCount); if (round == 0) { @@ -210,7 +203,7 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer { points[indexM4 + 1] = (float) (this.getHeight() / 2 + getConfig("radius") * Math.sin(angle)); } // Calculates points for current round - calcRound(i, angle); + } /* @@ -222,8 +215,21 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer { * 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) + setConfig("needsInit", 0); + } + @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { // Check if bytes initiated before if (bytes == null) return; @@ -243,26 +249,13 @@ public class CircleBarVisualizerSmooth extends BaseVisualizer { double radianAngle = Math.toRadians(angle); this.fillStartingPoints(i, radianAngle); - - } if (getConfig("needsInit") == 0) canvas.drawLines(points, paint); - super.onDraw(canvas); + //super.onDraw(canvas); // Resets configurations variable for next calling of onDraw this.resetConfigs(); } - - /* - * Reset configs - */ - private void resetConfigs() { - // The stepCounter increases - setConfig("stepCounter", getConfig("stepCounter") + 1); - // Initialized, no longer need initializing - if (getConfig("needsInit") == 1) - setConfig("needsInit", 0); - } } \ No newline at end of file diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java index d65044062b66173669d236304268027fdbe939d1..46a534cd6b4d8bff6c5b0f50525afe6bb20a7a95 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/CircleVisualizer.java @@ -1,48 +1,49 @@ /* -* 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 android.content.Context; -import android.graphics.Canvas; -import androidx.annotation.Nullable; -import android.util.AttributeSet; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.app.Context; /** - * Custom view that creates a circle visualizer effect for - * the android {@link android.media.MediaPlayer} - * + * Custom component that creates a circle visualizer effect for + *

* Created by gautam on 13/11/17. */ -public class CircleVisualizer extends BaseVisualizer { +public class CircleVisualizer extends BaseVisualizer implements Component.DrawTask { private float[] points; private float radiusMultiplier = 1; private float strokeWidth = 0.005f; public CircleVisualizer(Context context) { super(context); + addDrawTask(this); } - public CircleVisualizer(Context context, @Nullable AttributeSet attrs) { + public CircleVisualizer(Context context, AttrSet attrs) { super(context, attrs); + addDrawTask(this); } - public CircleVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + public CircleVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + addDrawTask(this); } @Override @@ -75,7 +76,7 @@ public class CircleVisualizer extends BaseVisualizer { } @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { if (bytes != null) { paint.setStrokeWidth(getHeight() * strokeWidth); if (points == null || points.length < bytes.length * 4) { @@ -105,6 +106,5 @@ public class CircleVisualizer extends BaseVisualizer { } canvas.drawLines(points, paint); } - super.onDraw(canvas); } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java index 3b9feaeb167a5577decf8557e6727288eaf6fdd0..9d481de7cf48b91fd11596f904d18ff1b3d7c68f 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/LineBarVisualizer.java @@ -14,38 +14,39 @@ * limitations under the License. */ package com.chibde.visualizer; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import androidx.annotation.Nullable; -import android.util.AttributeSet; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.render.Paint; +import ohos.agp.utils.Color; +import ohos.agp.utils.Point; +import ohos.app.Context; /** - * Custom view that creates a Line and Bar visualizer effect for - * the android {@link android.media.MediaPlayer} + * Custom component that creates a Line and Bar visualizer effect for *

* Created by gautam chibde on 22/11/17. */ -public class LineBarVisualizer extends BaseVisualizer { +public class LineBarVisualizer extends BaseVisualizer implements Component.DrawTask { private Paint middleLine; private float density; private int gap; public LineBarVisualizer(Context context) { super(context); + addDrawTask(this::onDraw); } - public LineBarVisualizer(Context context, @Nullable AttributeSet attrs) { + public LineBarVisualizer(Context context, AttrSet attrs) { super(context, attrs); + addDrawTask(this::onDraw); } - public LineBarVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + public LineBarVisualizer(Context context, AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + addDrawTask(this::onDraw); } @Override @@ -79,15 +80,18 @@ public class LineBarVisualizer extends BaseVisualizer { } } + @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { if (middleLine.getColor() != Color.BLUE) { - middleLine.setColor(color); + middleLine.setColor(new Color()); } if (bytes != null) { float barWidth = getWidth() / density; float div = bytes.length / density; - canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, 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++) { @@ -101,10 +105,14 @@ public class LineBarVisualizer extends BaseVisualizer { * (getHeight() / 2) / 128; float barX = (i * barWidth) + (barWidth / 2); - canvas.drawLine(barX, bottom, barX, getHeight() / 2, paint); - canvas.drawLine(barX, top, barX, getHeight() / 2, 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); + // 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 e037c4a3dda4de86fe706f8503ea5f8382c994c0..476f0b6d96ae67a850877bd7b6e9e837d2a3150b 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/LineVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/LineVisualizer.java @@ -14,40 +14,40 @@ * limitations under the License. */ package com.chibde.visualizer; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Rect; -import androidx.annotation.Nullable; -import android.util.AttributeSet; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.utils.Rect; +import ohos.app.Context; /** - * Custom view that creates a Bar visualizer effect for - * the android {@link android.media.MediaPlayer} + * Custom component that creates a Bar visualizer effect fo * * Created by gautam chibde on 28/10/17. */ -public class LineVisualizer extends BaseVisualizer { +public class LineVisualizer extends BaseVisualizer implements Component.DrawTask { private float[] points; private Rect rect = new Rect(); private float strokeWidth = 0.005f; public LineVisualizer(Context context) { super(context); + addDrawTask(this::onDraw); } public LineVisualizer(Context context, - @Nullable AttributeSet attrs) { + AttrSet attrs) { super(context, attrs); + addDrawTask(this::onDraw); } public LineVisualizer(Context context, - @Nullable AttributeSet attrs, + AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + addDrawTask(this::onDraw); } @Override @@ -69,7 +69,7 @@ public class LineVisualizer extends BaseVisualizer { } @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { if (bytes != null) { if (points == null || points.length < bytes.length * 4) { points = new float[bytes.length * 4]; @@ -78,15 +78,14 @@ public class LineVisualizer extends BaseVisualizer { rect.set(0, 0, getWidth(), getHeight()); for (int i = 0; i < bytes.length - 1; i++) { - points[i * 4] = rect.width() * i / (bytes.length - 1); - points[i * 4 + 1] = rect.height() / 2 - + ((byte) (bytes[i] + 128)) * (rect.height() / 3) / 128; - points[i * 4 + 2] = rect.width() * (i + 1) / (bytes.length - 1); - points[i * 4 + 3] = rect.height() / 2 - + ((byte) (bytes[i + 1] + 128)) * (rect.height() / 3) / 128; + points[i * 4] = rect.getWidth() * i / (bytes.length - 1); + points[i * 4 + 1] = rect.getHeight() / 2 + + ((byte) (bytes[i] + 128)) * (rect.getHeight() / 3) / 128; + points[i * 4 + 2] = rect.getWidth() * (i + 1) / (bytes.length - 1); + points[i * 4 + 3] = rect.getHeight() / 2 + + ((byte) (bytes[i + 1] + 128)) * (rect.getHeight() / 3) / 128; } canvas.drawLines(points, paint); } - super.onDraw(canvas); } } diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/MyApplication.java b/audiovisualizer/src/main/java/com/chibde/visualizer/MyApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..14ce34c22bf9e670a5993a06da99c0627378b3e0 --- /dev/null +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/MyApplication.java @@ -0,0 +1,10 @@ +package com.chibde.visualizer; + +import ohos.aafwk.ability.AbilityPackage; + +public class MyApplication extends AbilityPackage { + @Override + public void onInitialize() { + super.onInitialize(); + } +} diff --git a/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java b/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java index 8409163461d42285dc43b0769ce1492e51a9a3ce..1bc0d91bba5d5dc3f0625c107d73ede2496aef61 100644 --- a/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java +++ b/audiovisualizer/src/main/java/com/chibde/visualizer/SquareBarVisualizer.java @@ -15,47 +15,48 @@ */ package com.chibde.visualizer; -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.util.AttributeSet; - -import androidx.annotation.Nullable; - import com.chibde.BaseVisualizer; +import ohos.agp.components.AttrSet; +import ohos.agp.components.Component; +import ohos.agp.render.Canvas; +import ohos.agp.render.Paint; +import ohos.agp.utils.Point; +import ohos.app.Context; /** - * Custom view that creates a Bar visualizer effect for - * the android {@link android.media.MediaPlayer} + * Custom component that creates a Bar visualizer effect for *

* Created by gautam chibde on 28/10/17. */ -public class SquareBarVisualizer extends BaseVisualizer { +public class SquareBarVisualizer extends BaseVisualizer implements Component.DrawTask { private float density = 16; private int gap; public SquareBarVisualizer(Context context) { super(context); + addDrawTask(this); } public SquareBarVisualizer(Context context, - @Nullable AttributeSet attrs) { + AttrSet attrs) { super(context, attrs); + addDrawTask(this); } public SquareBarVisualizer(Context context, - @Nullable AttributeSet attrs, + AttrSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + addDrawTask(this); } @Override protected void init() { this.density = 16; this.gap = 2; - paint.setStyle(Paint.Style.FILL); + paint.setStyle(Paint.Style.FILL_STYLE); } /** @@ -79,12 +80,14 @@ public class SquareBarVisualizer extends BaseVisualizer { * * @param gap Spacing between the square */ + public void setGap(int gap) { this.gap = gap; } @Override - protected void onDraw(Canvas canvas) { + public void onDraw(Component component, Canvas canvas) { + if (bytes != null) { float barWidth = getWidth() / density; float div = bytes.length / density; @@ -98,11 +101,12 @@ public class SquareBarVisualizer extends BaseVisualizer { float barX = (i * barWidth) + (barWidth / 2); float y1 = getHeight() - ((barWidth + (gap / 2f)) * count); float y2 = getHeight() - ((barWidth - gap / 2f) + ((barWidth + gap / 2f) * count)); - canvas.drawLine(barX, y1, barX, y2, paint); + Point points = new Point(barX, y1); + Point pointss = new Point(barX, y2); + canvas.drawLine(points, pointss, paint); count++; } } - super.onDraw(canvas); } } } diff --git a/audiovisualizer/src/main/res/values/strings.xml b/audiovisualizer/src/main/res/values/strings.xml deleted file mode 100644 index 2e82bfc3a09644954eedf6187f888651b7f1f04b..0000000000000000000000000000000000000000 --- a/audiovisualizer/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - AudioVisulaizer - diff --git a/audiovisualizer/src/main/resources/base/element/string.json b/audiovisualizer/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f010f38fe06eab7e4247e694bea737d9262936fa --- /dev/null +++ b/audiovisualizer/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "visualizers" + } + ] +} diff --git a/sample/src/main/res/raw/red_e.mp3 b/audiovisualizer/src/main/resources/base/profile/red_e.mp3 similarity index 100% rename from sample/src/main/res/raw/red_e.mp3 rename to audiovisualizer/src/main/resources/base/profile/red_e.mp3 diff --git a/audiovisualizer/src/test/java/com/chibde/ExampleUnitTest.java b/audiovisualizer/src/test/java/com/chibde/ExampleUnitTest.java deleted file mode 100644 index d357c0154e4f433e604edea8cf2ffa5c838fabbc..0000000000000000000000000000000000000000 --- a/audiovisualizer/src/test/java/com/chibde/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.chibde; - -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void additionIsCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 5a19def76958ff4631c21b33ad81be6291d4d4ef..e57a5c8fd6e05875ba202640446ba6fed958d597 100644 --- a/build.gradle +++ b/build.gradle @@ -1,32 +1,41 @@ +import java.lang.annotation.Target + +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 5 + defaultConfig { + + compatibleSdkVersion 5 + } +} + buildscript { repositories { - mavenCentral() - mavenLocal() maven { - url "https://maven.google.com" + url 'https://mirrors.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' } - google() jcenter() - } dependencies { - classpath 'com.android.tools.build:gradle:4.2.1' - classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' + classpath 'com.huawei.ohos:hap:2.4.2.7' + classpath 'com.huawei.ohos:decctest:1.0.0.6' + } } allprojects { repositories { - mavenCentral() - mavenLocal() maven { - url "https://maven.google.com" + url 'https://mirrors.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' } jcenter() } } - -task clean(type: Delete) { - delete rootProject.buildDir -} diff --git a/sample/.gitignore b/entry/.gitignore similarity index 100% rename from sample/.gitignore rename to entry/.gitignore diff --git a/entry/build.gradle b/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..d2a7fab214c7b263d1f46f68cf0df9bcbda5e02f --- /dev/null +++ b/entry/build.gradle @@ -0,0 +1,16 @@ +apply plugin: 'com.huawei.ohos.hap' +ohos { + + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 5 + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testCompile 'junit:junit:4.12' + compile project(path: ':audiovisualizer') + + +} diff --git a/entry/entry.iml b/entry/entry.iml new file mode 100644 index 0000000000000000000000000000000000000000..8d6c3d3eb9575facd61e191c638a55a751a4bf2f --- /dev/null +++ b/entry/entry.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/entry/src/main/config.json b/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..6f700822772645d28e669f52cb9d1fe7f52da756 --- /dev/null +++ b/entry/src/main/config.json @@ -0,0 +1,181 @@ +{ + "app": { + "bundleName": "com.chibde.visualizer", + "vendor": "chibde", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 5, + "target": 5, + "releaseType": "Release" + } + }, + "deviceConfig": {}, + "module": { + "reqPermissions": [ + { + "name": "ohos.permission.MICROPHONE" + } + ], + "package": "com.chibde.visualizer", + "name": "com.chibde.audiovisualizer.MyApplication", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "AudioVisualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.visualizer.LineVisualizerAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.visualizer.BarVisualizerAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.visualizer.CircleBarVisualizerAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.visualizer.LineBarVisualizerAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.visualizer.SquareBarVisualizerAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.visualizer.CircleVisualizerAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + }, + { + "orientation": "unspecified", + "name": "com.chibde.audiovisualizer.sample.ServiceExampleAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "visualizer", + "type": "page", + "launchType": "standard", + "metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar" + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/entry/src/main/java/com/chibde/audiovisualizer/MainAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..ca063d0d9611ed014776c0d990a0d4c9f5c77ae5 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/MainAbility.java @@ -0,0 +1,13 @@ +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/MyApplication.java b/entry/src/main/java/com/chibde/audiovisualizer/MyApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..63ca63d2df313c5b48503038604f3e4f1919661d --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/MyApplication.java @@ -0,0 +1,10 @@ +package com.chibde.audiovisualizer; + +import ohos.aafwk.ability.AbilityPackage; + +public class MyApplication extends AbilityPackage { + @Override + public void onInitialize() { + super.onInitialize(); + } +} diff --git a/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java b/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..526834ae97a5cf9a0b789099cbdd3425b632980a --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/BaseAbility.java @@ -0,0 +1,128 @@ +/* + * 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.audiovisualizer.sample; + +import com.chibde.audiovisualizer.util.PlaySoundUtil; +import com.chibde.visualizer.ResourceTable; +import ohos.aafwk.ability.Ability; +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Image; +import ohos.agp.utils.Color; +import ohos.agp.window.dialog.ToastDialog; +import ohos.agp.window.service.WindowManager; +import ohos.bundle.IBundleManager; + +import java.io.IOException; + +/** + * BaseAbility that contains common code for all visualizers + *

+ * Created by gautam chibde on 18/11/17. + */ +abstract public 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(); + + } + + private void getPermissions() { + if (verifySelfPermission("ohos.permission.MICROPHONE") != IBundleManager.PERMISSION_GRANTED) { + // 应用未被授予权限 + if (canRequestPermission("ohos.permission.MICROPHONE")) { + // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示) + requestPermissionsFromUser( + new String[]{"ohos.permission.MICROPHONE"}, 23); + + } else { + // 显示应用需要权限的理由,提示用户进入设置授权 + + } + + } else { + // 权限已被授予 + //初始化的方法 + initialize(); + } + } + + private void initialize() { + setPlayer(); + } + + //播放音乐的方法 + private void setPlayer() { + try { + playSoundUtil = new PlaySoundUtil(); + + } catch (IOException e) { + e.printStackTrace(); + + } + + init(); + } + + @Override + public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults); + if (requestCode == 23) { + + initialize(); + } else { + terminateAbility(); + + } + } + + @Override + protected void onStop() { + super.onStop(); + if (playSoundUtil != null) { + if (playSoundUtil.getAudioRenderer() != null) { + if (playSoundUtil.getAudioRenderer().getState() != null) { + playSoundUtil.getAudioRenderer().stop();//暂停 + playSoundUtil.getAudioRenderer().release();//释放 + } + + } + } + + + } + + + protected int getLayout() { + return 0; + } + + 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 new file mode 100644 index 0000000000000000000000000000000000000000..11325f25f710232cac22a7a7e93696107ed6e3ff --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/MainAbility.java @@ -0,0 +1,178 @@ +/* + * 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.audiovisualizer.sample; + + +import com.chibde.audiovisualizer.sample.visualizer.BarVisualizerAbility; +import com.chibde.audiovisualizer.sample.visualizer.CircleBarVisualizerAbility; +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 ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.aafwk.content.Operation; +import ohos.agp.components.Button; +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); + super.setUIContent(ResourceTable.Layout_ability_main); + WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#303F9F")); + Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); + Component back = findComponentById(ResourceTable.Id_im_back); + title_name.setText("AudioVisualizer"); + back.setVisibility(Component.HIDE); + + //获取资源ID + Button line = (Button) findComponentById(ResourceTable.Id_line); + line.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + line(component); + } + }); + Button bar = (Button) findComponentById(ResourceTable.Id_bar); + bar.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + bar(component); + } + }); + Button circle = (Button) findComponentById(ResourceTable.Id_circle); + circle.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + circle(component); + } + }); + Button circleBar = (Button) findComponentById(ResourceTable.Id_circleBar); + circleBar.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + circleBar(component); + } + }); + Button lineBar = (Button) findComponentById(ResourceTable.Id_lineBar); + lineBar.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + lineBar(component); + } + }); + Button service = (Button) findComponentById(ResourceTable.Id_service); + service.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + service(component); + } + }); + Button square = (Button) findComponentById(ResourceTable.Id_square); + square.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + square(component); + } + }); + + } + + public void line(Component component) { +// startAbility(LineVisualizerAbility.class); + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(LineVisualizerAbility.class.getName()) + .build(); + intent.setOperation(build); + startAbility(intent); + } + + public void bar(Component component) { + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(BarVisualizerAbility.class.getName()) + .build(); + intent.setOperation(build); + startAbility(intent); + } + + public void circle(Component component) { + + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(CircleVisualizerAbility.class.getName()) + .build(); + intent.setOperation(build); + startAbility(intent); + } + + public void circleBar(Component component) { + // startAbility(CircleBarVisualizerAbility.class); + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(CircleBarVisualizerAbility.class.getName()) + .build(); + intent.setOperation(build); + startAbility(intent); + } + + public void lineBar(Component component) { + + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(LineBarVisualizerAbility.class.getName()) + .build(); + intent.setOperation(build); + startAbility(intent); + } + + public void service(Component component) { + //startAbility(ServiceExampleAbility.class); + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(ServiceExampleAbility.class.getName()) + .build(); + intent.setOperation(build); + startAbility(intent); + } + + public void square(Component component) { + // startAbility(SquareBarVisualizerAbility.class); + Intent intent = new Intent(); + Operation build = new Intent.OperationBuilder() + .withBundleName(getBundleName()) + .withAbilityName(SquareBarVisualizerAbility.class.getName()) + .build(); + 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 new file mode 100644 index 0000000000000000000000000000000000000000..3aafb1a90c98ff63d5073aece850a8270eb72901 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/MediaPlayerService.java @@ -0,0 +1,110 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..91b38f03e5ea08b774643d50b87d7da598ab367e --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/ServiceExampleAbility.java @@ -0,0 +1,117 @@ +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; +import ohos.agp.utils.Color; +import ohos.media.audio.AudioRenderer; + +public class ServiceExampleAbility extends BaseAbility { + private BarVisualizer barVisualizer; + private boolean isfirst = true; + private long currentClick = 0; + private Image im_pause; + private Image im_start; + private Image im_reset; + + @Override + public void init() { + //获取资源ID + barVisualizer = (BarVisualizer) findComponentById(ResourceTable.Id_visualizer); + if (barVisualizer != null) { + barVisualizer.setColor(Color.BLUE.getValue()); + barVisualizer.setDensity(70); + barVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); + } + Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); + title_name.setText("AudiuoVisualizer"); + im_start = (Image) findComponentById(ResourceTable.Id_im_start); + im_reset = (Image) findComponentById(ResourceTable.Id_im_reset); + im_pause = (Image) findComponentById(ResourceTable.Id_im_pause); + Component back = findComponentById(ResourceTable.Id_im_back); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + im_start.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + if (currentClick != 0) { + if (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() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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 (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 + protected int getLayout() { + return ResourceTable.Layout_ability_service_example; + } +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..ceef5437414c98dd4eb65b4526d0c86cac1dd873 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/BarVisualizerAbility.java @@ -0,0 +1,139 @@ +/* + * 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.audiovisualizer.sample.visualizer; + +import com.chibde.audiovisualizer.sample.BaseAbility; +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; +import ohos.agp.utils.Color; +import ohos.media.audio.AudioRenderer; + +public class BarVisualizerAbility extends BaseAbility { + private boolean isfirst = true; + private long currentClick = 0; + 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) { + 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. + + 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); + Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); + title_name.setText("Bar Visualizer"); + Component back = findComponentById(ResourceTable.Id_im_back); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + im_pause.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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; + } + } + 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 + 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 new file mode 100644 index 0000000000000000000000000000000000000000..7d88b16ff0c50526f1fff61de3ce02835c63da32 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleBarVisualizerAbility.java @@ -0,0 +1,136 @@ +/* + * 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.audiovisualizer.sample.visualizer; + + +import com.chibde.audiovisualizer.sample.BaseAbility; +import com.chibde.visualizer.CircleBarVisualizer; +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 CircleBarVisualizerAbility extends BaseAbility { + private boolean isfirst = true; + private long currentClick = 0; + 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); + Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); + title_name.setText("Circle Bar Visualizer"); + Component back = findComponentById(ResourceTable.Id_im_back); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + im_start.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + if (currentClick != 0) { + if (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() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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 (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 + 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 new file mode 100644 index 0000000000000000000000000000000000000000..dbe9b43c2218b2dc14d2729306c25c17854c04dd --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/CircleVisualizerAbility.java @@ -0,0 +1,136 @@ +/* + * 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.audiovisualizer.sample.visualizer; + + +import com.chibde.audiovisualizer.sample.BaseAbility; +import com.chibde.visualizer.CircleVisualizer; +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 CircleVisualizerAbility extends BaseAbility { + private boolean isfirst = true; + private long currentClick = 0; + private Image im_pause; + + @Override + protected int getLayout() { + return ResourceTable.Layout_ability_circle_visualizer; + } + + @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); + im_pause = (Image) findComponentById(ResourceTable.Id_im_pause); + title_name.setText("Circle Visualizer"); + Component back = findComponentById(ResourceTable.Id_im_back); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + im_start.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + if (currentClick != 0) { + if (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() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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 (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(); + } + + } + }); + } +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..c871d8d6b4702f7d6b174481aa449b1c1a686ba9 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineBarVisualizerAbility.java @@ -0,0 +1,139 @@ +/* + * 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.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 Image im_start; + private Image im_reset; + private Image im_pause; + + @Override + protected int getLayout() { + return ResourceTable.Layout_ability_line_bar_visualizer; + } + + @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); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + im_start = (Image) findComponentById(ResourceTable.Id_im_start); + im_reset = (Image) findComponentById(ResourceTable.Id_im_reset); + im_pause = (Image) findComponentById(ResourceTable.Id_im_pause); + im_pause.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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; + } + } + 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(); + } + + } + }); + } +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..1b6c5aa8a1e4b85fae7dd3450c10c5c63785c736 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/LineVisualizerAbility.java @@ -0,0 +1,153 @@ +/* + * 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.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; +import ohos.agp.utils.Color; +import ohos.media.audio.AudioRenderer; + +public class LineVisualizerAbility extends BaseAbility { + + private boolean isfirst = true; + private long currentClick = 0; + private Image im_start; + private Image im_pause; + private Image im_reset; + + @Override + protected int getLayout() { + return ResourceTable.Layout_ability_line_visualizer; + } + + @Override + public void init() { + LineVisualizer lineVisualizer = (LineVisualizer) findComponentById(ResourceTable.Id_visualizer); + if (lineVisualizer != null) { + lineVisualizer.setColor(Color.BLUE.getValue()); + lineVisualizer.setStrokeWidth(1); + lineVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName()); + } + im_reset = (Image) findComponentById(ResourceTable.Id_im_reset); + im_pause = (Image) findComponentById(ResourceTable.Id_im_pause); + im_start = (Image) findComponentById(ResourceTable.Id_im_start); + Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name); + title_name.setText("Line Visualizer"); + Component back = findComponentById(ResourceTable.Id_im_back); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + im_pause.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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; + } + } + 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(); + } + } + }); + // 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 new file mode 100644 index 0000000000000000000000000000000000000000..eee4c1380fe75b8aa72472f597080fc71df7211e --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/sample/visualizer/SquareBarVisualizerAbility.java @@ -0,0 +1,133 @@ +/* + * 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.audiovisualizer.sample.visualizer; + + +import com.chibde.audiovisualizer.sample.BaseAbility; +import com.chibde.visualizer.ResourceTable; +import com.chibde.visualizer.SquareBarVisualizer; +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 SquareBarVisualizerAbility extends BaseAbility { + private boolean isfirst = true; + private long currentClick = 0; + private Image im_pause; + + //初始化 + @Override + public void init() { + SquareBarVisualizer squareBarVisualizer = (SquareBarVisualizer) findComponentById(ResourceTable.Id_visualizer); + if (squareBarVisualizer != null) { + squareBarVisualizer.setColor(Color.BLUE.getValue()); + squareBarVisualizer.setDensity(65); + 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"); + Component back = findComponentById(ResourceTable.Id_im_back); + back.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + terminateAbility(); + } + }); + 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); + im_start.setClickedListener(new Component.ClickedListener() { + @Override + public void onClick(Component component) { + if (currentClick != 0) { + if (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() { + @Override + public void onClick(Component component) { + new Thread(new Runnable() { + @Override + public void run() { + playSoundUtil.getAudioRenderer().pause(); + } + }).start(); + 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 (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 + 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 new file mode 100644 index 0000000000000000000000000000000000000000..527791e76b33c478ba6ba7dae464331be176ab99 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/slice/MainAbilitySlice.java @@ -0,0 +1,24 @@ +package com.chibde.audiovisualizer.slice; + +import com.chibde.visualizer.ResourceTable; +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; + +public class MainAbilitySlice extends AbilitySlice { + @Override + 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 new file mode 100644 index 0000000000000000000000000000000000000000..bcb75f1d484714bffbba03130a2177dfefa695d1 --- /dev/null +++ b/entry/src/main/java/com/chibde/audiovisualizer/util/PlaySoundUtil.java @@ -0,0 +1,63 @@ +package com.chibde.audiovisualizer.util; + +import ohos.media.audio.*; + +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; + + 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) + .channelMask(AudioStreamInfo.ChannelMask.CHANNEL_OUT_STEREO) + .streamUsage(AudioStreamInfo.StreamUsage.STREAM_USAGE_MEDIA) + .build(); + audioRendererInfo = new AudioRendererInfo.Builder().audioStreamInfo(audioStreamInfo) + .audioStreamOutputFlag(AudioRendererInfo.AudioStreamOutputFlag.AUDIO_STREAM_OUTPUT_FLAG_DIRECT_PCM) // pcm格式的输出流 + .bufferSizeInBytes(1024) + .isOffload(true) // false表示分段传输buffer并播放,true表示整个音频流一次性传输到HAL层播放 + .build(); + audioRenderer = new AudioRenderer(audioRendererInfo, playMode); + + + } + + 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(); + } + + } + + public AudioRenderer getAudioRenderer() { + return this.audioRenderer; + } +} diff --git a/entry/src/main/resources/base/element/color.json b/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..8d76d910785868c6e66bef29d1e37f8fdfec0428 --- /dev/null +++ b/entry/src/main/resources/base/element/color.json @@ -0,0 +1,57 @@ +{ + "color": [ + { + "name": "purple_progress", + "value": "#ffaa66cc" + }, + { + "name": "green_complete", + "value": "#ff99cc00" + }, + { + "name": "red_error", + "value": "#ffff4444" + }, + { + "name": "blue_pressed", + "value": "#ff0099cc" + }, + + { + "name": "holo_blue_bright", + "value": "#FF00DDFF" + }, + { + "name": "holo_green_light", + "value": "#ff99cc00" + }, + { + "name": "holo_orange_light", + "value": "#ffffbb33" + }, + { + "name": "holo_red_light", + "value": "#ffff4444" + }, + { + "name": "blue_normal", + "value": "#ff33b5e5" + }, + { + "name": "white_normal", + "value": "#FFFFFFFF" + },{ + "name":"colorPrimary", + "value": "#FF1838E9" + },{ + "name": "colorPrimaryDark", + "value": "#303F9F" + },{ + "name": "colorAccent", + "value": "#FF4081" + },{ + "name": "custom", + "value": "#3F51B5" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..79706002e6cdd2fcfbc47c18f0e1968c3478bf8a --- /dev/null +++ b/entry/src/main/resources/base/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "app_name", + "value": "AudioVisualizer" + }, + { + "name": "mainability_description", + "value": "Java_Phone_Empty Feature Ability" + }, + { + "name": "image_play_pause", + "value": "Play Pause Button" + }, + { + "name": "image_replay_button", + "value": "Replay Button" + }, + { + "name": "line_visualizer", + "value": "LINE VISUALIZER" + }, + { + "name": "bar_visualizer", + "value": "BAR VISUALIZER" + }, + { + "name": "circle_visualizer", + "value": "CIRCLE VISUALIZER" + }, + { + "name": "circle_bar_visualizer", + "value": "CIRCLE BAR VISUALIZER" + }, + { + "name": "line_bar_visualizer", + "value": "LINE BAR VISUALIZER" + }, + { + "name": "ability_with_service", + "value": "ABILITY WITH SERVICE" + }, + { + "name": "square_bar_visualizer", + "value": "SQUARE BAR VISUALIZER" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/graphic/background_ability_main.xml b/entry/src/main/resources/base/graphic/background_ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..a002c2353e78af3a15235e00738328ff566bc085 --- /dev/null +++ b/entry/src/main/resources/base/graphic/background_ability_main.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_bar_visualizer.xml b/entry/src/main/resources/base/layout/ability_bar_visualizer.xml new file mode 100644 index 0000000000000000000000000000000000000000..55ad4c4109e828fa616fec512b4213f46c490eae --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_bar_visualizer.xml @@ -0,0 +1,31 @@ + + + + + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_circle_bar_visualizer.xml b/entry/src/main/resources/base/layout/ability_circle_bar_visualizer.xml new file mode 100644 index 0000000000000000000000000000000000000000..64835e89d4c840e9e598d93b9b559eee598c413b --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_circle_bar_visualizer.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_circle_visualizer.xml b/entry/src/main/resources/base/layout/ability_circle_visualizer.xml new file mode 100644 index 0000000000000000000000000000000000000000..84b33207fff511673221b5ca2935230a698aa355 --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_circle_visualizer.xml @@ -0,0 +1,25 @@ + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_line_bar_visualizer.xml b/entry/src/main/resources/base/layout/ability_line_bar_visualizer.xml new file mode 100644 index 0000000000000000000000000000000000000000..336610442add7f6c3368334b4eac6618c40634fb --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_line_bar_visualizer.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_line_visualizer.xml b/entry/src/main/resources/base/layout/ability_line_visualizer.xml new file mode 100644 index 0000000000000000000000000000000000000000..eac544c8fceb347ca448c75dba8a06a36f388db8 --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_line_visualizer.xml @@ -0,0 +1,30 @@ + + + + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/layout/ability_main.xml b/entry/src/main/resources/base/layout/ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..807839f03957e07fbd33e239e69ad57208c65d20 --- /dev/null +++ b/entry/src/main/resources/base/layout/ability_main.xml @@ -0,0 +1,107 @@ + + + +