0

オーディオを順番に再生するアプリを作成します。チェックボックス付きのリストビューでの選択..アプリは1つのオーディオだけを再生するのにうまく機能しますが、2つ以上のオーディオを選択すると、アプリは最後のサウンドを再生するだけです..ログキャットにエラーはありません..

これが私の主な活動です

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 は、複数のサウンドを 1 つのバイナリに格納する場合に便利だからです。オフセットはファイルの先頭からのオフセットを指定し、長さはファイル内のサウンドの長さを指定します。

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 に答える