2014-09-06 43 views
0

我是一個業餘的android應用程序開發人員,現在正在爲我們的音樂學校開發一個android應用程序。Android媒體播放器和列表視圖 - 印度音樂片

Now I'm in the stage to load a mp3 file in an android Media Player, play or stop each song in the list view when the user clicks play or stop button. 

![enter image description here][1] 


    [1]: http://i.stack.imgur.com/jUbOB.png 

I have stored the mp3 files in res/raw folder. And I have created a class called Song to create data for each of the songs. 

Below is the XML file to fill each item in the list view 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tvViewSample" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgFilmPoster" 
     android:layout_width="75sp" 
     android:layout_height="75sp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> 

    <TextView 
     android:id="@+id/tvSongName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvFilmName" 
     android:layout_below="@+id/tvFilmName" 
     android:text="@string/dummy" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/tvYearReleased" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvSongName" 
     android:layout_below="@+id/tvSongName" 
     android:text="@string/dummy" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/tvPrice" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvYearReleased" 
     android:layout_below="@+id/tvYearReleased" 
     android:text="@string/dummy" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/tvFilmName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="14dp" 
     android:layout_toRightOf="@+id/imgFilmPoster" 
     android:focusable="true" 
     android:text="@string/dummy" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <ImageView 
     android:id="@+id/imgAddCart" 
     android:layout_width="35sp" 
     android:layout_height="35sp" 
     android:contentDescription="@string/add_to_cart" 
     android:layout_alignTop="@+id/imgView" 
     android:layout_toRightOf="@+id/imgView" 
     android:src="@drawable/add_to_cart" 
     android:tag = "@string/add_to_cart" /> 

    <ImageView 
     android:id="@+id/imgView" 
     android:layout_width="35sp" 
     android:layout_height="35sp" 
     android:layout_below="@+id/imgFilmPoster" 
     android:layout_marginTop="14dp" 
     android:layout_toRightOf="@+id/imgPlay" 
     android:src="@drawable/view_icon" /> 

    <ImageView 
     android:id="@+id/imgPlay" 
     android:layout_width="35sp" 
     android:layout_height="35sp" 
     android:layout_alignLeft="@+id/tvPrice" 
     android:layout_alignTop="@+id/imgView" 
     android:contentDescription="@string/play_mp3" 
     android:src="@drawable/play" /> 

</RelativeLayout> 

Below is the XML file to display the list view 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#F8AE9F" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     style="@style/aboutComposer" 
     android:layout_width="wrap_content" 
     android:layout_height="45sp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:gravity="center_vertical|center_horizontal" 
     android:text="@string/msv_songs" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/tvCartTotal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:text="@string/cart_total" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvCartTotal" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="14dp" 
     android:src="@drawable/icon_pay_now" /> 

    <ListView 
     android:id="@+id/lstMSVSongs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="16dp" 
     android:focusable="false" > 

    </ListView> 

</RelativeLayout> 

Song.class文件

package com.srindroid.indianmusicsheets; 

public class Song { 

    private int filmImageID;  
    private String movieName; 
    private String songName; 
    private String yearReleased; 
    private int songPrice; 
    private int sampleSheetMusicID; 
    private int mp3File; 
    private int sheetMusicID; 

    public Song(int filmImageID, String movieName, String songName, 
      String yearReleased, int songPrice, int mp3File) { 
     super(); 
     this.filmImageID = filmImageID; 
     this.movieName = movieName; 
     this.songName = songName; 
     this.yearReleased = yearReleased; 
     this.songPrice = songPrice; 
     this.mp3File = mp3File; 
    } 
    public int getFilmImageID() { 
     return filmImageID; 
    } 
    public String getMovieName() { 
     return movieName; 
    } 
    public String getSongName() { 
     return songName; 
    } 
    public String getYearReleased() { 
     return yearReleased; 
    } 
    public int getSongPrice() { 
     return songPrice; 
    } 
    public int getmp3File() { 
     return mp3File; 
    } 
    public void setFilmImageID(int filmImageID) { 
     this.filmImageID = filmImageID; 
    } 
    public void setMovieName(String movieName) { 
     this.movieName = movieName; 
    } 
    public void setSongName(String songName) { 
     this.songName = songName; 
    } 
    public void setYearReleased(String yearReleased) { 
     this.yearReleased = yearReleased; 
    } 
    public void setSongPrice(int songPrice) { 
     this.songPrice = songPrice; 
    } 
    public void setSampleSheetMusicID(int sampleSheetMusicID) { 
     this.sampleSheetMusicID = sampleSheetMusicID; 
    } 
    public void setMp3File(int mp3File) { 
     this.mp3File = mp3File; 
    } 
    public void setSheetMusicID(int sheetMusicID) { 
     this.sheetMusicID = sheetMusicID; 
    } 



} 

