Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Finally i got the solution. Here is the code</p> <pre><code>import java.util.ArrayList; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class Tracks extends Activity implements OnItemClickListener { ListView lv; ArrayList&lt;String&gt; songs; ArrayAdapter&lt;String&gt; adapter; AudioManager am; MediaPlayer mp; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tracks); lv=(ListView)findViewById(R.id.Tracks); am=(AudioManager)getSystemService(Context.AUDIO_SERVICE); loadMusic(); adapter=new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1,songs); lv.setAdapter(adapter); lv.setOnItemClickListener(this); } @SuppressWarnings("deprecation") private void loadMusic() { Uri uri=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String projection[]= {android.provider.MediaStore.Audio.Media.DATA,android.provider.MediaStore.Audio.Media.TITLE}; cursor=this.managedQuery(uri, projection, null, null, null); songs=new ArrayList&lt;String&gt;(); while (cursor.moveToNext()) { String duration=android.provider.MediaStore.Audio.Media.DURATION; songs.add(cursor.getString(1)+"\t"+duration); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.tracks, menu); return true; } @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position, long id) { cursor.moveToPosition(position); //Toast.makeText(getBaseContext(), songs.get(position), Toast.LENGTH_SHORT).show(); int id1=cursor.getColumnIndex(MediaStore.Audio.Media.DATA); //Uri filename=ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); mp=new MediaPlayer(); //mp.reset(); try { mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDataSource(cursor.getString(id1)); mp.prepare(); mp.start(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mp.release(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload