android - 播放媒体文件时出错

标签 android

我正在使用 MediaPlayer 播放一些有时会重叠的声音文件。我注意到在 LogCat 窗口中我不断收到此消息:

android 组件 OMX.TI.ACC 的最大实例。解码已创建。

它似乎对我的应用程序没有影响,因为声音继续正常播放。有谁知道这条消息是什么意思,我需要担心吗?

最佳答案

SoundPool 可能是播放多个短声音的更好选择。

创建 SoundPool

public static final int SOUND_1 = 1;
public static final int SOUND_2 = 2;

SoundPool mSoundPool;
HashMap<Integer, Integer> mHashMap;

@Override
public void onCreate(Bundle savedInstanceState){
    mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
    mSoundMap = new HashMap<Integer, Integer>();

    if(mSoundPool != null){
        mSoundMap.put(SOUND_1, mSoundPool.load(this, R.raw.sound1, 1));
        mSoundMap.put(SOUND_2, mSoundPool.load(this, R.raw.sound2, 1));
    }
}

然后通过调用自定义函数播放声音。

播放声音

/*
*Call this function from code with the sound you want e.g. playSound(SOUND_1);
*/
public void playSound(int sound) {
    AudioManager mgr = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    float volume = streamVolumeCurrent / streamVolumeMax;  

    if(mSoundPool != null){
        mSoundPool.play(mSoundMap.get(sound), volume, volume, 1, 0, 1.0f);
    }
}

关于android - 播放媒体文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910703/

相关文章:

java - 如何为使用 setListAdapter 填充的 Listview 设置主题

android - 如何将 Gradle 下载限制为每个实例只下载一个文件

android - Google 控制台警告我的应用需要 CAMERA 权限

android - 如何在Android 10中下载pdf文件并将其存储在文件夹中?

android - 如何通过 cordoba 中的 webview 获得对 navigator.getusermedia 的许可?

android.app.Application 无法转换为扩展 android.app.Application 的类

android - CollapsingToolbarLayout 上的 Fling 滚动在折叠时停止

java - 在android广播接收器中创建数据库的代码

android - 从 Android 到 Web API 的 POST 数据返回 404

android - Android 中的线性布局和相对布局