我正在创建一个mp3player,所有音频文件都从本地设备存储中提取。我在ListView中显示项目。
但是,当我单击任何特定项目时,我遇到了异常。我无法解决此问题。
Unknown URL: content://media/external/audio/media/null
这是我的代码-
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
private long lg;
private Context mContext;
private android.app.Activity mActivity;
private ArrayList<Song> songList;
private MediaPlayer mPlayer;
private java.util.HashMap<Long,String> mAudioMap = new java.util.HashMap<>();
private ListView songView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runtimpermission();
mContext = getApplicationContext();
mActivity= MainActivity.this;
songView = (ListView) findViewById(R.id.song_list);
songList = new ArrayList<Song>();
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
getSongList();
Collections.sort(songList, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String titleValue = (String) adapterView.getItemAtPosition(position);
Uri fileUri =Uri.withAppendedPath(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, titleValue);
stopPlaying();
try{
// Initialize the media player
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);
// Set media player data source
mPlayer.setDataSource(mContext,fileUri );
//Toast.makeText(mContext,contentUri+"",Toast.LENGTH_SHORT).show();
// Prepare the selected audio
mPlayer.prepare();
// Finally, start playing selected music
mPlayer.start();
// Listener for media player completion
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
}
});
}catch (java.io.IOException e){
// When IO exception catch
e.printStackTrace();
Toast.makeText(mContext,"Error.",Toast.LENGTH_SHORT).show();
}
}
});
}
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songList.add(new Song(thisId, thisTitle, thisArtist));
}
while (musicCursor.moveToNext());
}
}
protected void stopPlaying(){
if(mPlayer!=null){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
Toast.makeText(this,"Stop playing",Toast.LENGTH_SHORT).show();
}
}
}
这是完整的错误日志-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.monstertechno.musicplayerappui, PID: 12872
java.lang.IllegalStateException: Unknown URL: content://media/external/audio/media/null
at android.os.Parcel.readException(Parcel.java:1607)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:235)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:187)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1104)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:865)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1007)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:973)
at com.monstertechno.musicplayerappui.MainActivity$2.onItemClick(MainActivity.java:82)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3050)
at android.widget.AbsListView$3.run(AbsListView.java:3893)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)