【问题标题】:Android Media Player and List View - Indian Music SheetsAndroid 媒体播放器和列表视图 - 印度乐谱
【发布时间】:2014-09-06 07:08:24
【问题描述】:

我是一名业余 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(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();
                        }

                    }
            });*

【问题讨论】:

    标签: android android-listview android-mediaplayer


    【解决方案1】:

    抱歉,我没有看到您在代码中的任何地方使用 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。

    【讨论】:

    • 来回改变开始和暂停的图像怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多