从列表中选择歌曲,在项目点击监听器上导航到媒体播放器并使用媒体播放器播放歌曲

时间:2015-04-08 10:00:16

标签: android

public class MarathiActivity extends Activity 
{
    ListView list;

    String[] web = {
            "song name 1 ",
            "song name 2",
            "song name 3",
            "song name 4",
            "song name 5",
            "song name 6",
            "song name 7",
            "song name 8"

    } ;

    Integer[] imageId = {
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher
    };

    public  int[] resID = {
            R.raw.song1,
            R.raw.song2,
            R.raw.song3,
            R.raw.song4,
            R.raw.song5,
            R.raw.song6,
            R.raw.song7,
            R.raw.song8
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marathi);
        CustomList adapter = new
                CustomList(MarathiActivity.this, web);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i = new Intent(MarathiActivity.this,MusicPlayer.class);
                startActivity(i);
               // playSong(position);

            }
        });

其马拉地语歌曲列表,

public class MusicPlayer extends Activity
{
    private MediaPlayer mediaPlayer;
    public TextView songName, duration;
    private double timeElapsed = 0, finalTime = 0;
    private int forwardTime = 2000, backwardTime = 2000;
    private Handler durationHandler = new Handler();
    private SeekBar seekbar;
    MarathiActivity ma;
    int position0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set the layout of the Activity
        setContentView(R.layout.musicplayer);

        //initialize views
        initializeViews();
    }

    public void initializeViews(){
        songName = (TextView) findViewById(R.id.songName);

        mediaPlayer = MediaPlayer.create(this,R.raw.songname);
        finalTime = mediaPlayer.getDuration();
        duration = (TextView) findViewById(R.id.songDuration);
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        songName.setText("Sample_Song.mp3");

        seekbar.setMax((int) finalTime);
        seekbar.setClickable(false);
    }

    // play mp3 song
    public void play(View view) {
        mediaPlayer.start();
        timeElapsed = mediaPlayer.getCurrentPosition();
        seekbar.setProgress((int) timeElapsed);
        durationHandler.postDelayed(updateSeekBarTime, 100);
    }

    //handler to change seekBarTime
    private Runnable updateSeekBarTime = new Runnable() {
        public void run() {
            //get current position
            timeElapsed = mediaPlayer.getCurrentPosition();
            //set seek bar progress
            seekbar.setProgress((int) timeElapsed);
            //set time remaining
            double timeRemaining = finalTime - timeElapsed;
            duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

            //repeat yourself that again in 100 milliseconds
            durationHandler.postDelayed(this, 100);
        }
    };

    // pause mp3 song
    public void pause(View view) {
        mediaPlayer.pause();
    }

  /*  // go forward at forwardTime seconds
    public void forward(View view) {
        //check if we can go forward at forwardTime seconds before song endes
        if ((timeElapsed + forwardTime)  0) {
            timeElapsed = timeElapsed - backwardTime;

            //seek to the exact second of the track
            mediaPlayer.seekTo((int) timeElapsed);
        }
    }*/

}

我想创建媒体播放器并在媒体播放器中的项目点击监听器上播放所选择的歌曲,我在音乐播放器活动中遇到麻烦,我如何将所选歌曲名称映射到我必须以原始方式保存的歌曲文件夹??

2 个答案:

答案 0 :(得分:0)

我真的不明白,你想要什么,但试着这样做:

public class MySongs {
public String name;
public int imageId;
public int songRawId;

public MySongs(String name, int imageId, int songRawId) {
    this.name = name;
    this.imageId = imageId;
    this.songRawId = songRawId;
}    

}

然后你可以使用你的歌曲列表:

List<MySongs> songs = new ArrayList<MySongs>();

在onCreate中,您可以添加您想要的所有歌曲:

songs.add(new MySongs("name1", R.mipmap.ic_launcher, R.raw.song1));
songs.add(new MySongs("name2", R.mipmap.ic_launcher, R.raw.song2));
...

之后,您可以将此列表放入listAdapter中。在OnItemClick中只需执行MySongs selectedSong = songs.get(position);

然后您将获得有关所选歌曲的所有信息。您可以使用selectedSong.name作为示例来获取它们

答案 1 :(得分:0)

请修改您的代码,如下面的代码

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i = new Intent(MarathiActivity.this,MusicPlayer.class);
                i.putExtra("SongName", web[position]);
                i.putExtra("Song", resID[position]);
                startActivity(i);
               // playSong(position);

            }
        });

MusicPlayer.class

    String m_songName;
    int m_songRes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set the layout of the Activity
        setContentView(R.layout.musicplayer);


        if(getIntent().getExtras()!= null)
        {
            m_songName = getIntent().getExtras().getString("SongName");
            m_songRes = getIntent().getExtras().getInt("Song");
        }

        //initialize views
        initializeViews();
    }

    public void initializeViews(){
        songName = (TextView) findViewById(R.id.songName);

        mediaPlayer = MediaPlayer.create(this,m_songRes);
        finalTime = mediaPlayer.getDuration();
        duration = (TextView) findViewById(R.id.songDuration);
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        songName.setText(m_songName);

        seekbar.setMax((int) finalTime);
        seekbar.setClickable(false);
    }