我制作了一个显示歌曲列表的简单应用,并计划在点击时让每个列表项播放歌曲。然而,我已经把一切都搞定了,音乐不会开始播放。如何使用MediaPlayer播放ContentResolver中的歌曲?我已经设置了一个示例代码供您查看。如果可能的话,有一些样品或例子是很荣幸的。
public class MainActivity extends ActionBarActivity {
List tracks;
String[] ColId;
List<Track>tack;
private MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
tracks = Track.getItems(this);
ColId = Track.COLUMNS;
ListView trackList = (ListView)findViewById(R.id.listView);
ListTrackAdapter adapter = new ListTrackAdapter(this, tracks);
trackList.setAdapter(adapter);
trackList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//long //id = 0;/* retrieve it from somewhere */;
Uri contentUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, position);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(MainActivity.this, contentUri);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
String pos = String.valueOf(tracks.get(position));
Toast.makeText(MainActivity.this,position+" = "+ColId[0],Toast.LENGTH_SHORT).show();
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.release();
mMediaPlayer.stop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.release();
mMediaPlayer.stop();
}
}
}
Track的课程
public class Track {
public long id;
public long albumId;
public long artistId;
public String path;
public String title;
public String album;
public String artist;
public Uri uri; // URI
public long duration;
public int trackNo;
public static final String[] COLUMNS = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.TRACK,
};
public Track(Cursor cursor)
{
id = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media._ID ));
path = cursor.getString( cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
title = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.TITLE ));
album = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.ALBUM ));
artist = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST ));
albumId = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.ALBUM_ID ));
artistId = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST_ID ));
duration = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.DURATION ));
trackNo = cursor.getInt( cursor.getColumnIndex( MediaStore.Audio.Media.TRACK ));
uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
}
public static List getItems(Context activity) {
List tracks = new ArrayList();
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
Track.COLUMNS,
null,
null,
null
);
while( cursor.moveToNext() ){
if( cursor.getLong(cursor.getColumnIndex( MediaStore.Audio.Media.DURATION)) < 3000 ){continue;}
tracks.add(new Track(cursor));
}
cursor.close();
return tracks;
}
}
答案 0 :(得分:0)
试试这个,
$r = mysql_fetch_assoc($result);
$myvar = $r['something'];
替换为
mMediaPlayer.setDataSource(MainActivity.this, contentUri);
然后它运作正常。