Android 应用程序加载后立即开始音频
我正在创建一个游戏,游戏中有一些音频剪辑。我正在使用音频池,并且只使用 2 个音频剪辑。它们都是mp3格式,一个是700kb,另一个是800字节。
我试图在游戏首次加载时播放 700kb 音频剪辑。只要应用程序打开,这种背景声音就会永远循环。然而我尝试过的一切都失败了。我能够让它工作的唯一方法是在启动时启动一个线程并让该线程休眠大约 15 秒。之后,它调用开始音频并播放,但如果线程被破坏,它将杀死声音。
我假设音频可能正在缓冲,这就是为什么它在开始时不播放?另外,您是否必须从线程中进行所有音频呼叫?
I'm creating a game, and I have a few audio clips in the game. I'm using audio pool and I'm only using 2 audio clips. They are both mp3 format, 1 is 700kb and the other is 800 bytes.
I'm trying to play the 700kb audio clip when the game is first loaded. It's a background sound that will loop forever as long as the app is open. However everything I've tried fails. The only way I was able to get it to work is to start a thread on startup and have the thread sleep for like 15 seconds. After that it calls the start audio and it plays, but if the thread is destroyed it will kill the sound with it.
I'm assuming the audio is buffering maybe and that's why it doesn't play at start? Also do you have to make all audio calls from a thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚在 Activity onCreate() 方法中使用了此代码,它工作正常:
如果此实例没有停止指令,它将继续为应用程序中的所有活动播放,如果您退出应用程序,它仍然会播放并且当您回来时,新实例将同时开始播放旧实例!媒体播放器是它自己的进程,所以我认为不需要另一个线程。
您确定您的 MP3 文件没有问题并导致系统出现问题吗?
I have just used this code in an Activity onCreate() method it is working fine:
If there is no stop instructions for this instance it will continue to play for all activities in the application, and if you exit your application it will still play and when you come back a new instance will start to play over the old at the same time! The media player is its own process so I think there is no need for another thread.
Are you sure something is not wrong with your MP3 file and makes the system struggle?