1

I am writing a Music App, in which i am getting songs from raw folder, and showing in List of available songs, once user click on any of the song, calling another activity namely Play activity, here i am providing another option, like: Play button[to play that specific song] which user has selected via List from MainActivity.java.

Problem:

whenever i do click on Play button after selection of different-different songs from List, it is always playing last song which i have given in PlayActivity.java in this case as you can see gayatri.mp3 is the last song, so it is playing this one everytime, but i want to play that particular one that user has selected.

PlayActivity.java:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        if(mMedia != null){
            mMedia.release(); 
        }

        Intent intent= getIntent();
        // final String MusicID = intent.getStringExtra("vMusicID");
        final String MusicName = intent.getStringExtra("vMusicName");

        final TextView txtView = (TextView)findViewById(R.id.textView1);
        txtView.setText(MusicName);

        mMedia = MediaPlayer.create(this, R.raw.guitar);
        mMedia = MediaPlayer.create(this, R.raw.helo);
        mMedia = MediaPlayer.create(this, R.raw.msg);
        mMedia = MediaPlayer.create(this, R.raw.roja);
        mMedia = MediaPlayer.create(this, R.raw.gayatri);


        final Button btn1 = (Button) findViewById(R.id.button1); // Play

        // Start
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                txtView.setText(MusicName);
                mMedia.start();
                startPlayProgressUpdater();

            }
        });

MainActivity.java:

 MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        /*** Rows 1 ***/
        map = new HashMap<String, String>();
        map.put("MusicID", "1");
        map.put("MusicName", "Gayatri");
        map.put("MusicPath", "gayatri.mp3");
        MyArrList.add(map);

        /*** Rows 2 ***/
        map = new HashMap<String, String>();
        map.put("MusicID", "2");
        map.put("MusicName", "Guitar");
        map.put("MusicPath", "guitar.mp3");
        MyArrList.add(map);

        /*** Rows 3 ***/
        map = new HashMap<String, String>();
        map.put("MusicID", "3");
        map.put("MusicName", "Helo");
        map.put("MusicPath", "helo.mp3");
        MyArrList.add(map);

        /*** Rows 4 ***/
        map = new HashMap<String, String>();
        map.put("MusicID", "4");
        map.put("MusicName", "Msg");
        map.put("MusicPath", "msg.mp3");
        MyArrList.add(map);

        /*** Rows 5 ***/
        map = new HashMap<String, String>();
        map.put("MusicID", "5");
        map.put("MusicName", "Roja");
        map.put("MusicPath", "roja.mp3");
        MyArrList.add(map);

        //  listView1
        final ListView lstView1 = (ListView)findViewById(R.id.listView1);
        lstView1.setAdapter(new ImageAdapter(this));
4

1 回答 1

1

更新:

在这里您需要歌曲的资源 ID,使用以下代码获取 ID,

int roja = this.getResources().getIdentifier("roja", "raw", this.getPackageName()); 
int msg= this.getResources().getIdentifier("msg", "raw", this.getPackageName()); 
int helo= this.getResources().getIdentifier("helo", "raw", this.getPackageName()); 
int gayatri= this.getResources().getIdentifier("gayatri", "raw", this.getPackageName()); 

int[] songIds= {roja,msg,helo,gayatri};

如果您正在使用,List那么您也可能会得到Index,在这里您只需传递indexArray songIds

IE

mMedia = MediaPlayer.create(this,songIds[0]);
mMedia.start();
于 2013-05-28T06:13:11.120 回答