Reputation: 81
I am using AIDE for Android, and have 2 java files (one is MainActivity.java, and the other is M4APlayer.Java).
During the onCreate()
call, I have a text changed listener attached to an EditText control named sugar
so that when the text inside that control changes, it calls the onTextChanged()
function.
Inside the onTextChanged()
function, I have 4 different scenarios (where a person's blood sugar can either be low and dropping, low and rising, high and dropping, or high and rising). Depending on what number (from 1 to 4) is inside the sugar
box, my goal is to pass a String to a function called M4APlayer.play() in M4APlayer.java, determining what sound to play (depending on what number from 1 to 4 is entered in the sugar
box).
Everything worked fine until I added the MediaPlayer.create()
function. My app now crashes with the error
Unable to match the desired swap behavior
What could this mean? Here is my MainActivity.java code:
package com.UnitedToolsForSouls.SoundAlert;
import android.app.*;
import android.os.*;
import java.util.*;
import android.widget.*;
import android.text.*;
import android.content.*;
public class MainActivity extends Activity {
private static EditText sugar;
private static TextView result;
private static TextView scenario1;
private static TextView scenario2;
private static TextView scenario3;
private static TextView scenario4;
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
sugar = (EditText) findViewById(R.id.sugar);
result = (TextView) findViewById(R.id.result);
scenario1 = (EditText) findViewById(R.id.scenario1);
scenario2 = (EditText) findViewById(R.id.scenario2);
scenario3 = (EditText) findViewById(R.id.scenario3);
scenario4 = (EditText) findViewById(R.id.scenario4);
sugar.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Called as text is being changed
String sug = sugar.getText().toString();
String s1 = scenario1.getText().toString();
String s2 = scenario2.getText().toString();
String s3 = scenario3.getText().toString();
String s4 = scenario4.getText().toString();
result.setText("???");
if (sug.length() == 1) {
if (sug.charAt(0) == '1') {
result.setText(s1);
}
if (sug.charAt(0) == '2') {
result.setText(s2);
}
if (sug.charAt(0) == '3') {
result.setText(s3);
}
if (sug.charAt(0) == '4') {
result.setText(s4);
}
M4APlayer.play(context, result.getText().toString());
}
}
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4) {
// TODO: Implement this method
}
@Override
public void afterTextChanged(Editable p1) {
// TODO: Implement this method
}
});
}
}
Here is my code for M4APlayer:
package com.UnitedToolsForSouls.SoundAlert;
import android.content.Context;
import android.media.MediaPlayer;
public class M4APlayer {
public static void play(Context context, String file) {
/*PROBLEM*/
MediaPlayer mp = MediaPlayer.create(context, R.id.scenario1);
//mp.start();
//Stop the fplayer.
//Loop and play the file when nothing is playing.
}
}
Also, why would MediaPlayer.create()
take an integer argument as the second argument, instead of a String (for whatever the filename to play would be)?
I tried commenting out the code in M4APlayer.play()
until my app stopped crashing, to find that it was MediaPlayer.create()
that did this. There is no specific answer yet to this scenario. One answer suggested on clearing the cache and restarting my phone, but commenting out that one line did the trick.
Upvotes: 0
Views: 60
Reputation: 10582
The second parameter to:
MediaPlayer.create(context, R.id.scenario1)
is expected to be a raw resource ID but you are passing a view ID.
Make sure you have put your audio files to the src/main/res/raw
directory. And use the resource ID of the form R.raw.your_audio_file
.
Upvotes: 0