diff --git a/media/Audio/README.md b/media/Audio/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fb337db9920b9cdffa711d8abf22100696ebe1e0 --- /dev/null +++ b/media/Audio/README.md @@ -0,0 +1,13 @@ +# Audio + +- The audio module is used to implement audio-related features, including audio playback, recording, and volume management. + + Your application can invoke the APIs to convert audio data into audible analog signals, play the audio signals using output devices, and manage playback tasks. + + Your application can invoke the APIs to record voices using input devices, convert the voices into audio data, and manage recording tasks. + + Your application can invoke the APIs to adjust volume, manage input and output devices, and listen for audio interrupts and recording interrupts. + + To play sounds, your application needs invoke the APIs to manage audio resource loading and playback, tone creation and playback, and system sound playback. + + diff --git a/media/Audio/README_zh.md b/media/Audio/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..2f56244d9411196901a3488bb0f3d28cb66f3e43 --- /dev/null +++ b/media/Audio/README_zh.md @@ -0,0 +1,13 @@ +# 音频 + +- 音频模块支持音频业务的开发,提供音频相关的功能,主要包括音频播放、音频采集、音量管理和短音播放等。 + + 1、音频播放的主要工作是将音频数据转码为可听见的音频模拟信号并通过输出设备进行播放,同时对播放任务进行管理。 + + 2、音频采集的主要工作是通过输入设备将声音采集并转码为音频数据,同时对采集任务进行管理。 + + 3、音量管理的主要工作是音量调节,输入/输出设备管理,注册音频中断、音频采集中断的回调等。 + + 4、短音播放主要负责管理音频资源的加载与播放、tone音的生成与播放以及系统音播放。 + + diff --git a/media/Audio/build.gradle b/media/Audio/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..e76a14f743baa5377849b43abf29dd0fe29b6478 --- /dev/null +++ b/media/Audio/build.gradle @@ -0,0 +1,34 @@ +// 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 4 + } +} +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.4.2' + } +} +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} \ No newline at end of file diff --git a/media/Audio/entry/build.gradle b/media/Audio/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..1324a93eda46cb945cc94de68b1cb0d1bcca10d1 --- /dev/null +++ b/media/Audio/entry/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/media/Audio/entry/src/main/config.json b/media/Audio/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..096f8f4bb88f525204c3c4bb5ef0eba1f6e811e7 --- /dev/null +++ b/media/Audio/entry/src/main/config.json @@ -0,0 +1,62 @@ +{ + "app": { + "bundleName": "ohos.samples.audio", + "version": { + "code":1000000, + "name": "1.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.audio", + "name": ".MainAbility", + "reqCapabilities": [ + "video_support" + ], + "deviceType": [ + "default" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree":false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "formsEnabled": false, + "name": ".MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.READ_USER_STORAGE" + }, + { + "name": "ohos.permission.MANAGE_USER_STORAGE" + }, + { + "name": "ohos.permission.WRITE_USER_STORAGE" + }, + { + "name": "ohos.permission.MICROPHONE" + } + ] + } +} \ No newline at end of file diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/MainAbility.java b/media/Audio/entry/src/main/java/ohos/samples/audio/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..44a1bc16e5b20e9354d19d50980ba9f62203947c --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/MainAbility.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 ohos.samples.audio; + +import ohos.samples.audio.slice.MainAbilitySlice; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.bundle.IBundleManager; +import ohos.global.resource.RawFileEntry; +import ohos.global.resource.Resource; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.security.SystemPermission; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Mainability + */ +public class MainAbility extends Ability { + private static final String TAG = MainAbility.class.getSimpleName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(MainAbilitySlice.class.getName()); + requestPermission(); + } + + private void requestPermission() { + String[] permissions = { + SystemPermission.WRITE_USER_STORAGE, SystemPermission.MICROPHONE + }; + List permissionFiltered = Arrays.stream(permissions) + .filter(permission -> verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED) + .collect(Collectors.toList()); + + requestPermissionsFromUser(permissionFiltered.toArray(new String[permissionFiltered.size()]), 0); + } + + @Override + public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { + if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) { + return; + } + if (requestCode == 0) { + if (grantResults[0] == IBundleManager.PERMISSION_GRANTED) { + writeToDisk(); + }else { + terminateAbility(); + } + } + } + + private void writeToDisk() { + String rawFilePath = "entry/resources/rawfile/sample.mp3"; + String externalFilePath = getFilesDir() + "/sample.mp3"; + File file = new File(externalFilePath); + if (file.exists()) { + return; + } + RawFileEntry rawFileEntry = getResourceManager().getRawFileEntry(rawFilePath); + try (FileOutputStream outputStream = new FileOutputStream(new File(externalFilePath))) { + Resource resource = rawFileEntry.openRawFile(); + byte[] cache = new byte[1024]; + int len = resource.read(cache); + while (len != -1) { + outputStream.write(cache, 0, len); + len = resource.read(cache); + } + } catch (IOException exception) { + HiLog.error(LABEL_LOG, "%{public}s", "writeToDisk: IOException"); + } + } +} diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..034c27dc4ba9afaf0c583703550b2dc270c18b63 --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 ohos.samples.audio.slice; + +import ohos.samples.audio.ResourceTable; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Button; +import ohos.agp.components.Component; +import ohos.agp.components.Text; +import ohos.agp.window.dialog.ToastDialog; +import ohos.app.dispatcher.task.TaskPriority; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.media.audio.AudioCapturer; +import ohos.media.audio.AudioCapturerCallback; +import ohos.media.audio.AudioCapturerConfig; +import ohos.media.audio.AudioCapturerInfo; +import ohos.media.audio.AudioDeviceDescriptor; +import ohos.media.audio.AudioManager; +import ohos.media.audio.AudioStreamInfo; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; + +/** + * AudioCapturer + */ +public class AudioRecorderSlice extends AbilitySlice { + private static final String TAG = AudioRecorderSlice.class.getName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + private static final int SAMPLE_RATE = 44100; + + private static final int BUFFER_SIZE = 1024; + + private Button recordButton; + + private AudioManager audioManager = new AudioManager(); + + private AudioCapturer audioCapturer; + + private File file = null; + + private boolean isRecording = false; + + private Text pathText; + + private AudioCapturerCallback callback = new AudioCapturerCallback() { + @Override + public void onCapturerConfigChanged(List configs) { + HiLog.info(LABEL_LOG, "%{public}s", "on capturer config changed"); + } + }; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_audio_recorder_slice_layout); + initComponents(); + initRecord(); + } + + private void initComponents() { + recordButton = (Button) findComponentById(ResourceTable.Id_main_start_recoding); + pathText = (Text) findComponentById(ResourceTable.Id_path_text); + recordButton.setClickedListener(this::record); + } + + private void initRecord() { + audioManager.registerAudioCapturerCallback(callback); + AudioDeviceDescriptor[] devices = AudioManager.getDevices(AudioDeviceDescriptor.DeviceFlag.INPUT_DEVICES_FLAG); + AudioDeviceDescriptor currentAudioType = devices[0]; + AudioCapturerInfo.AudioInputSource source = AudioCapturerInfo.AudioInputSource.AUDIO_INPUT_SOURCE_MIC; + AudioStreamInfo audioStreamInfo = new AudioStreamInfo.Builder().audioStreamFlag( + AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_AUDIBILITY_ENFORCED) + .encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT) + .channelMask(AudioStreamInfo.ChannelMask.CHANNEL_IN_STEREO) + .streamUsage(AudioStreamInfo.StreamUsage.STREAM_USAGE_MEDIA) + .sampleRate(SAMPLE_RATE) + .build(); + AudioCapturerInfo audioCapturerInfo = new AudioCapturerInfo.Builder().audioStreamInfo(audioStreamInfo) + .audioInputSource(source) + .build(); + audioCapturer = new AudioCapturer(audioCapturerInfo, currentAudioType); + } + + @Override + protected void onStop() { + super.onStop(); + if (audioCapturer != null) { + audioCapturer.stop(); + audioCapturer.release(); + } + audioManager.unregisterAudioCapturerCallback(callback); + audioManager = null; + } + + private void record(Component component) { + if (isRecording && audioCapturer != null) { + stopRecord(); + return; + } + startRecord(); + } + + private void stopRecord() { + if (audioCapturer.stop()) { + isRecording = false; + recordButton.setText("Start"); + showTips("Stop record"); + pathText.setText("Path:" + getFilesDir() + File.separator + "record.mp3"); + } + } + + private void startRecord() { + if (audioCapturer.start()) { + isRecording = true; + recordButton.setText("Stop"); + showTips("Start record"); + runRecord(); + } + } + + private void runRecord() { + getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() -> { + file = new File(getFilesDir() + File.separator + "record.mp3"); + try (FileOutputStream outputStream = new FileOutputStream(file)) { + byte[] bytes = new byte[BUFFER_SIZE]; + while (audioCapturer.read(bytes, 0, bytes.length) != -1) { + outputStream.write(bytes); + bytes = new byte[BUFFER_SIZE]; + outputStream.flush(); + } + } catch (IOException exception) { + HiLog.error(LABEL_LOG, "%{public}s", "record exception"); + } + }); + } + + private void showTips(String message) { + new ToastDialog(this).setText(message).show(); + } +} diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/MainAbilitySlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/MainAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..43d59c190e4e9a7505082ab1d1d97ded90807123 --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/MainAbilitySlice.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 ohos.samples.audio.slice; + +import ohos.samples.audio.ResourceTable; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Component; + +/** + * MainAbilitySlice + */ +public class MainAbilitySlice extends AbilitySlice { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_main_ability_slice); + initComponents(); + } + + private void initComponents() { + Component playerButton = findComponentById(ResourceTable.Id_player_button); + Component capturerButton = findComponentById(ResourceTable.Id_capturer_button); + Component shortSoundPlayer = findComponentById(ResourceTable.Id_short_sound_button); + + playerButton.setClickedListener(component -> present(new PlayerSlice(), new Intent())); + capturerButton.setClickedListener(component -> present(new AudioRecorderSlice(), new Intent())); + shortSoundPlayer.setClickedListener(component -> present(new ShortSoundPlayerSlice(), new Intent())); + } +} diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/PlayerSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/PlayerSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..06873d3ce3329d2053a858b4d3c8c45b59b2c930 --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/PlayerSlice.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 ohos.samples.audio.slice; + +import ohos.samples.audio.ResourceTable; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Button; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.media.audio.AudioInterrupt; +import ohos.media.audio.AudioManager; +import ohos.media.audio.AudioRenderer; +import ohos.media.audio.AudioRendererInfo; +import ohos.media.audio.AudioStreamInfo; +import ohos.media.codec.Codec; +import ohos.media.common.BufferInfo; +import ohos.media.common.Source; + +import java.nio.ByteBuffer; + +/** + * PlayerSlice + */ +public class PlayerSlice extends AbilitySlice { + private static final String TAG = PlayerSlice.class.getName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + private static final int SAMPLE_RATE = 44100; + + private static final int BUFFER_SIZE = 1024; + + private AudioRenderer audioRenderer; + + private Codec codec; + + private boolean isPlaying; + + private Button playButton; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_player_slice_layout); + initComponents(); + initAudioRenderer(); + } + + private void initComponents() { + playButton = (Button) findComponentById(ResourceTable.Id_playButton); + playButton.setClickedListener(component -> play()); + } + + private void initAudioRenderer() { + AudioStreamInfo audioStreamInfo = new AudioStreamInfo.Builder().sampleRate(SAMPLE_RATE) + .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 audioRendererInfo = new AudioRendererInfo.Builder().audioStreamInfo(audioStreamInfo) + .audioStreamOutputFlag(AudioRendererInfo.AudioStreamOutputFlag.AUDIO_STREAM_OUTPUT_FLAG_DIRECT_PCM) + .bufferSizeInBytes(BUFFER_SIZE) + .isOffload(false) + .sessionID(AudioRendererInfo.SESSION_ID_UNSPECIFIED) + .build(); + audioRenderer = new AudioRenderer(audioRendererInfo, AudioRenderer.PlayMode.MODE_STREAM); + audioRenderer.setVolume(1.0f); + audioRenderer.setSpeed(1.0f); + setPlayCallback(audioStreamInfo); + } + + private void play() { + if (isPlaying) { + audioRenderer.pause(); + isPlaying = false; + playButton.setText("Play"); + } else { + audioRenderer.start(); + decoderAudio(); + isPlaying = true; + playButton.setText("Pause"); + } + } + + private void setPlayCallback(AudioStreamInfo streamInfo) { + AudioInterrupt audioInterrupt = new AudioInterrupt(); + AudioManager audioManager = new AudioManager(); + audioInterrupt.setStreamInfo(streamInfo); + audioInterrupt.setInterruptListener((type, hint) -> { + if (type == AudioInterrupt.INTERRUPT_TYPE_BEGIN && hint == AudioInterrupt.INTERRUPT_HINT_PAUSE) { + HiLog.info(LABEL_LOG, "%{public}s", "sound paused"); + } else if (type == AudioInterrupt.INTERRUPT_TYPE_BEGIN && hint == AudioInterrupt.INTERRUPT_HINT_STOP) { + HiLog.info(LABEL_LOG, "%{public}s", "sound stopped"); + } else if (type == AudioInterrupt.INTERRUPT_TYPE_END && (hint == AudioInterrupt.INTERRUPT_HINT_RESUME)) { + HiLog.info(LABEL_LOG, "%{public}s", "sound resumed"); + } else { + HiLog.info(LABEL_LOG, "%{public}s", "unknown state"); + } + }); + audioManager.activateAudioInterrupt(audioInterrupt); + } + + private void decoderAudio() { + if (codec == null) { + codec = Codec.createDecoder(); + } else { + codec.stop(); + } + + Source source = new Source(getFilesDir() + "/sample.mp3"); + codec.setSource(source, null); + codec.registerCodecListener(new Codec.ICodecListener() { + @Override + public void onReadBuffer(ByteBuffer outputBuffer, BufferInfo bufferInfo, int trackId) { + audioRenderer.write(outputBuffer, bufferInfo.size); + } + + @Override + public void onError(int errorCode, int act, int trackId) { + HiLog.error(LABEL_LOG, "%{public}s", "codec registerCodecListener error"); + } + }); + codec.start(); + } + + @Override + protected void onStop() { + super.onStop(); + if (codec != null) { + codec.stop(); + } + if (audioRenderer != null) { + audioRenderer.release(); + } + } +} diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/ShortSoundPlayerSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/ShortSoundPlayerSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..a9529b7e9fe0d3aec16c963002f3f57d9f60d598 --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/ShortSoundPlayerSlice.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 ohos.samples.audio.slice; + +import static ohos.media.audio.SoundPlayer.SoundType.KEYPRESS_STANDARD; +import static ohos.media.audio.ToneDescriptor.ToneType.DTMF_6; + +import ohos.samples.audio.ResourceTable; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Component; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.media.audio.AudioManager; +import ohos.media.audio.SoundPlayer; + +/** + * ShortSoundPlayerSlice + * + * @since 2020-12-22 + */ +public class ShortSoundPlayerSlice extends AbilitySlice implements SoundPlayer.OnCreateCompleteListener { + private static final String TAG = ShortSoundPlayerSlice.class.getSimpleName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + private static final int TONE_SOUND_DURATION = 800; + + private SoundPlayer soundPlayer; + + @Override + protected void onStart(Intent intent) { + super.onStart(intent); + setUIContent(ResourceTable.Layout_short_sound_player_slice_layout); + initComponents(); + } + + private void initComponents() { + Component toneButton = findComponentById(ResourceTable.Id_tone_button); + Component systemButton = findComponentById(ResourceTable.Id_system_button); + Component musicPlayButton = findComponentById(ResourceTable.Id_music_button); + + toneButton.setClickedListener(component -> playToneSound()); + systemButton.setClickedListener(component -> playSystemSound()); + musicPlayButton.setClickedListener(this::playMusicSound); + } + + private void playMusicSound(Component component) { + releaseSoundPlayer(); + soundPlayer = new SoundPlayer(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue()); + soundPlayer.createSound(getFilesDir() + "/sample.mp3"); + soundPlayer.setOnCreateCompleteListener(this); + } + + @Override + public void onCreateComplete(SoundPlayer soundPlayer, int cacheId, int status) { + HiLog.info(LABEL_LOG, "%{public}s", "on create complete"); + if (status == 0) { + int result = soundPlayer.play(cacheId); + soundPlayer.setLoop(result, 2); + soundPlayer.setPlaySpeedRate(result, 1.0f); + } + } + + private void playToneSound() { + SoundPlayer toneSoundPlayer = new SoundPlayer(); + SoundPlayer.AudioVolumes audioVolumes = new SoundPlayer.AudioVolumes(); + toneSoundPlayer.createSound(DTMF_6, TONE_SOUND_DURATION); + audioVolumes.setCentralVolume(1.0f); + audioVolumes.setSubwooferVolume(1.0f); + toneSoundPlayer.play(); + } + + private void playSystemSound() { + SoundPlayer systemSoundPlayer = new SoundPlayer(getBundleName()); + systemSoundPlayer.playSound(KEYPRESS_STANDARD, 1.0f); + } + + @Override + protected void onStop() { + releaseSoundPlayer(); + } + + private void releaseSoundPlayer() { + if (soundPlayer != null) { + soundPlayer.release(); + } + } +} diff --git a/media/Audio/entry/src/main/resources/base/element/string.json b/media/Audio/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a677b58e114f80ef7954d072aaaa0e18a5f35af7 --- /dev/null +++ b/media/Audio/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "Audio" + }, + { + "name": "mainability_description", + "value": "hap sample empty page" + } + ] +} \ No newline at end of file diff --git a/media/Audio/entry/src/main/resources/base/graphic/button_bg.xml b/media/Audio/entry/src/main/resources/base/graphic/button_bg.xml new file mode 100644 index 0000000000000000000000000000000000000000..f4a98035344fc8f1a3211c66ec8098eb24fdaf66 --- /dev/null +++ b/media/Audio/entry/src/main/resources/base/graphic/button_bg.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/media/Audio/entry/src/main/resources/base/layout/audio_recorder_slice_layout.xml b/media/Audio/entry/src/main/resources/base/layout/audio_recorder_slice_layout.xml new file mode 100644 index 0000000000000000000000000000000000000000..211cb9e7497b2529d06d76e3aae7db8e10420250 --- /dev/null +++ b/media/Audio/entry/src/main/resources/base/layout/audio_recorder_slice_layout.xml @@ -0,0 +1,54 @@ + + + + + + +