如何在单击“播放”按钮的同时播放特定的音频歌曲

时间:2013-05-28 06:06:03

标签: android android-audiomanager

我正在写一个音乐应用,其中我从原始文件夹获取歌曲,并在可用歌曲列表中显示,用户点击任何一首歌,拨打另一个活动即播放活动,在这里我提供另一个选项,例如:播放按钮[播放该特定歌曲]用户有哪些从MainActivity.java中选择通过列表。

问题:

每当我从List中选择不同的歌曲后点击“播放”按钮时,它总是播放我在 PlayActivity.java 中给出的最后一首歌,因为你可以看到 gayatri.mp3 是最后一首歌,所以每次播放这首歌,但我想播放用户选择的那首歌。

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));

1 个答案:

答案 0 :(得分: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, 您只需将index传递给Array songIds

即可

<强>即

mMedia = MediaPlayer.create(this,songIds[0]);
mMedia.start();