2013-10-25 88 views
0

我构建的应用程序将按顺序播放音频。在列表视图的选择与复选框.. 该应用程序适用于播放只是一个音频,但是当我选择2个或更多的音频应用程序只播放最后的声音..没有错误在我的logcat ..从资产顺序播放音频 - Android

this是我的主要activiy

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; 

} 

我很需要帮助修复该存根。我非常感谢1瓦特ho帮助我..对不起我的英文不好

回答

0

因为如果您在一个二进制文件中存储多个声音,您需要使用加载(FileDescriptor fd,long offset,long length,int priority)。偏移量指定从文件起始处的偏移量,长度指定文件中声音的长度。

AssetFileDescriptor descriptor = manager.openFd(fileName); 
long start = descriptor.getStartOffset(); 
long end = descriptor.getLength(); 
player.setDataSource(descriptor.getFileDescriptor(), start,end,priority);