如何在android中的listview中显示音乐文件

时间:2013-07-04 04:27:13

标签: android android-emulator

您好我需要在listview中显示音乐文件数组,当我点击该音乐文件时我需要播放该歌曲,我尝试使用下面的代码在listview中显示歌曲,但它在向textview添加数组时显示空指针异常行,但同样的代码工作在列表视图中显示图像,我错了任何人建议我..

public class CustomListViewExample extends Activity {
    Integer[] text;
    public static ArrayList<Integer> list1 = new ArrayList<Integer>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.citylist);
        list1.add(R.raw.apple);
        list1.add(R.raw.intro_letter_report_card);
        list1.add(R.raw.intro_title_page_1);

        text = list1.toArray(new Integer[list1.size()]);

        ListView l1 = (ListView) findViewById(R.id.ListView01);
        l1.setAdapter(new MyCustomAdapter(text));
    }

    class MyCustomAdapter extends BaseAdapter {
        Integer[] data_image;

        MyCustomAdapter() {
            data_image = null;
        }

        MyCustomAdapter(Integer[] text) {
            data_image = text;
        }

        @Override
        public int getCount() {
            return data_image.length;
        }

        @Override
        public String getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row;

            row = inflater.inflate(R.layout.city_row_item, parent, false);

            TextView t1=(TextView)row.findViewById(R.id.textView1);

            t1.setText(""+data_image[position]);
            return (row);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

使用以下代码在ListView中显示音频文件并单击其中任何一个,播放该歌曲

public class AudioListActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audiolist_activity);
    init_phone_music_grid();
}
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.PhoneMusicList);
    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) {

        }
    }
};

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();
        TextView tv = new TextView(mContext.getApplicationContext());
        String id = null;
        if (convertView == null) {
            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);
            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            musiccursor.moveToPosition(position);
            id += " Size(KB):" + musiccursor.getString(music_column_index);
            tv.setText(id);
        } else
            tv = (TextView) convertView;
        return tv;
    }
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    mMediaPlayer.stop();
}
}

答案 1 :(得分:0)

首先使用声音池将声音加载到内存中。这就是我在我的应用Beat Shaker中执行此操作的方式:

// Sound pool new instance
spool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
// Sound pool on load complete listener
spool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        Log.i("OnLoadCompleteListener","Sound "+sampleId+" loaded.");
        loaded = true;
    }
});       
// Load the sample IDs
soundId = new int[3];
soundId[0] = spool.load(this, R.raw.clave, 1);
soundId[1] = spool.load(this, R.raw.maracas, 1);
soundId[2] = spool.load(this, R.raw.crash, 1);

然后,您可以从相应的列表选择侦听器调用一个线程,该侦听器运行与此类似的函数:

public void Sound1(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    int streamID = -1;
    do{
        streamID = spool.play(soundId[0], volume, volume, 1, 0, 1f);
    } while(streamID==0);
};

答案 2 :(得分:0)

对于NPE,似乎textView1中没有R.layout.city_row_item视图。

关于显示音乐文件列表并播放它们,您可以参考this exemple

另外,我相信您需要参考this video from 2010 IO为ListViews创建更好的适配器(因为在您的代码中您不使用convertView项目。)