0

我构建了一个应用程序,它将按顺序播放音频。带有复选框的列表视图中的选择.. 该应用程序可以很好地播放一个音频,但是当我选择 2 个或更多音频时,该应用程序只会播放最后一个声音.. 我的 logcat 中没有错误..

这是我的主要活动

public class MainActivity extends ListActivity {

private String[] lv_arr = {};
private ListView mainListView = null;
private final String SETTING_TODOLIST = "todolist";
private ArrayList<String> selectedItems = new ArrayList<String>();
private ArrayList<String> nameItems = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnSave = (Button)findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            playSound();
        }
    });

    Button btnClear = (Button)findViewById(R.id.btnClear);
    btnClear.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    ArrayList<String> listTODO = PrepareListFromXml();
    mainListView = getListView();
    mainListView.setCacheColorHint(0);

    // bind data with list
    lv_arr = (String[])listTODO.toArray(new String[0]);
    mainListView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, lv_arr));
    mainListView.setItemsCanFocus(false);
    mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    LoadSelections();
}

private void playSound(){
    String nameChoosed = "";

    int count = mainListView.getAdapter().getCount();

    for (int i=0; i<count; i++){

        if(mainListView.isItemChecked(i)){
            if(nameChoosed.length()>0){
                nameChoosed +=","+mainListView.getItemAtPosition(i);
            } else {
                nameChoosed += mainListView.getItemAtPosition(i);
            }
        }

        //Toast.makeText(MainActivity.this, nameChoosed, Toast.LENGTH_SHORT).show();
    }
    Toast.makeText(MainActivity.this, nameChoosed, Toast.LENGTH_SHORT).show();
    nameItems.addAll(Arrays.asList(nameChoosed.split(",")));
    System.out.println("filenameitems = "+nameItems);
    playing(MainActivity.this, nameItems);
}

private void playing(Context context, ArrayList<String> list){
    MediaPlayer mp = new MediaPlayer();
    CxMediaPlayer cx = new CxMediaPlayer(MainActivity.this);

    SoundManager.getInstance();
    SoundManager.initSounds(MainActivity.this);


    for (int i=0; i<list.size(); i++){

        String file = list.get(i);
        Log.d("TAG", list.get(i));
        //cx.play(file);
        SoundManager.getInstance();
        SoundManager.initSounds(MainActivity.this);
        SoundManager.addSound(i, list.get(i));
        SoundManager.playSound(i, 1f);

    }

}

private void LoadSelections() {
    // if the selections were previously saved load them

    SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);

    if (settingsActivity.contains(SETTING_TODOLIST)) {
        String savedItems = settingsActivity
                .getString(SETTING_TODOLIST, "");

        this.selectedItems.addAll(Arrays.asList(savedItems.split(",")));
        int count = this.mainListView.getAdapter().getCount();

        for (int i = 0; i < count; i++) {
            String currentItem = (String) this.mainListView.getAdapter()
                    .getItem(i);
            if (this.selectedItems.contains(currentItem)) {
                this.mainListView.setItemChecked(i, true);
            }

        }

    }
}

private ArrayList<String> PrepareListFromXml() {

    ArrayList<String> todoItems = new ArrayList<String>();
    XmlResourceParser todolistXml = getResources().getXml(R.xml.todolist);

    int eventType = -1;
    while (eventType != XmlResourceParser.END_DOCUMENT) {
        if (eventType == XmlResourceParser.START_TAG) {

            String strNode = todolistXml.getName();
            if (strNode.equals("item")) {
                todoItems.add(todolistXml.getAttributeValue(null, "title"));
            }
        }

        try {
            eventType = todolistXml.next();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return todoItems;
}

}

这在我的 SoundManager 中

static private SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap<Integer, Integer> mSoundPoolMap;
private static AudioManager  mAudioManager;
private static Context mContext;
private static AssetFileDescriptor afd;
private static AssetManager am;

static float streamVolume = 0;
static int index1 = 0;
static float speed1 = 0;

private SoundManager()
{
}

/**
 * Requests the instance of the Sound Manager and creates it
 * if it does not exist.
 *
 * @return Returns the single instance of the SoundManager
 */
static synchronized public SoundManager getInstance()
{
    if (_instance == null)
      _instance = new SoundManager();
    return _instance;
 }

/**
 * Initialises the storage for the sounds
 *
 * @param theContext The Application context
 */
public static  void initSounds(Context theContext)
{
         mContext = theContext;
     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
     mSoundPoolMap = new HashMap<Integer, Integer>();
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
} 

/**
 * Add a new Sound to the SoundPool
 *
 * @param Index - The Sound Index for Retrieval
 * @param SoundID - The Android ID for the Sound asset.
 */
public static void addSound(int Index,String SoundID)
{
      // mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
    try {
        am = mContext.getAssets();
        afd = am.openFd(SoundID);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mSoundPoolMap.put(Index, mSoundPool.load(afd, 1));
}

/**
 * Loads the various sound assets
 * Currently hardcoded but could easily be changed to be flexible.
 */
public static void loadSounds()
{
       // mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.starwars, 1));
        //mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.terminator, 1));
}

/**
 * Plays a Sound
 *
 * @param index - The Index of the Sound to be played
 * @param speed - The Speed to play not, not currently used but included for compatibility
 */
public static void playSound(int index,float speed)
{
    index1 = index;
    speed1 = speed;
    streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);         
    mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
            // TODO Auto-generated method stub
            mSoundPool.play(mSoundPoolMap.get(index1), streamVolume, streamVolume, 1, 0, speed1);
        }
    });

}

/**
 * Stop a Sound
 * @param index - index of the sound to be stopped
 */
public static void stopSound(int index)
{
        mSoundPool.stop(mSoundPoolMap.get(index));
}

/**
 * Deallocates the resources and Instance of SoundManager
 */
public static void cleanup()
{
        mSoundPool.release();
        mSoundPool = null;
    mSoundPoolMap.clear();
    mAudioManager.unloadSoundEffects();
    _instance = null;

}

我非常需要修复这个存根的帮助..我非常感谢帮助我的人..对不起我的英语不好

4

1 回答 1

0

您需要使用 load (FileDescriptor fd, long offset, long length, int priority),因为如果您将多个声音存储在单个二进制文件中,此版本的 load 很有用。偏移量指定从文件开头的偏移量,长度指定文件中声音的长度。

AssetFileDescriptor descriptor = manager.openFd(fileName);
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
player.setDataSource(descriptor.getFileDescriptor(), start,end,priority);
于 2014-04-02T12:30:17.293 回答