У меня есть 8 кнопок в формате 4x2 Grid Layout каждый с текстом случайного английского слова или фразы. Каждая кнопка воспроизводит очень короткий аудиофайл, переводя слово или фразу на французский.Звуковые сбои после нажатия кнопок несколько раз
Как только я нажимаю кнопки несколько раз, все кнопки перестают работать. Единственный способ, которым приложение работает снова, - полностью закрыть эмулятор и снова запустить его.
Вот код Java:
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);
}
}
Ошибка заключается в следующем:
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>
Разрыв этого режима, если вы нажмете только ту же кнопку? Как только вы нажали все кнопки хотя бы один раз? Я не знаком с фреймворком, но вам нужно сделать какую-либо очистку на ваших вызовах MediaPlayer.create? – user2896976
Исходя из ограниченного количества коротких аудиофайлов и того, как работает ваше приложение, я бы рекомендовал использовать ['SoundPool'] (https://developer.android.com/reference/android/media/SoundPool.html) вместо' MediaPlayer '. – Tigger
@ user2896976 Знаете, что, я думаю, что однажды я нажимаю все кнопки хотя бы один раз. Я думаю, что все. Я нажимал одну и ту же кнопку несколько раз подряд, и ничего не происходит. – MAA