媒体播放器在我的代码中不起作用

时间:2017-12-20 02:47:07

标签: java android android-mediaplayer

带有音频的Listview无法播放

你好,你好吗? 我正在尝试制作音频的列表视图,所有内容似乎都应该流动,但是当我点击一个列表项目时,音频应该重现,但它会显示“应用程序停止工作”我不明白我的代码有什么问题帮助我,我有时会打破我的头,但我无法解决这个问题。下面是logcat,然后我的代码,请有人帮助我使它工作。 - 谢谢你

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

}

3 个答案:

答案 0 :(得分:1)

使用如下代码:

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

玩:

mediaPlayer.start();

暂停:

mediaPlayer.pause();

答案 1 :(得分:1)

您的代码的问题在于您声明了一个未填充的单独的原始资源数组。

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

<强>解决方案

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

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 :(得分:0)

您需要处理资源文件,您可以创建另一种填充歌曲列表的方法,问题来自您使用空数组的int[] resID = new int[0];

另见Supported Media Formats

<强> ADD

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

使用songs作为播放列表

<强> 更新

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

}