gpt4 book ai didi

android - 从 SD 卡播放 mp3 歌曲

转载 作者:行者123 更新时间:2023-11-29 00:48:11 26 4
gpt4 key购买 nike

我正在从 android 中的 sdcard 访问歌曲,歌曲显示在 ListView 中,但我无法播放这些文件。

代码如下:

package com.ex.imageGallery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.media.MediaPlayer;
import android.net.Uri;

public class temp extends Activity{
protected static final String TAG = null;
private Cursor audioCursor;
private int video_column_index;
private static String currentposition;
ListView videolist;
int count;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_audio);
//init_phone_video_grid();
System.gc();
final String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.SIZE };

audioCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
count = audioCursor.getCount();
videolist = (ListView) findViewById(R.id.PhoneVideoList);
videolist.setAdapter(new AudioAdapter(getApplicationContext()));
videolist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//System.out.println(proj[0]);
System.out.println(audioCursor.getString(position));
//Uri mur = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI+Uri.parse(audioCursor.getString(position));
try{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(audioCursor.getString(position));
mp.prepare();
mp.start();
}
catch(Exception ex)
{
System.out.println(ex);
}


}



});
}

class AudioAdapter extends BaseAdapter {
private Context vContext;

public AudioAdapter(Context c) {
vContext = c;
}

public int getCount() {
return count;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(vContext.getApplicationContext());
String id = null;
if (convertView == null) {
video_column_index = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
audioCursor.moveToPosition(position);
id = audioCursor.getString(video_column_index);
video_column_index = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
audioCursor.moveToPosition(position);
id += "Size(KB):" + audioCursor.getString(video_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}

最佳答案

关于android - 从 SD 卡播放 mp3 歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com