media player does not work in my code

谁说胖子不能爱 提交于 2020-01-04 07:04:21

问题


Listview with audios does not play

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

MainActivity.class

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

}

回答1:


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]);

Solution

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


}



回答2:


Use code like below:

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);

to play:

mediaPlayer.start();

to pause:

mediaPlayer.pause();



回答3:


you need to work on the resource files , you can create another method that fills your list of songs , your problem comes from the int[] resID = new int[0]; you are using an empty array.

see also Supported Media Formats

ADD

int[] songs={R.raw.gemidaoremix,R.raw.nuncanemvi};

use songs as a playlist

UPDATE

public class MainActivity extends AppCompatActivity {
    ListView lv;
    MediaPlayer mp;
    int[] songs={R.raw.gemidaoremix,R.raw.nuncanemvi};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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();
    mp = MediaPlayer.create(this, songs[songIndex]);

    mp.start();
}

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

}



来源:https://stackoverflow.com/questions/47898076/media-player-does-not-work-in-my-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!