媒体播放器在我的代码中不起作用 [英] media player does not work in my code

查看:106
本文介绍了媒体播放器在我的代码中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,你好吗? 我正在尝试制作音频的列表视图,似乎一切都按原样流动,但是当我单击列表项时,音频应该再现,但出现应用程序停止工作",我不明白我的代码有什么问题,有人可以帮帮我,我有时会伤脑筋,但我不能解决这个问题.下面是logcat,然后是我的代码,请有人帮我使其正常工作. -谢谢

Hello how are you? I'm trying to make a listview of audios everything seems to flow as it should, but when I click on a listitem the audio should reproduce but instead it appears "the app stopped working" I do not understand what's wrong with my code someone can help me I'm at times breaking my head but I can not solve this problem. Below is the logcat, and then my code, please someone help me make it work. -Thank you

Logcat

public class MainActivity extends AppCompatActivity {
    ListView lv;
    MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memes_main);

        lv = findViewById(R.id.lv);
        mp = new MediaPlayer();

        ArrayList<memes> item = new ArrayList<>();

        item.add(new memes("Gemidão", R.raw.gemidaoremix));
        item.add(new memes("Nunca nem vi", R.raw.nuncanemvi));


        ArrayAdapter<memes> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
        lv.setAdapter(adapter);

        lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                                    int position, long id) {
                playSong(position);

            }
        });
    }


    public void playSong(int songIndex) {

        mp.reset();
        int[] resID = new int[0];
        mp = MediaPlayer.create(this, resID[songIndex]);

        mp.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.release();
    }


    }

memes.class

public class memes{

    private String nome;
    private int resID;

    memes(String nome, int resID){

        this.nome = nome;
        this.resID = resID;
    }

    public String getNome(){
        return nome;
    }

    public int getResId(){
        return resID;
    }

    @Override
    public String toString(){
        return nome;
    }

}

推荐答案

您的代码存在的问题是,您声明了一个单独的未填充的原始资源数组.

The problem with your code is that you are declaring a separate array of raw resource which is not populated.

int[] resID = new int[0];
mp = MediaPlayer.create(this, resID[songIndex]);

解决方案

由于要使用arraylist填充ListView,因此需要从Arraylist本身获取资源.解决方案将是这样的:

Since you are using arraylist to populate the ListView,you need to fetch the resource from the Arraylist itself. The solution will be like this :

public class MainActivity extends AppCompatActivity {

    ListView lv;
    MediaPlayer mp;
    ArrayList<Memes> item;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = findViewById(R.id.lv);
        mp = new MediaPlayer();

        item = new ArrayList<>();

        item.add(new Memes("Gemidão", R.raw.gemidaoremix));
        item.add(new Memes("Nunca nem vi", R.raw.nuncanemvi));


        ArrayAdapter<Memes> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
        lv.setAdapter(adapter);

        lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                                    int position, long id) {
                playSong(position);

            }
        });
    }

    public void playSong(int songIndex) {
        mp.reset();
        mp = MediaPlayer.create(this, item.get(songIndex).getResId());
        mp.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.release();
    }


}

这篇关于媒体播放器在我的代码中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