1

我已经检索了手机中存在的歌曲,但我无法播放歌曲。请帮我。我使用 setOnDataSource 但我无法得到它。在文档中它显示了 setDataSource() 的用法。请提出任何更正以使代码非常完美这是代码: package com.example.testmedia;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
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;
import android.widget.Toast;

public class Tracks_media extends Activity implements OnItemClickListener   {




MediaPlayer mediaplayer;
String Displayname="",id="";
Cursor cursor;
List<String> musiclist=new ArrayList<String>();
ListView tracklist;
ArrayAdapter<String> itemusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracks_media);
    tracklist=(ListView)findViewById(R.id.TrackList);
    itemusic=new ArrayAdapter<String>(this,      
            android.R.layout.simple_list_item_1, musiclist);
    mediaplayer=new MediaPlayer();
    tracklist.setAdapter(itemusic);
    Loadmusic();
    tracklist.setOnItemClickListener(this);
}

//Retrive all songs
private void Loadmusic() {
    // TODO Auto-generated method stub
    ContentResolver cr=getContentResolver();
    Uri uri=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    cursor=cr.query(uri, null, null, null, null);
    if(cursor==null)
    {
        Toast.makeText(this, "No media Files present",         
             Toast.LENGTH_SHORT).show();



    }
    while (cursor.moveToNext())
    {


Displayname=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));

id=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
        itemusic.add(id+"\t"+Displayname+"");
    }   


    cursor.close();

}



@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_media, menu);
    return true;
}



@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    // TODO Auto-generated method stub
    mediaplayer.reset();
    try {
        mediaplayer.setDataSource(musiclist.get(position));
        mediaplayer.prepare();
        mediaplayer.start();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


}

我尝试了很多方法,但无法做到正确。请告诉我如何播放检索的媒体。

4

3 回答 3

2

Finally i got the solution. Here is the 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<String> songs;
     ArrayAdapter<String> 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<String>(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<String>();
        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<?> 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();
    }

}
于 2013-09-25T15:28:01.037 回答
1

尝试添加:

cursor=cr.query(uri, new String[] {"*"}, null, null, null);

并尝试创建新实例MediaPlayer而不是mediaPlayer.reset();

如果这不起作用,请参阅此

于 2013-09-22T15:00:24.587 回答
1

试试这个编辑过的代码

public class bcd extends Activity implements OnItemClickListener   {




MediaPlayer mediaplayer;
String Displayname="",id="";
Cursor cursor;
List<String> musiclist=new ArrayList<String>();
ListView tracklist;
ArrayAdapter<String> itemusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracks_media);
tracklist=(ListView)findViewById(R.id.TrackList);


mediaplayer=new MediaPlayer();

Loadmusic();
tracklist.setOnItemClickListener(this);
}

//Retrive all songs
private void Loadmusic() {
// TODO Auto-generated method stub
ContentResolver cr=getContentResolver();
Uri uri=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor=cr.query(uri, null, null, null, null);
if(cursor==null)
{
    Toast.makeText(this, "No media Files present",         
         Toast.LENGTH_SHORT).show();



}
while (cursor.moveToNext())
{


Displayname=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));

id=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
    musiclist.add(id+"\t"+Displayname+"");
}   


cursor.close();
itemusic=new ArrayAdapter<String>(this,      
        android.R.layout.simple_list_item_1, musiclist);
tracklist.setAdapter(itemusic);
}



@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_media, menu);
return true;
}



@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
mediaplayer.reset();
try {
    mediaplayer.setDataSource(musiclist.get(position));
    mediaplayer.prepare();
    mediaplayer.start();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}
于 2013-09-22T20:52:46.893 回答