2013-05-28 74 views
1

我寫一個音樂應用程序,在我得到從原始文件夾的歌曲,並在可用歌曲的名單顯示,一旦用戶點擊任何一首歌曲,打電話給另一個活動,即播放活動,這裏我提供了另一個選項,如:播放按鈕[播放該特定歌曲]哪個用戶通過MainActivity.java中的列表選擇了如何播放特定音頻樂曲,而點擊播放按鈕

問題:

每當我做點擊播放按鈕從列表不同,不同的歌曲選擇後,它總是打最後一首歌我在給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

更新時間:

在這裏,你需要你的歌曲資源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

mMedia = MediaPlayer.create(this,songIds[0]); 
mMedia.start(); 
相關問題