MsvSongs.class文件

package com.srindroid.indianmusicsheets; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.srindroid.indianmusicsheets.R.drawable; 

public class MsvSongs extends Activity { 

    int cart_total; 
    MediaPlayer mp3Player = null; 

    private List<Song> msvSongs = new ArrayList<Song>();  

    @Override 
    protected void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.msv_music_sheets); 
     populateSongsList(); 
     populateSongsListView(); 
    } 

    private void populateSongsList() { 
     msvSongs.add(new Song(R.drawable.ayirathil_oruvan,"Aayirathil Oruvan","Aadho Andha Paravai Pola","1975",250,R.raw.adho_andha_paravai_pola)); 
     msvSongs.add(new Song(R.drawable.server_sundaram,"Server Sundaram","Avalukku Enna Azhagiya Mugam","1964",175,R.raw.avalukkenna_azhagiamugham));  
    } 

    private void populateSongsListView() { 
     ArrayAdapter<Song> msvSongs = new MSVSongsAdapter(); 
     ListView songsList = (ListView) findViewById(R.id.lstMSVSongs); 
     songsList.setAdapter(msvSongs); 

    } 

    private class MSVSongsAdapter extends ArrayAdapter<Song>{ 
     public MSVSongsAdapter(){ 
      super(MsvSongs.this, R.layout.songs_view, msvSongs); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View itemView = convertView; 
      if(itemView == null){ 
       itemView = getLayoutInflater().inflate(R.layout.songs_view, parent, false);   
      } 

      for(int i=0;i<msvSongs.size();i++){ 
       //Find a song to display 
       final Song currentSong = msvSongs.get(position); 

       //Fill the FilmPoster View 
       ImageView filmPoster = (ImageView)itemView.findViewById(R.id.imgFilmPoster); 
       filmPoster.setImageResource(currentSong.getFilmImageID()); 

       //Fill the FilmName View 
       TextView filmName = (TextView)itemView.findViewById(R.id.tvFilmName); 
       filmName.setText(currentSong.getMovieName()); 

       //Fill the SongName View 
       TextView songName = (TextView)itemView.findViewById(R.id.tvSongName); 
       songName.setText(currentSong.getSongName()); 

       //Fill the YearReleased View 
       TextView yearReleased = (TextView)itemView.findViewById(R.id.tvYearReleased); 
       yearReleased.setText(currentSong.getYearReleased()); 

       //Fill the Price View 
       TextView songPrice = (TextView)itemView.findViewById(R.id.tvPrice); 
       songPrice.setText("Rs. "+currentSong.getSongPrice()); 

       //Fill the Play MP3 View 
       final ImageView viewMP3 = (ImageView)itemView.findViewById(R.id.imgPlay);    
       viewMP3.setOnClickListener(new View.OnClickListener() {     


        **@Override 
        public void onClick(View v) { 
         String contentDesc = (String) viewMP3.getContentDescription();      
         if(contentDesc.contains("Play")){       
          mp3Player = MediaPlayer.create(MsvSongs.this, currentSong.getmp3File()); 
          mp3Player.start(); 
          viewMP3.setImageResource(drawable.stop);        
          viewMP3.setContentDescription("Stop MP3"); 
          Toast.makeText(MsvSongs.this, "Playing MP3", Toast.LENGTH_SHORT).show();  
         }else{    
          mp3Player.pause(); 
          mp3Player.stop(); 
          viewMP3.setImageResource(drawable.play);         
          viewMP3.setContentDescription("Play MP3"); 
          Toast.makeText(MsvSongs.this, "Stopping MP3", Toast.LENGTH_SHORT).show(); 
          } 

         } 
       });** 

       //Fill the View Sample View 
       ImageView viewSample = (ImageView)itemView.findViewById(R.id.imgView); 
       viewSample.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         Toast.makeText(MsvSongs.this, R.raw.he_is_a_pirate, Toast.LENGTH_LONG).show();      
        } 
       }); 

       //Fill the Add To Cart View 
       final ImageView viewCart = (ImageView)itemView.findViewById(R.id.imgAddCart);    
       final TextView cartTotal = (TextView) findViewById(R.id.tvCartTotal); 
       viewCart.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) {      
         String contentDesc = (String) viewCart.getContentDescription(); 
         if(contentDesc.contains("Add")){ 
          viewCart.setImageResource(drawable.remove_from_cart); 
          viewCart.setContentDescription("Remove From Cart"); 
          cart_total=cart_total+(currentSong.getSongPrice()); 
          cartTotal.setText("Your cart total is Rs. "+cart_total); 
         }else{ 
          viewCart.setImageResource(drawable.add_to_cart); 
          viewCart.setContentDescription("Add To Cart"); 
          cart_total=cart_total-(currentSong.getSongPrice()); 
          cartTotal.setText("Your cart total is Rs. "+cart_total); 
         } 

        } 
       }); 

      }   
      return itemView;    
     }  

    } 
} 

