2015-06-04 54 views
3

我有你的位置的GridView和我在的onCreate做的情況下,那麼,錯誤試圖啓動媒體播放器:開始叫狀態誤差(-38,0)

case position = 1 play audio1,

case position = 2 play audio2

但是,當我執行,播放一個錯誤並停止播放聲音。

start called in state error (-38, 0)

的活動:

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

錯誤:

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) 
+0

你檢查這些http://stackoverflow.com/questions/9888510/mediaplayer-error-19-0-after-repeated-plays http://stackoverflow.com/questions/16172496/mediaplayer -error-19-0-doesnt-work-release- –

+0

我使用版本,但不播放音頻。剛開始玩。 –

回答

1

這個錯誤通常意味着你試圖調用一個函數,而在錯誤狀態,例如調用seek而媒體播放器還沒有準備好。 嘗試添加然後mpApple.setOnPreparedListener()聽衆稱爲start()一旦準備,你也將需要釋放玩家曾經因爲他們在文檔建議你做,所以我建議你做一個MediaPlayer實例,並停止+使用它之前釋放播放新的音頻文件。例如

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

非常感謝!有效! –

0

如果您停止媒體播放器提示錯誤,因爲啓動媒體播放器繼續和你進一步的開始。你的解決方案是你停止媒體,然後再次啓動它的工作。

+0

我加停止像這樣「情況1:mpBeer.stop(); mpBeer.start(); break;」但是當我運行不播放任何聲音。 –

0

try like this..

switch (position) { 
case 0: 

    if (mp != null) 
    mp.release(); 
    mpApple.start(); 
    break; 
    } 
相關問題