android - Intent 加载时声音未响起

标签 android audio android-intent soundpool

我正在使用意图从另一个类(class)调用这个类(class),我希望在加载该类(class)时播放这首歌曲“song.ogg” ..请帮助我如何做?

public class DetlsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detls_layout);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        SoundPool soundPool;
        int soundID;
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundID = soundPool.load(this,R.raw.song, 1);
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        //soundPool.play(soundID,1f,1f,1,0,1f);
        Toast.makeText(this,"Ringing Song",Toast.LENGTH_LONG).show();
        try {
        AssetFileDescriptor assetFileDescriptor = this.getAssets().openFd("song.ogg");
        soundPool.load(assetFileDescriptor,1);
            Toast.makeText(this,"Played",Toast.LENGTH_LONG).show();
        }
        catch (Exception e) {
            Log.d("LOGCAT","Exception Song");
        }

最佳答案

    SoundPool soundPool;
    int soundID;
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    Toast.makeText(this,"Ringing Song",Toast.LENGTH_LONG).show();
    try {
        soundID = soundPool.load(this,R.raw.song, 1);
        int waitLimit = 1000;
        int waitCounter = 0;
        int throttle = 10;
        while(soundPool.play(soundID, 1.f, 1.f, 1, 0, 1.f) == 0 && waitCounter < waitLimit)
          {waitCounter++; SystemClock.sleep(throttle);}
        Toast.makeText(this,"Played "+soundID,Toast.LENGTH_LONG).show();

    }
    catch (Exception e) {
        Log.d("LOGCAT","Exception Song");
        Toast.makeText(this,"Exception "+e,Toast.LENGTH_LONG).show();
    }

用上面的代码替换您的代码。当前代码给出警告声池样本1尚未准备好,因此请使用while条件进行检查。

在这里,您的资源位于原始文件夹中,这就是我们使用soundID = soundPool.load(this,R.raw.song, 1);的原因。如果您的资源当时位于Assets文件夹中,则可以使用soundID = soundPool.load(getAssets().openFd("song.ogg"),1);

关于android - Intent 加载时声音未响起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26602357/

相关文章:

c++ - gstreamer 的音乐可视化错误

c++ - XAudio2 延迟声音 - 播放多个声音时

android - 在 Twitter 上使用 SHARE INTENT 共享组合文本和图像时出现问题

android - 我需要在我的 ExpandableListView 中延迟我的组的扩展

bash - 我需要一些基本的BASH帮助-查找+ execdir + sox

android - 我可以覆盖 Android 输入法处理自动完成和自动更正的方式吗?

android - 如何在 Activity 关闭时触发重新加载?

java - 按手机上的后退按钮有不同的行为来返回 Intent

java.lang.IllegalArgumentException : Unknown URL file at android. content.ContentResolver.delete

android - 无法捕获 ExoPlaybackException : Source error