現在我面對上面的代碼的問題是,當用戶點擊的播放按鈕第一首歌,媒體播放器按預期播放。當用戶點擊第二首歌曲的播放按鈕時,媒體播放器播放它而不停止第一首歌曲。我使用mp3Player.isPlaying(),即使歌曲實際播放時也總是返回FALSE。

你能幫我解決嗎?

下面是我寫的播放或列表視圖

最終ImageView的viewMP3 =(ImageView的)itemView.findViewById(R.id.imgPlay)停止每首歌的代碼;
viewMP3.setOnClickListener(新View.OnClickListener(){

   **@Override 
       public void onClick(View v) { 
        String contentDesc = (String) viewMP3.getContentDescription();      
        if(contentDesc.contains("Play")){       
         mp3Player = MediaPlayer.create(MsvSongs.this, currentSong.getmp3File()); 
         mp3Player.start(); 
         viewMP3.setImageResource(drawable.stop);        
         viewMP3.setContentDescription("Stop MP3"); 
         Toast.makeText(MsvSongs.this, "Playing MP3", Toast.LENGTH_SHORT).show();  
        }else{    
         mp3Player.pause(); 
         mp3Player.stop(); 
         viewMP3.setImageResource(drawable.play);         
         viewMP3.setContentDescription("Play MP3"); 
         Toast.makeText(MsvSongs.this, "Stopping MP3", Toast.LENGTH_SHORT).show(); 
         } 

        } 
      });* 

回答

0

對不起,我沒有你使用mp3player.isPlaying()在你的代碼的任何地方。你刪除它看到了什麼?

似乎。就像你正在測試相關的ImageView歌曲能否播放或不是你不檢查另一首歌曲無論是打還是不當你創建的MediaPlayer

嘗試是:

if(contentDesc.contains("Play")){       
    if (mp3Player != null && mp3Player.isPlaying()) { 
     mp3Player.pause(); 
     mp3Player.stop(); 
     mp3Player.release(); 
    } 
    mp3Player = MediaPlayer.create(MsvSongs.this, currentSong.getmp3File()); 
    mp3Player.start(); 
    ... 
}else{    
    mp3Player.pause(); 
    mp3Player.stop(); 
    mp3Player.release(); 
} 

請不要忘記在製作新版本之前先釋放您的MediaPlayer。

+0

如何更改圖像以便來回啓動和暫停? – 2015-09-16 23:59:53