You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将raw目录音频导入ListView并自定义名称(含播放显示)

解决自定义音频名称显示与播放时更新TextView的问题

我来帮你搞定这两个需求,咱们一步一步来:

第一步:建立自定义名称与音频资源的关联

原来你是直接通过R.raw.class.getFields()获取系统默认的文件名,现在要替换成自定义名称,最清晰的方式是创建一个数据结构来绑定音频资源ID和你想要的自定义名称。先定义一个简单的Song类:

public class Song {
    private int resourceId;
    private String customName;

    public Song(int resourceId, String customName) {
        this.resourceId = resourceId;
        this.customName = customName;
    }

    public int getResourceId() {
        return resourceId;
    }

    public String getCustomName() {
        return customName;
    }
}

接着在onCreate方法里,把raw目录里的音频和自定义名称一一对应起来:

// 替换原来的fields和mediaList逻辑
List<Song> songList = new ArrayList<>();
// 按照你的需求添加,比如raw里的audio1对应"清晨的鸟叫",audio2对应"雨夜私语"
songList.add(new Song(R.raw.audio1, "清晨的鸟叫"));
songList.add(new Song(R.raw.audio2, "雨夜私语"));
songList.add(new Song(R.raw.audio3, "咖啡馆小调"));
// 继续添加更多音频...

第二步:修改ListView适配器,显示自定义名称

接下来给ListView设置适配器,让它展示咱们的自定义名称,用ArrayAdapter就能轻松实现:

// 初始化ListView(确保布局里的ListView ID和这里对应)
listView = findViewById(R.id.listView);

// 创建自定义适配器
ArrayAdapter<Song> adapter = new ArrayAdapter<>(this,
        android.R.layout.simple_list_item_1, songList) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = view.findViewById(android.R.id.text1);
        // 设置自定义名称为列表项显示内容
        textView.setText(songList.get(position).getCustomName());
        return view;
    }
};

listView.setAdapter(adapter);

第三步:播放时更新TextView显示当前歌曲

给ListView添加点击事件,点击时播放对应音频,并把当前播放的歌曲名称同步到textBilgi里:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Song currentSong = songList.get(position);
        
        // 先停止并释放之前的MediaPlayer(避免重复播放)
        if (mp != null && mp.isPlaying()) {
            mp.stop();
            mp.release();
        }
        
        // 初始化MediaPlayer并开始播放
        mp = MediaPlayer.create(GirisActivity.this, currentSong.getResourceId());
        mp.start();
        
        // 更新TextView显示当前播放的歌曲信息
        textBilgi.setText("正在播放:" + currentSong.getCustomName());
        
        // 可选:播放结束后更新提示
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                textBilgi.setText("播放结束");
                mp.release();
            }
        });
    }
});

额外的内存优化提醒

记得在Activity销毁时释放MediaPlayer资源,防止内存泄漏:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mp != null) {
        mp.release();
        mp = null;
    }
}

这样操作后,ListView就会显示你设置的自定义名称,点击播放时textBilgi也会实时展示当前播放的歌曲信息啦!

内容的提问来源于stack exchange,提问作者Alper Göktepe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:29:23