gpt4 book ai didi

java - J2ME背景音乐

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:16 25 4
gpt4 key购买 nike

我想在我的 J2ME 游戏中播放背景音乐。我该如何编写该函数的代码?

最佳答案

您在 Playing Audio with J2ME 也有类似的问题

为方便起见,我从该线程发布了一些代码:

// loads the InputStream for the sound 
InputStream inputStream = this.getClass().getResourceAsStream( musicFile );

// create the standard Player
musicPlayer = Manager.createPlayer( inputStream, musicEncoding );
musicPlayer.prefetch();

// add player listener to access sound events
musicPlayer.addPlayerListener( this );

if( loopMusic )
{
// use the loop count method for infinite looping
musicPlayer.setLoopCount( -1 );
}

// The set occurs twice to prevent sound spikes at the very
// beginning of the sound.
VolumeControl volumeControl =
(VolumeControl) musicPlayer.getControl( "VolumeControl" );
volumeControl.setLevel( curVolume );

// finally start the piece of music
musicPlayer.start();

// set the volume once more
volumeControl = (VolumeControl) musicPlayer.getControl( "VolumeControl" );
volumeControl.setLevel( curVolume );

// finally, delete the input stream to save on resources
inputStream.close();
inputStream = null;

关于java - J2ME背景音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2046189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com