Android Listview。如何播放所有音乐和选择歌曲

时间:2013-02-12 12:49:30

标签: android media-player

此代码加载ListView中的所有音乐,播放我选择的歌曲,我需要播放列表中的所有音乐,但当我使用whilefor时,我无法选择另一首歌曲在玩耍时。

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
  ListView musiclist;
  Cursor musiccursor;
  int music_column_index;
  int count;
  MediaPlayer mMediaPlayer;
MediaController mc;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_phone_music_grid();

  }

  @SuppressWarnings("deprecation")
private void init_phone_music_grid() {
        System.gc();
        String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
        musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
        count = musiccursor.getCount();
        musiclist = (ListView) findViewById(R.id.listView1);
        musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

        musiclist.setOnItemClickListener(musicgridlistener);

        mMediaPlayer = new MediaPlayer();


  }

  private OnItemClickListener musicgridlistener = new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
long id) {

            System.gc();
              music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
              musiccursor.moveToPosition(position);

              String filename = musiccursor.getString(music_column_index);

              try {
                    if (mMediaPlayer.isPlaying()) {
                          mMediaPlayer.reset();
                    }
                    mMediaPlayer.setDataSource(filename);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
              } catch (Exception e) {

              }



             //

        }

  };

  private OnItemClickListener musicgridlistener1 = new OnItemClickListener() {
      public void onItemClick(AdapterView parent, View v, int position,
long id) {

        System.gc();
            music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            musiccursor.moveToPosition(position);

            String filename = musiccursor.getString(music_column_index);

            try {
                  if (mMediaPlayer.isPlaying()) {
                        mMediaPlayer.reset();
                  }
                  mMediaPlayer.setDataSource(filename);
                  mMediaPlayer.prepare();
                  mMediaPlayer.start();
            } catch (Exception e) {

            }
       try{
            while(mMediaPlayer.isPlaying()){}

      } catch (Exception e)
      {
          //handle the exception 
          e.printStackTrace();
          }
          while(musiccursor.getPosition()!= musiccursor.getCount()){
               mMediaPlayer.reset();
             musiccursor.moveToNext();

               filename = musiccursor.getString(music_column_index);

              try {
                    if (mMediaPlayer.isPlaying()) {
                          mMediaPlayer.reset();
                    }
                    mMediaPlayer.setDataSource(filename);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();

              } catch (Exception e) {

              }
              while(mMediaPlayer.isPlaying()){}
          }

           //
      }

};

  public class MusicAdapter extends BaseAdapter {
        private Context mContext;

        public MusicAdapter(Context c) {
              mContext = c;
        }

        public int getCount() {
              return count;
        }

        public Object getItem(int position) {
              return position;
        }

        public long getItemId(int position) {
              return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            System.gc();
            String id = null;
            TextView tv;
            if (convertView == null) {
            tv = new TextView(mContext.getApplicationContext());
            } else{
            tv = (TextView) convertView;
            }

            musiccursor.moveToPosition(position);

            music_column_index = musiccursor
            .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            id = musiccursor.getString(music_column_index);
            music_column_index =          musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            id += " Size(KB):" + musiccursor.getString(music_column_index);
            tv.setText(id);
            return tv;
            }
  }
}

2 个答案:

答案 0 :(得分:1)

您应该构建播放列表队列,而不是循环浏览数据。然后,您可以根据需要填充和减少它,并且不必担心数据源的更改。

答案 1 :(得分:-1)

导入默认音乐播放器:

要简单地启动音乐播放器,请执行以下操作:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

请注意,这仅适用于SDK API级别8级