我正在尝试创建一个音板应用程序,它适用于播放的前几个mp3,但之后我收到以下错误: “MediaPlayer-JNI:MediaPlayer未经发布而最终确定。”我是否正确发布了音频? (看最后一个功能)。谢谢。这是我的代码:
public class MainActivity extends AppCompatActivity {
GridView gridview;
MediaPlayer mediaPlayer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
mediaPlayer = new MediaPlayer();
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.rich1);
mediaPlayer.start();
break;
case 1:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.no_youre_finished);
mediaPlayer.start();
break;
case 2:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.mexico);
mediaPlayer.start();
break;
case 3:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.thank_you_darling);
mediaPlayer.start();
break;
case 4:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.youll_get_bored_with_winning);
mediaPlayer.start();
break;
case 5:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.i_just_sold_);
mediaPlayer.start();
break;
case 6:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.amazingvictory);
mediaPlayer.start();
break;
case 7:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.bequiet);
mediaPlayer.start();
break;
case 8:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.bequietandjusthitthem);
mediaPlayer.start();
break;
case 9:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.bernysanders);
mediaPlayer.start();
break;
case 10:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.buildinghotel);
mediaPlayer.start();
break;
case 11:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.buildmilitary);
mediaPlayer.start();
break;
case 12:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.canigoalong);
mediaPlayer.start();
break;
case 13:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.china);
mediaPlayer.start();
break;
case 14:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.countrygoingtohell);
mediaPlayer.start();
break;
case 15:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.enough);
mediaPlayer.start();
break;
case 16:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.ever);
mediaPlayer.start();
break;
case 17:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.fivehundredbillion);
mediaPlayer.start();
break;
case 18:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.fordismovingtomexico);
mediaPlayer.start();
break;
case 19:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.fortyninepercent);
mediaPlayer.start();
break;
case 20:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.friendsleavinggermany);
mediaPlayer.start();
break;
case 21:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.gethimout);
mediaPlayer.start();
break;
case 22:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.getoutandvote);
mediaPlayer.start();
break;
case 23:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.heardofivanka);
mediaPlayer.start();
break;
case 24:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.heresapoll);
mediaPlayer.start();
break;
case 25:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.hewasverynasty);
mediaPlayer.start();
break;
case 26:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.iamselffunding);
mediaPlayer.start();
break;
case 27:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.idontknowmaybesomethingelse);
mediaPlayer.start();
break;
case 28:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.ifwereattackedjapan);
mediaPlayer.start();
break;
case 29:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.ihavealotoffriendschina);
mediaPlayer.start();
break;
case 30:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.ilovepolice);
mediaPlayer.start();
break;
case 31:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.iloveyou);
mediaPlayer.start();
break;
case 32:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.iorderfourthousand);
mediaPlayer.start();
break;
case 33:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.isntittrue);
mediaPlayer.start();
break;
case 34:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.largestdrugbuyer);
mediaPlayer.start();
break;
case 35:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.legally);
mediaPlayer.start();
break;
case 36:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.letsdoit);
mediaPlayer.start();
break;
case 37:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.listentothis);
mediaPlayer.start();
break;
case 38:
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.lookatthosenumbers);
mediaPlayer.start();
break;
default:
break;
}
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
}
});
}
}
答案 0 :(得分:0)
所以我最终做的是在switch语句之前释放媒体播放器:
// Release any resources from previous MediaPlayer
if (mediaPlayer != null) {
mediaPlayer.release();
}