Android 应用程序加载后立即开始音频

发布于 2024-10-11 04:14:55 字数 275 浏览 5 评论 0原文

我正在创建一个游戏,游戏中有一些音频剪辑。我正在使用音频池,并且只使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-10-18 04:14:55

我刚刚在 Activity onCreate() 方法中使用了此代码,它工作正常:

public class LaunchActivity extends Activity {
       private MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whatever);

        setVolumeControlStream(AudioManager.STREAM_MUSIC); 
        mp = MediaPlayer.create(this, R.raw.a);
        mp.start();

    }

如果此实例没有停止指令,它将继续为应用程序中的所有活动播放,如果您退出应用程序,它仍然会播放并且当您回来时,新实例将同时开始播放旧实例!媒体播放器是它自己的进程,所以我认为不需要另一个线程。

您确定您的 MP3 文件没有问题并导致系统出现问题吗?

I have just used this code in an Activity onCreate() method it is working fine:

public class LaunchActivity extends Activity {
       private MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whatever);

        setVolumeControlStream(AudioManager.STREAM_MUSIC); 
        mp = MediaPlayer.create(this, R.raw.a);
        mp.start();

    }

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文