我在Android中访问sdcard中的歌曲,歌曲显示在ListView中,但我无法播放这些文件。
这是代码:
package com.ex.imageGallery;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.media.MediaPlayer;
import android.net.Uri;
public class temp extends Activity{
protected static final String TAG = null;
private Cursor audioCursor;
private int video_column_index;
private static String currentposition;
ListView videolist;
int count;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_audio);
//init_phone_video_grid();
System.gc();
final String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.SIZE };
audioCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
count = audioCursor.getCount();
videolist = (ListView) findViewById(R.id.PhoneVideoList);
videolist.setAdapter(new AudioAdapter(getApplicationContext()));
videolist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//System.out.println(proj[0]);
System.out.println(audioCursor.getString(position));
//Uri mur = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI+Uri.parse(audioCursor.getString(position));
try{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(audioCursor.getString(position));
mp.prepare();
mp.start();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
}
class AudioAdapter extends BaseAdapter {
private Context vContext;
public AudioAdapter(Context c) {
vContext = 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(vContext.getApplicationContext());
String id = null;
if (convertView == null) {
video_column_index = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
audioCursor.moveToPosition(position);
id = audioCursor.getString(video_column_index);
video_column_index = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
audioCursor.moveToPosition(position);
id += "Size(KB):" + audioCursor.getString(video_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
答案 0 :(得分:2)
答案已经被接受,但这正是你真正想要的:
http://androidsamples.blogspot.com/2009/06/displaying-list-of-music-files-stored.html
答案 1 :(得分:0)
答案 2 :(得分:0)
将此类添加到您的代码中......
public class SongsManager {
// SDCard Path
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().toString());
private final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public SongsManager() {
}
public SongsManager(String foldername) {
MEDIA_PATH = MEDIA_PATH + foldername +"/";
}
/**
* Function to read all mp3 files from sdcard and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put(
"songTitle",
file.getName().substring(0,
(file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
创建此类的实例并将其读作..
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
播放这样的歌......
public void playSong(int songIndex){
// Play song
try {
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}