2017-02-05 9 views
0

4つのグリッドレイアウトに8つのボタンがあり、それぞれに英語の単語やフレーズがランダムに表示されます。各ボタンは、単語またはフレーズをフランス語に翻訳する非常に短いオーディオファイルを再生します。ボタンを数回押した後で音声がクラッシュする

ボタンを数回押すと、すべてのボタンがもう機能しなくなります。アプリが再び動作する唯一の方法は、エミュレータを完全にシャットダウンしてからもう一度起動することです。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 

    @Override 
    public void onClick(View view) { 
     switch(view.getId()) 
     { 
      case R.id.button1: 
       Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.hello).start(); 
       break; 
      case R.id.button2: 
       Toast.makeText(this, "How are you?", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.howareyou).start(); 
       break; 
      case R.id.button3: 
       Toast.makeText(this, "My name is", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.mynameis).start(); 
       break; 
      case R.id.button4: 
       Toast.makeText(this, "Do you speak English?", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.doyouspeakenglish).start(); 
       break; 
      case R.id.button5: 
       Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.goodevening).start(); 
       break; 
      case R.id.button6: 
       Toast.makeText(this, "Please", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.please).start(); 
       break; 
      case R.id.button7: 
       Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.welcome).start(); 
       break; 
      case R.id.button8: 
       Toast.makeText(this, "I live in", Toast.LENGTH_SHORT).show(); 
       MediaPlayer.create(this, R.raw.ilivein).start(); 
       break; 
     } 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

//  ViewGroup grid = (ViewGroup) findViewById(R.id.gridLayout); 
//  for(int count = 0; count < grid.getChildCount(); count++) { 
//   View childView = grid.getChildAt(count); 
//   int resID = childView.getId(); 
//   Log.i("ID", Integer.toString(resID)); 
//   findViewById(resID).setOnClickListener(this); 
//  } 

     findViewById(R.id.button1).setOnClickListener(this); 
     findViewById(R.id.button2).setOnClickListener(this); 
     findViewById(R.id.button3).setOnClickListener(this); 
     findViewById(R.id.button4).setOnClickListener(this); 
     findViewById(R.id.button5).setOnClickListener(this); 
     findViewById(R.id.button6).setOnClickListener(this); 
     findViewById(R.id.button7).setOnClickListener(this); 
     findViewById(R.id.button8).setOnClickListener(this); 

    } 
} 

エラーは以下の通りである:ここで

は、Javaコードで

D/MediaPlayer: setSubtitleAnchor in MediaPlayer 
E/EGL_emulation: tid 2462: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH) 
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x956ee900, error=EGL_BAD_MATCH 
E/MediaPlayer: error (1, -19) 
E/MediaPlayer: Error (1,-19) 

私はそれがリソースのいくつかの並べ替えを閉じていないとは何かを持って感じている、オーディオ私が推測するファイル。私はそれがとにかくあり、ここでXMLコードが関連している疑いが、

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.mustafa.gridlayoutdemo.MainActivity"> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/gridLayout"> 
     <Button 
      android:text="Hello" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button1" 

      android:layout_row="0" 
      android:layout_column="0" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="how are you" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button2" 

      android:layout_row="0" 
      android:layout_column="1" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="my name is" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button3" 

      android:layout_row="1" 
      android:layout_column="0" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="do you speak english" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button4" 

      android:layout_row="1" 
      android:layout_column="1" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="good evening" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button5" 

      android:layout_row="2" 
      android:layout_column="0" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="please" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button6" 

      android:layout_row="2" 
      android:layout_column="1" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="welcome" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button7" 

      android:layout_row="4" 
      android:layout_column="0" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 

     <Button 
      android:text="i live in" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button8" 

      android:layout_row="4" 
      android:layout_column="1" 
      android:layout_gravity="fill" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"/> 
    </GridLayout> 
</RelativeLayout> 
+1

同じボタンだけを押すと壊れますか?少なくとも一度はすべてのボタンを押したことがありますか?フレームワークに精通していませんが、 'MediaPlayer.create'コールでクリーンアップを行う必要がありますか? – user2896976

+1

限られた数の短いオーディオファイルとアプリケーションの仕組みに基づいて、 'MediaPlayer 'の代わりに[' SoundPool'](https://developer.android.com/reference/android/media/SoundPool.html)を使用することをお勧めします'。 – Tigger

+0

@ user2896976あなたが知っていることは、一度は少なくとも一度はすべてのボタンを押すことだと思います。私はそうだと思います。同じボタンを数回続けて押しても何も起こりません。 – MAA

答えて

1

で以下のコードを追加し

MediaPlayer mediaPlayer = MediaPlayer.create(cotext, null); 

次のようにグローバルMediaPlayerオブジェクトを作成し、この

のようにしてみてください新しいメディアファイルを開始/再生する前に、以前の呼び出し。ここに参考になる可能性のある改訂コードがあります。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 

private MediaPlayer mp = new MediaPlayer(); 

@Override 
public void onClick(View view) { 
    switch(view.getId()) 
    { 
     case R.id.button1: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.hello); 
      mp.start(); 
      break; 
     case R.id.button2: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "How are you?", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.howareyou) 
      mp.start(); 
      break; 
     case R.id.button3: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "My name is", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.mynameis); 
      mp.start(); 
      break; 
     case R.id.button4: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "Do you speak English?", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.doyouspeakenglish); 
      mp.start(); 
      break; 
     case R.id.button5: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.goodevening); 
      mp.start(); 
      break; 
     case R.id.button6: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "Please", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.please); 
      mp.start(); 
      break; 
     case R.id.button7: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.welcome); 
      mp.start(); 
      break; 
     case R.id.button8: 
      if(mp.isPlaying()){ 
       stopPlaying(); 
      } 
      Toast.makeText(this, "I live in", Toast.LENGTH_SHORT).show(); 
      mp = MediaPlayer.create(this, R.raw.ilivein); 
      mp.start(); 
      break; 
    } 
} 

private void stopPlaying() { 
      if (mp != null) { 
       mp.stop(); 
       mp.release(); 
       mp = null; 
      } 
     } 
+0

私はこれがうまくいくと思ったので、アップヴォート。その後、アプリ全体がクラッシュしました。ここにスタックトレースの最も関連する部分があります。 java.lang.NullPointerException:NULLオブジェクト参照で仮想メソッド 'boolean android.media.MediaPlayer.isPlaying()'を呼び出そうとしました – MAA

+0

ifステートメント内のステートメントにあります。 MediaPlayer mpは初期化されていないが、isPlayingメソッドが呼び出されているためだと思う。 – MAA

+0

ええ、それが理由だ。私は答えを編集しました。申し訳ありませんが、間違いです。今すぐコードが動作しますか? – tahsinRupam

0

それはあまりにも多くのメディアプレーヤーが原因である可能性があり、一度にオープンしました。そこで、次に、各caseのためには1つだけですMediaPlayerのオブジェクトを使用して停止する必要があり、対応する解像度のID

AssetFileDescriptor afd = MainActivity.this.getResources().openRawResource(R.raw.livein) 
mediaPlayer.setDataSource(afd); 
mediaPlayer.start(); 
関連する問題