媒体播放器在播放时突然停止

时间:2013-10-14 12:38:22

标签: listview android-mediaplayer

我是android新手 我已经创建了一个媒体播放器应用程序,但每当我开始播放它时会突然停止并显示错误,因为“很遗憾,youapp停止了”。 我应该如何纠正我的应用程序?

请帮助我这里的代码:

package com.example.allah;


import java.util.ArrayList;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listView1);
String[] values = new String[] { "Allah1", "Allah2" };
final ArrayList<String> list = new ArrayList<String>();

for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
myplaymethod();

}
});
}
public void myplaymethod(){
int myKey = 0;
View play_button = null;
switch (myKey) {
case 1:
    MediaPlayer my = MediaPlayer.create(getBaseContext(), R.raw.allah1);
    my.start();
    play_button.setVisibility(View.GONE);
    break;

case 2:
    MediaPlayer ma = MediaPlayer.create(getBaseContext(), R.raw.allah2);
    ma.start();
    play_button.setVisibility(View.GONE);
    break;

}


}

1 个答案:

答案 0 :(得分:0)

您的mKey值为0,并且开关中没有取0的情况,请这样做

我假设您的列表中只有两首歌曲,因为您在mplayermethod()中只有两个案例。

listview.setOnItemClickListener内用mplayermethod(arg2)替换你的mplayermethod()

和mplayermethod()

public void myplaymethod(int mKey){

View play_button = null;
switch (myKey) {
case 0:
MediaPlayer my = MediaPlayer.create(getBaseContext(), R.raw.allah1);
my.start();
play_button.setVisibility(View.GONE);
break;

case 1:
MediaPlayer ma = MediaPlayer.create(getBaseContext(), R.raw.allah2);
ma.start();
play_button.setVisibility(View.GONE);
break;

}