如何在android中播放媒体文件

时间:2013-09-22 14:47:44

标签: android

我已经恢复了手机中的歌曲, 但我无法播放这首歌。请帮我。我使用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();
    }

}


}

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

3 个答案:

答案 0 :(得分:2)

最后我得到了解决方案。这是代码

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();
    }

}

答案 1 :(得分:1)

尝试添加:

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

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

如果那不起作用,see this

答案 2 :(得分: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();
}

}