尝试启动媒体播放器时出错:启动调用状态错误(-38,0)

时间:2015-06-04 05:14:57

标签: android gridview media-player

我有一个GridView与您的位置,我在 onCreate 上做案例,所以,

  

案例位置= 1播放audio1,

     

案例位置= 2播放audio2

但是当我执行时,显示错误并停止播放声音。

  

开始调用状态错误(-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)

3 个答案:

答案 0 :(得分:1)

此错误通常意味着您在错误状态下尝试调用函数,例如在媒体播放器尚未就绪时调用搜索。 尝试添加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();
                            }
                        });
                }
            }
        });
}
}

答案 1 :(得分:0)

如果您停止mediaPlayer,则会出现错误,因为启动mediaPlayer会继续启动并进一步启动。你的解决方案就是停止媒体,然后重新开始工作。

答案 2 :(得分:0)

  

试试这个..

 switch (position) {
 case 0:

    if (mp != null)
    mp.release(); 
     mpApple.start();
    break;
    }