current position:Home>How to realize Android entertainment live broadcast / game special sound effect
How to realize Android entertainment live broadcast / game special sound effect
2022-07-17 07:37:56【Zego instant developer】
1 Function introduction
We're on the air , In order to enhance the sense of reality , The short effect sound that needs to be played to set off the scene atmosphere . for example : Applause 、 Laughter 、 Gift sound 、 Cue tone, etc . In the game , Sometimes you need to play bullets 、 Impact sound, etc .
ZegoExpress SDK Provide sound file player , adopt ZegoAudioEffectPlayer Unified management of sound effects , Support sound effect play ( Multiple sound effects can be overlapped )、 Playback controls ( Such as pause play 、 Volume adjustment 、 Set the playback schedule )、 Pre loading sound effects and other functions .
2 Support formats
The sound file player supports playing MP3、M4A、AAC、WAV Local audio file in format .
3 Sample source code download
Please refer to Download sample source code Access to the source code .
Please check the relevant source code “/ZegoExpressExample/AdvancedAudioProcessing/src/main/java/im/zego/advancedaudioprocessing/audioeffectplayer” A file in a directory .
4 Prerequisite
Before implementing the function of sound file player , Please make sure :
- Integrated in project ZEGO Express SDK, Realize basic real-time audio and video functions , Please refer to Quick start - Integrate and Quick start - Implementation process .
- Already in ZEGO Console Create project , And apply for a valid AppID, Please refer to Console - project management Medium “ Project information ”.
5 Use steps
5.1 Create a sound player
call ZegoExpressEngine
Of createAudioEffectPlayer
Method to create a sound player instance .
Only one instance can be created at the same time , When exceeded, it will return to null
.
ZegoAudioEffectPlayer audioEffectPlayer = ZegoExpressEngine.getEngine().createAudioEffectPlayer();
5.2 Playback controls
5.2.1 ( Optional ) Set event callback for sound player
Sound player event callback settings You can call the... Of the sound player as needed setEventHandler
Method to set the event callback for the player , For monitoring “ Sound playback status changes ” Wait for notice .
audioEffectPlayer.setEventHandler(new IZegoAudioEffectPlayerEventHandler() {
@Override
public void onAudioEffectPlayStateUpdate(ZegoAudioEffectPlayer audioEffectPlayer, int audioEffectID, ZegoAudioEffectPlayState state, int errorCode) {
Log.d("[ZEGO]", "onAudioEffectPlayStateUpdate errorCode:" + errorCode + " audioEffectID:" + audioEffectID + " state:" + state);
}
});
5.2.2 Start playing
call start
Method to play the sound effect , At present, only simultaneous playback is supported 12 individual , And can only be local files , Playing network resources is not supported . “audioEffectID” Need to keep the whole situation unique .
- If it has passed
loadResource
Method preloads the sound effects , Then you only need to pass in the “audioEffectID”,“path”( The path of sound resources ) Just leave the field blank . - If you need to play repeatedly, you can use
ZegoAudioEffectPlayConfig
in “playCount” Configure the number of repetitions . If set to “0”, Play infinity means repeat , Until the user manually callsstop
stop it .
int audioEffectID = 1;
ZegoAudioEffectPlayConfig config = new ZegoAudioEffectPlayConfig();
config.playCount = 10;
config.isPublishOut = true;
audioEffectPlayer.start(audioEffectID,"/storage/emulated/0/Android/data/im.zego.express.example.video/files/3-s.mp3",config);
5.2.3 Pause / recovery / Stop playing
- call
pause
Method can pause the playback “audioEffectID” Specified sound effect , callpauseAll
Method pauses all playing sound effects . - After the sound effect pauses , call
resume
Method to resume playback “audioEffectID” Specified sound effect , callresumeAll
Method restores all paused sound effects . - call
stop
Method can stop playing “audioEffectID” Specified sound effect , callstopAll
Method stops playing all sound effects .
audioEffectPlayer.pause(audioEffectID);
audioEffectPlayer.resume(audioEffectID);
audioEffectPlayer.stop(audioEffectID);
audioEffectPlayer.pauseAll();
audioEffectPlayer.resumeAll();
audioEffectPlayer.stopAll();
5.2.4 Adjust the volume
- call
setVolume
Method can be set “audioEffectID” Specified sound volume , The value range is [0, 200], The default value is “100”. - call
setVolumeAll
Method sets the volume of all sound effects at the same time , The value range is [0, 200], The default value is “100”.
int volume =70;
audioEffectPlayer.setVolume(audioEffectID, volume);
// Set the volume of all sound effects
audioEffectPlayer.setVolumeAll(volume);
5.2.5 Play progress control
- call
getTotalDuration
Method to obtain the total duration of a single sound effect . - call
getCurrentProgress
Method to obtain the current playback progress of the sound effect . - call
seekTo
Method can set the playback progress as needed .
// The total time to get the sound effect
long totalDuration = audioEffectPlayer.getTotalDuration(audioEffectID);
// Get the current playback progress of the sound effect
long progress = audioEffectPlayer.getCurrentProgress(audioEffectID);
// Set the playback schedule
audioEffectPlayer.seekTo(audioEffectID, 1, new IZegoAudioEffectPlayerSeekToCallback() {
@Override
public void onSeekToCallback(int errorCode) {
Log.d("[ZEGO]", "onSeekToCallback errorCode:" + errorCode);
}
});
5.3 ( Optional ) Preload resources
Preload resourcesIn a scene where the same sound effect is played frequently ,SDK In order to optimize the performance of repeatedly reading and decoding files , It provides the function of preloading sound effect files into memory .
call loadResource
Method to load sound effect resources , It can be done by “callback” Parameter to listen for the loading result , The display can only be played after loading successfully . Support simultaneous preloading at most 15 A local sound file ( Network resources are not supported ), And the duration of a single sound effect file cannot exceed 30 s, Otherwise, an error will be reported .
When the loaded sound effect is used , You can call unloadResource
Interface uninstall , To release resources . otherwise SDK Will be in ZegoAudioEffectPlayer
The loaded sound effects will be automatically unloaded when the instance is released .
Preloading is not required , In order to improve performance or when you need to play a specific sound effect repeatedly, it is recommended to use .
// Load sound resources
audioEffectPlayer.loadResource(audioEffectID, "/storage/emulated/0/Android/data/im.zego.express.example.video/files/3-s.mp3", new IZegoAudioEffectPlayerLoadResourceCallback() {
@Override
public void onLoadResourceCallback(int i) {
Log.d("[ZEGO]", "onLoadResourceCallback errorCode:" + i );
}
});
// Uninstall sound resources
audioEffectPlayer.unloadResource(audioEffectID);
5.4 Destroy the media player
After using the sound player , Need to call in time destroyAudioEffectPlayer
Method destruction , Release the resources occupied by the player .
engine.destroyAudioEffectPlayer(audioEffectPlayer);
6 API Reference list
Method | describe |
---|---|
createAudioEffectPlayer | Create a sound player instance |
setEventHandler | Set the sound player callback |
start | Play sound effects |
pause | Pause playing a single sound effect |
pauseAll | Pause all sound effects |
resume | Resume playing a single sound effect |
resumeAll | Resume playing all sound effects |
stop | Stop playing a single sound effect |
stopAll | Stop playing all sound effects |
setVolume | Adjust sound volume |
setVolumeAll | Adjust the volume of all sound effects |
getTotalDuration | Control playback progress |
getCurrentProgress | Get the current playback progress |
seekTo | Set the specified playback progress |
loadResource | Preload resources |
unloadResource | Unload resources |
destroyAudioEffectPlayer | Destroy the sound player instance |
7 What's the difference between a sound player and a media player ?
- Media player is mainly used to play video and long music , Support playing network resources . At most... Can be created at the same time 4 Player instances , An instance can only play one audio and video .
- The sound effect player is mainly used for playing sound effects with short time , Playing network resources is not supported . Only one instance of sound player can be created at the same time , The sound player supports simultaneous playback of multi-channel sound effects , At most one instance can be played simultaneously 12 A sound effect .
obtain Demo
obtain In this paper, the Demo、 Developing documents 、 Technical support .
obtain SDK Business activities 、 Hot products .
Registration constitutes ZEGO Developer account number , Quick start .
copyright notice
author[Zego instant developer],Please bring the original link to reprint, thank you.
https://en.bfun.fun/2022/198/202207150520363075.html
The sidebar is recommended
- SARFT: illegal and immoral artists are forbidden to appear in TV dramas
- It's not just friends that reunites
- Finally, there is a movie. Let's say goodbye to Wu Mengda on the screen
- "Never forget the farm" after a visit, everyone can find the courage to face aging
- NFT insider 65: South Korean entertainment giant CJ ENM has reached a cooperation with the sandbox, and YGG has established a subdao in the Brazilian community
- "Mom! I realized Gu Ailing's happiness... Ah - help, help!"
- Gaya decrypts the front and back scenes of the 2022 Oriental satellite TV Spring Festival Gala
- Wu Yue sent an angry report! Rumor father said that she was abandoned by her boyfriend and was still babbled after 16 years of separation
- Stars appear in the rehearsal of CCTV Lantern Festival party, and Nigel Maiti will pick the beam again? Wan Qian and Song Yi are well dressed
- Official propaganda "sugar"! 2022 "tiger adds wings" small palm Lantern Festival comes, zero consumption can also win awards
guess what you like
The Spring Festival has achieved their "ten billion" miracle!
February 14th! "Zhiyin" is about to resume its flight
No emo on Valentine's Day| Ticket delivery
Rizhao newborn's popular name comes out! The three names with the most duplicate names are
The ice dance drama we are one starring Pang Qing and Tong Jian, the world champion, premiered successfully at Beijing overpass Art Center
Wang Lili, who did not participate, became one of the hottest people in this winter Olympics
Zhang Yimou, who is in his seventies, simply "changed the world" with the glory of the Winter Olympics
Bao Wenjing's family went shopping in the supermarket. The dumplings were exquisite and changed a lot. Their height was close to Bao Bell's waist
Many times on CCTV Spring Festival Gala! Reveal the secret of lucky lion
Experts decode 2022 Spring Festival: presenting a new look of Chinese films
Random recommended
- The animated film "pillow knife song: a journey to the earth" was highly praised, and the soundtrack was pleasantly online
- This Sunday evening, there will be a festive event in Nanfeng City, Hongqiao... | aishenhuo warms the heart
- "Don't forget I love you" Naza and Liu Yihao perform the most beautiful confession
- From Ling Na BEI'ER to Bing Dwen Dwen, from Liu Xiang to Gu ailing
- You are so beautiful! Anne Hathaway appeared on Broadway to support uncle wolf's musical
- The grand prize was unveiled, and Jinnan court was on the list
- It's half time, come and look around!
- Cao Jingxing, a media man, died at the age of 75 and was once called "news radar"
- Veterans will always be young, dreams will always shine | Beijing to the future focuses on the Chinese army that will never give up
- Dongxi asked | how much do you know about "Baba niangya" in the TV series "little niangya"?
- The audience is abandoning the cinema?
- The online drama "Hello, Captain" is about to start a new chapter of civil aviation drama
- Exclusive dialogue -- Peng Xiaoran: pay tribute to the Winter Olympic athletes, who are familiar with northeast cuisine
- "Our hot life" Chen Xiaochun's first experience of legal aid Zhang Qi's visit to women's prison tears collapse
- Bloom the beauty of Winter Olympics with "the name of ice and snow"
- Naza basks in the photo of Lord snow eagle, holding flowers and smiling sweetly
- Friends returns: why is this play so happy? Because it is the portrayal of our ideal life
- Happy thousands of smiles show the new year's new atmosphere
- Retro beauty! Zhang Zifeng's self portrait of his face is super cute to the camera
- After 12 years as a supporting actor, he was almost expelled from happy fried dough twist. Now Wei Xiang is successful as the male number one?
- Ten male stars of "drama dishonor": if there are more such actors, the audience will not be poisoned by the rotten film
- From the king of TV viewing to the queen of turntable, why is Xie Na, the "first sister of satellite TV", less and less likeable
- On the thirty six strategies of Chinese traditional art of war from "sniper"
- Chen Suping: years flow, keeping the miss of Gu Yuezhen and the soul of "hard work"
- Screenwriter Wang Hailing talks about behind the scenes: not all life should be open, and "the world" can comfort ordinary people
- Rebirth entertainment is developing a new single FPS and the same universe as apex hero
- Rebirth entertainment may develop a new single FPS game based on the universe of apex heroes
- Farewell film curtain call performance, giant screen cinema of China Science and Technology Museum will start digital transformation
- Detective 7 pays attention to campus bullying, judges' panel online popularization of legal knowledge
- Li Fei directed the warm comedy "Nobel's gift" and started it. Zhang Benyu, Han Haolin, pan Binlong explored the true meaning of family affection
- Female star data exposure, Liu Yifei is the most popular, and Yang Mi's baby is not in the top three
- "I want to go to your world and love you" released the theme song MV Jiao Maiqi's warm singing of the theme song of the premiere film
- 40% red line! SARFT continues to regulate sky high film remuneration
- Essence of SIFF film school | Dennis Villeneuve: pursuing poetry and intuition in the dunes of film
- Liu Yu, a member of into1, took a funny group photo for his teammate Bo Yuan's birthday
- Workplace gongdou? Fashion circle melon collection? Yuan Yongyi, Song Jia, song Zuer's new play "dress up" is a bit of a hit
- The costume action movie "Daming flying fish suit" appeared on the international stage as a dark horse
- What is the experience of walking on the red carpet in school uniforms
- Urban workplace drama "dress up" is popular, and the old depression points to the rules of workplace survival
- Simple and thick epic masterpiece | after the view of CCTV reality TV series "human world"