Android音频-声音池替代 [英] android audio - soundpool alternatives

查看:164
本文介绍了Android音频-声音池替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢Android Soundpool类,因为它简单易用,并且可以与我在应用程序中使用的标准音频文件很好地配合使用.现在,我想使用户可以通过在SD卡上指定音频文件来指定某些声音.不幸的是,我遇到了Soundpool的限制,当声音文件太大时,我会得到

I like the Android Soundpool class for its simplicity and it works well with the standard audio files I am using in my app. Now I want to make it possible for the user to specify certains sounds by specifying audio files on the sd card. Unfortunately I run into limitations of Soundpool, when the sound file is too big i get a

AudioFlinger无法创建曲目.状态:-12

AudioFlinger could not create track. status: -12

响应.似乎我必须切换到MediaPlayer,然后再再次进入MediaPlayer的复杂性之前,我想问一下是否有可用于android的音频库

response. It seems I have to switch to MediaPlayer yet before getting into the complexity of MediaPlayer again I wanted to ask if there is an audio library available for android which

  • Soundpool具有播放各种声音的简便性
  • Soundpool在文件大小方面没有局限性.

非常感谢您.

马丁

推荐答案

现在,我想到了一个非常简单的AudioPool类,该类随后随MediaPlayer类播放添加到其中的音频.这种实现方式肯定还不成熟,但我只是想分享一下它,因为它至少让您知道了如何轻松实现这一点.如果您发现此类课程有任何问题,请告诉我们.

For now I came up with a very simple AudioPool class which plays audio added to it subsequently with the MediaPlayer class. This implementation is for sure not mature yet I just thought to share it as it at least gives some idea how this can be approached easily. If you see any problems with this class please let us know.

用法:

 AudioPool ap = new AudioPool();

 File root = Environment.getExternalStorageDirectory() ;

 int id1 = ap.addAudio(root + "/gong1.mp3");
 int id2 = ap.addAudio(root + "/gong2.mp3");
 int id3 = ap.addAudio(root + "/gong3.mp3"); 

 ap.playAudio(id1);
 ap.playAudio(id3);
 ap.playAudio(id3);
 ap.playAudio(id2);

随后将播放锣1->锣3->锣3->锣1.因为这基本上就是我所需要的,所以我把它留在这里...

which will play gong1 -> gong3 -> gong3 -> gong1 subsequently. As this is basically what I need I leave it here ...

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;

public class AudioPool {

static String TAG = "AudioPool";

MediaPlayer mPlayer;

int mAudioCounter;

int mCurrentId;

HashMap<Integer, String> mAudioMap;

LinkedList<Integer> mAudioQueue;

public AudioPool() {

    mAudioMap = new HashMap<Integer, String>();
    mAudioQueue = new LinkedList<Integer>();
    mAudioCounter = 0;

}

public int addAudio(String path) {
    Log.d(TAG, "adding audio " + path + " to the pool");

    if (mAudioMap.containsValue(path)) {
        return getAudioKey(path);
    }
    mAudioCounter++;
    mAudioMap.put(mAudioCounter, path);
    return mAudioCounter;
}

public boolean playAudio(int id) {

    if (mAudioMap.containsKey(id) == false) {
        return false;
    }

    if (mPlayer == null) {
        setupPlayer();
    }

    if (mPlayer.isPlaying() == false) {
        return prepareAndPlayAudioNow(id);
    } else {
        Log.d(TAG, "adding audio " + id + " to the audio queue");

        mAudioQueue.add(id);
    }
    return true;
}

public Integer[] getAudioIds() {
    return (Integer[]) mAudioMap.keySet().toArray(
            new Integer[mAudioMap.keySet().size()]);
}

public void releaseAudioPlayer() {
    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}


private boolean prepareAndPlayAudioNow(int id) {
    mCurrentId = id;
    try {
        Log.d(TAG, "playing audio " + id + " now");
        mPlayer.reset();
        mPlayer.setDataSource(mAudioMap.get(id));
        mPlayer.prepare();
        mPlayer.start();
        return true;
    } catch (Exception e) {
        Log.d(TAG, "problems playing audio " + e.getMessage());
        return false;
    }
}

private boolean playAudioAgainNow() {
    try {
        mPlayer.seekTo(0);
        mPlayer.start();
        return true;
    } catch (Exception e) {
        Log.d(TAG, "problems playing audio");
        return false;
    }
}

private void setupPlayer() {
    mPlayer = new MediaPlayer();
    mPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            audioDone();
        }
    });
}

private void audioDone() {

    if (mAudioQueue.size() > 0) {
        Log.d(TAG, mAudioQueue.size() + " audios in queue");
        int nextId = mAudioQueue.removeFirst();

        if (mCurrentId == nextId) {
            playAudioAgainNow();
        } else {
            prepareAndPlayAudioNow(nextId);
        }

    } else {
        releaseAudioPlayer();
    }
}

private int getAudioKey(String path) {
    for (Map.Entry<Integer, String> map : mAudioMap.entrySet()) {
        if (map.getValue().compareTo(path) == 0) {
            return map.getKey();
        }
    }
    return -1;
}

}

这篇关于Android音频-声音池替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