Error while trying to start media player: Start call in error state (-38, 0)
I have a GridView with your positions and I am doing it on onCreate.So,
case position = 1 play audio1,
case position = 2 play audio2
But when I execute, diplay an error and stop playing sounds.
start throwing in state error (-38, 0)
Activity:
public class HomeEnglishFoodsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_english_foods);
GridView gridView = (GridView) findViewById(R.id.gridviewHomeEnglishFoods);
gridView.setAdapter(new AdapterHomeEnglishFoodsActivity(this));
final MediaPlayer mpApple = MediaPlayer.create(this, R.raw.apple);
final MediaPlayer mpBeer = MediaPlayer.create(this, R.raw.beer);
final MediaPlayer mpCoffee = MediaPlayer.create(this, R.raw.coffee);
final MediaPlayer mpCheese = MediaPlayer.create(this, R.raw.cheese);
final MediaPlayer mpFrenchfries = MediaPlayer.create(this, R.raw.frenchfries);
final MediaPlayer mpIcecream = MediaPlayer.create(this, R.raw.icecream);
final MediaPlayer mpHotdog = MediaPlayer.create(this, R.raw.hotdog);
final MediaPlayer mpWine = MediaPlayer.create(this, R.raw.wine);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch (position) {
case 0:
mpApple.start();
break;
case 1:
mpBeer.start();
break;
case 2:
mpCoffee.start();
break;
case 3:
mpCheese.start();
break;
case 4:
mpFrenchfries.start();
break;
case 5:
mpIcecream.start();
break;
case 6:
mpHotdog.start();
break;
case 7:
mpWine.start();
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_english_foods, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Mistake:
06-04 00:46:21.334 17752-17752/br.gabrielrossetto.tap4learn E/MediaPlayer﹕ Error (-19,0)
06-04 00:46:29.612 17752-17752/br.gabrielrossetto.tap4learn E/MediaPlayer﹕ start called in state 0
06-04 00:46:29.612 17752-17752/br.gabrielrossetto.tap4learn E/MediaPlayer﹕ error (-38, 0)
06-04 00:46:29.652 17752-17752/br.gabrielrossetto.tap4learn E/MediaPlayer﹕ Error (-38,0)
source to share
This error usually means that you are trying to call a function while in the wrong state, such as calling a search while the media player is not ready yet. Try to add a mpApple.setOnPreparedListener()
listener then called start()
after it has been prepared, also you will need to release the player as soon as you are done as they recommend in the documentation, so I suggest you create one instance MediaPlayer
and stop + release before using it to play a new audio file. eg
public class HomeEnglishFoodsActivity extends ActionBarActivity
{
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
{
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View int position, long id) {
switch (position)
{
case 0:
if(player1 !=null)
{
player1.stop();
player1.release();
}
player1.create(this, R.raw.apple).setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer)
{
mediaPlayer.start();
}
});
}
}
});
}
}
source to share