鸿蒙手机(真机)播放音乐-第一集
这个项目会实现一个非常非常简单和粗糙的音乐播放器(只能听个响),之后会发第二集或者第三集进行优化
1.新建工程
自己去看https://blog.csdn.net/qq_33259323/article/details/112405157
2.准备音乐文件
测试的音乐文件:https://download.csdn.net/download/qq_33259323/14906054
在resources/rawfile文件夹下新music文件夹,然后把上面的测试文件复制到里面去
3.编写PA调用FA
PA调用FA 如果看不明白的可以看https://blog.csdn.net/qq_33259323/article/details/112466112
网页
<div class="container">
<div>
<text class="title">
{
{ $t('strings.hello') }}{
{title}}
</text>
</div>
<div>
<text class="title" onclick="load">
开始播放
</text>
<text class="title" onclick="start">
播放音乐
</text>
<text class="title" onclick="stop">
暂停音乐
</text>
</div>
</div>
export default {
data: {
title: ""
},
onInit() {
this.title = "我是大喵";
},
load(){
playAbility.play(ACTION_SOUND_LOAD);
},
start(){
playAbility.play(ACTION_SOUND_START);
},
stop(){
playAbility.play(ACTION_SOUND_STOP);
}
}
PA伪代码,PA的代码非常简单,就是传入不同地Code。页面点击事件通过传入不同的Code来实现不同的功能
// code
const ACTION_SOUND_LOAD = 2001;
const ACTION_SOUND_START = 2002;
const ACTION_SOUND_STOP = 2003;
export const playAbility={
play: async function(code){
var action = {};
action.bundleName = 'com.example.phone';
action.abilityName = 'com.example.phone.ability.PlayAbility';
action.messageCode = code;
action.data = {
};
action.abilityType = ABILITY_TYPE_EXTERNAL;
action.syncOption = ACTION_SYNC;
var result = await FeatureAbility.callAbility(action);
var ret = JSON.parse(result);
if (ret.code == 0) {
console.info(JSON.stringify(ret.abilityResult));
} else {
console.error(JSON.stringify(ret.code));
}
}
}
FA伪代码
扫描二维码关注公众号,回复:
12453056 查看本文章

@Override
protected void onStart(Intent intent) {
super.onStart(intent);
try {
playSoundUtil = new PlaySoundUtil();
} catch (IOException e) {
e.printStackTrace();
}
}
class PlayRemote extends RemoteObject implements IRemoteBroker {
public PlayRemote() {
super("PlayRemote");
}
@Override
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
case ACTION_SOUND_LOAD:{
System.out.println("ACTION_SOUND_LOAD");
playSoundUtil.loadSound("321.wav");
break;
}
case ACTION_SOUND_STOP: {
System.out.println("ACTION_SOUND_STOP");
playSoundUtil.getAudioRenderer().pause();
break;
}
case ACTION_SOUND_START: {
System.out.println("ACTION_SOUND_START");
playSoundUtil.getAudioRenderer().start();
break;
}
default: {
reply.writeString("service not defined");
return false;
}
}
return true;
}
@Override
public IRemoteObject asObject() {
return this;
}
}
4.播放音乐工具类
PlaySoundUtil.java
package com.example.phone.utils;
import ohos.media.audio.*;
import java.io.*;
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) // 44.1kHz
.audioStreamFlag(AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_MAY_DUCK) // 混音
.encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT) // 16-bit PCM
.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(false) // 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/music/%s", fileName);
soundInputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
int bufSize = audioRenderer.getBufferFrameSize();
System.out.println("bufSize"+bufSize);
byte[] buffer = new byte[1024] ;
int len;
try {
audioRenderer.start();
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;
}
}
5.运行
点击开始播放是可以播放的但是暂停无法暂停是为什么呢,请看第二集