我有一个GridView与您的位置,我在 onCreate 上做案例,所以,
案例位置= 1播放audio1,
案例位置= 2播放audio2
活动:
public class HomeEnglishAnimalsActivity extends ActionBarActivity {
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_english_animals);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
GridView gridView = (GridView) findViewById(R.id.gridviewHomeEnglishAnimals);
gridView.setAdapter(new AdapterHomeEnglishAnimalsActivity(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch (position) {
case 0:
if (player != null)
{
player.stop();
player.release();
}
player.create(HomeEnglishAnimalsActivity.this, R.raw.bee).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
break;
好的,代码可以工作,但是当我点击几次时,声音停止显示这个错误:
06-16 23:52:42.976 23918-23918/br.gabrielrossetto.tap4learn E/ExtMediaPlayer-JNI? QCMediaPlayer could not be located....
06-16 23:52:42.976 23918-23918/br.gabrielrossetto.tap4learn E/MediaPlayer-JNI? QCMediaPlayer mediaplayer NOT present
06-16 23:52:42.986 23918-23918/br.gabrielrossetto.tap4learn E/MediaPlayer? Should have subtitle controller already set
06-16 23:52:42.996 23918-23918/br.gabrielrossetto.tap4learn E/MediaPlayer? error (-19, 0)
06-16 23:52:42.996 23918-23918/br.gabrielrossetto.tap4learn E/MediaPlayer? Error (-19,0)
感谢您的帮助。
答案 0 :(得分:8)
试试这个
当您创建MediaPlayer对象并启动播放器时,您需要在音乐完成时将其释放。
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
答案 1 :(得分:3)
您需要释放用于播放声音的MediaPlayer。看看这个主题:Vladimir Kornea