阅读:2497回复:2

Mediaplayer播放提示音问题

楼主#
更多 发布于:2022-09-21 17:33
     客户的App使用MediaPlayer播放assets目录的ogg后缀的提示音没有声音,在rk3288-7.1使用没有问题,在9.0测试时候没有声音,我做了demo试了一下确实没有声音,assets目录放一些长一点的提示音和MP3都是正常的,但是放很短比如1秒左右的提示音就是没有声音,但是放在sdcard目录播放又有声音,看打印也看不出什么问题来,其实MediaPlayer占用资源大延迟高,并不太适合播放简短的提示音,使用SoundPool播放没有问题。

    
SoundPool soundPool;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool= new SoundPool.Builder()
                    .setMaxStreams(10)
                    .build();
        } else {
            soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        }
        findViewById(R.id.button).setOnClickListener(view -> {
            final int sourceid = soundPool.load(this, R.raw.a, 0);
            soundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
                soundPool.play(sourceid, 1, 1, 1, 0, 1);
            });
        });
    }

[url]http://190.lsal.cn/195/1329.gif?0728100424873[/url]
沙发#
发布于:2022-09-21 17:59

以前封装的接口:

public class SoundPoolUtil {
    private static SoundPoolUtil soundPoolUtil;
    private SoundPool soundPool;

    //单例模式
    public static SoundPoolUtil getInstance(Context context) {
        if (soundPoolUtil == null)
            soundPoolUtil = new SoundPoolUtil(context);
        return soundPoolUtil;
    }

    private SoundPoolUtil(Context context) {
        soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
        //加载音频文件
        soundPool.load(context, R.raw.tishi, 1);

    }

    public void play5Times() {
        soundPool.play(1, 1, 1, 0, 4, 0.7f);
    }

    public void play() {
        soundPool.play(1, 1, 1, 0, 0, 1);
    }
}
板凳#
发布于:2022-12-30 17:32
frameworks/av/media$
 
    Fix the problem of playing a short audio
 
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index b1c46df99f..a3f5787237 100755
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -3315,7 +3315,7 @@ bool AudioFlinger::PlaybackThread::threadLoop()
 
                 continue;
             }
-            if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) ||
+            if (/*(!mActiveTracks.size() && systemTime() > mStandbyTimeNs) ||*/
                                    isSuspended()) {
                 // put audio hardware into standby after short delay
                 if (shouldStandby_l()) {

播放简短提示音的问题可以这样修改
[url]http://190.lsal.cn/195/1329.gif?0728100424873[/url]
游客

返回顶部