dimanche 1 mars 2015

Android App crashing at mediaplayer prepareAsync();



I'm trying to make an app that asks the user for verbal input of how tired they are at certain times during a work shift. (The app will play an audio file that asks them how tired they are). I have placed log messages in the app to figure out at which part the app is crashing and it stops directly before prepareAsync().


Below is the code and I'll attach the logs at the end. Thank you in advance.


MainActivity.Java



import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.PopupWindow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class MainActivity extends ActionBarActivity {


// Initializes variables
private final static String STORETEXT = "storetext.txt";
private final static String STOREID = "storeid.txt";
public EditText newNumber, newPass;
private TextView idt, idn;
Context context;
private PopupWindow CID, RS, AP;


// Sets parameters on instance create
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button startButton = (Button) this.findViewById(R.id.start_button);
Button aboutButton = (Button) this.findViewById(R.id.about_button);
idt = (TextView) this.findViewById(R.id.idnumber);
idn = (TextView) this.findViewById(R.id.numberid);
context = getApplicationContext();

try {
InputStream inp = openFileInput(STOREID);

if (inp != null) {
InputStreamReader temp = new InputStreamReader(inp);

BufferedReader read = new BufferedReader(temp);

String str;

StringBuilder buf = new StringBuilder();

while ((str = read.readLine()) != null) {

buf.append(str+"\n");

}
inp.close();

idn.setText(buf.toString());
}
} catch (IOException t) {
Toast.makeText( this, "Exception: " + context.toString(), Toast.LENGTH_LONG).show();


}


startButton.setOnClickListener(onClickListener);
aboutButton.setOnClickListener(onClickListener);
}

@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_main, menu);
return true;
}


// Sets what happens when menu items are pressed
@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.view_results) {
initiateVRPopupWindow();
return true;
} else if (id == R.id.change_id_number) {
initiateCIDPopupWindow();

return true;
}
return super.onOptionsItemSelected(item);
}

// Initializes the Change ID Popup Window
private void initiateVRPopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.result_screen,
(ViewGroup) findViewById(R.id.result_screen_popup));

RS = new PopupWindow(layout, 700, 940, true);
RS.showAtLocation(layout, Gravity.CENTER, 0, 0);

Button Exit = (Button) layout.findViewById(R.id.RSExit);
TextView FinalResult = (TextView) layout.findViewById(R.id.RSResultText);

try {
InputStream in = openFileInput(STORETEXT);

if (in != null) {
InputStreamReader tmp = new InputStreamReader(in);

BufferedReader reader = new BufferedReader(tmp);

String str;

StringBuilder buf = new StringBuilder();

while ((str = reader.readLine()) != null) {

buf.append(str+"\n");

}
in.close();

FinalResult.setText(buf.toString());
}
} catch (java.io.FileNotFoundException e) {
Toast.makeText( this, "Exception: " + context.toString(), Toast.LENGTH_LONG).show();
}


// Create onClickListener to exit the View Result Screen
Exit.setOnClickListener(onClickListener);
} catch (Exception c) {
c.printStackTrace();
}
}

// Initializes the About Page Popup Window
private void initiateAPPopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.about_study,
(ViewGroup) findViewById(R.id.about_page));

AP = new PopupWindow(layout, 700, 940, true);
AP.showAtLocation(layout, Gravity.CENTER, 0, 0);

Button APExit = (Button) layout.findViewById(R.id.KSSExit);

APExit.setOnClickListener(onClickListener);
} catch (Exception c) {
c.printStackTrace();
}
}


// Initializes the Change ID Popup Window
private void initiateCIDPopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.id_change_popup,
(ViewGroup) findViewById(R.id.CIDpopup_element));

CID = new PopupWindow(layout, 700, 940, true);

CID.showAtLocation(layout, Gravity.CENTER, 0, 0);

newPass = (EditText) layout.findViewById(R.id.CIDPassword);
newNumber = (EditText) layout.findViewById(R.id.CIDNewNumber);
Button CIDSubmit = (Button) layout.findViewById(R.id.CIDSubmit);
Button CIDCancel = (Button) layout.findViewById(R.id.CIDCancel);

CIDCancel.setOnClickListener(onClickListener);
CIDSubmit.setOnClickListener(onClickListener);

} catch (Exception e) {
e.printStackTrace();
}
}



// What happens when buttons are pressed
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.start_button:
Log.e("BDUKSSS", "Starting Shift");

String IDText = String.valueOf(idt.getText());
String IDNumber = String.valueOf(idn.getText());
String popup = "Hello " + IDText + " " + IDNumber;

Toast toast = Toast.makeText(context, popup, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 0);
toast.show();

Intent questionScreenGet = new Intent(MainActivity.this, QuestionScreen.class);
startActivity(questionScreenGet);

break;
case R.id.about_button:
Log.e("BDUKSSS", "Open About Screen popup");

initiateAPPopupWindow();

break;

case R.id.KSSExit:
Log.e("BDUKSSS", "Exit About Screen popup");

AP.dismiss();

break;

case R.id.CIDSubmit:
Log.e("BDUKSSS", "Submitting New ID#");

String plusPass = getResources().getString(R.string.correctPass);
String minusPass = getResources().getString(R.string.wrongPass);
String newID;


if (newPass.getText().toString().equals("BronsonDu")) {

newID = newNumber.getText().toString();

plusPass += newID;

Toast correctPass = Toast.makeText(context, plusPass, Toast.LENGTH_LONG);
correctPass.show();

try {
OutputStreamWriter output =
new OutputStreamWriter(openFileOutput(STOREID, 0));

output.write(newID);

output.close();
} catch (Throwable t) {
Toast.makeText( MainActivity.this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}

try {
OutputStreamWriter clear =
new OutputStreamWriter(openFileOutput(STORETEXT, 0));

clear.write("");

clear.close();
} catch (Throwable t) {
Toast.makeText( MainActivity.this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}

} else {
Toast wrongPass = Toast.makeText(context, minusPass, Toast.LENGTH_SHORT);
wrongPass.show();
}

break;

case R.id.CIDCancel:
Log.e("BDUKSSS", "Close Change ID popup");
CID.dismiss();

finish();
startActivity(getIntent());

break;

case R.id.RSExit:
Log.e("BDUKSSS", "Close Result Screen popup");
RS.dismiss();

break;
}

}
};

}


QuestionScreen.java



package com.example.requiem.bdukssstudy;

import android.app.Activity;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.content.ActivityNotFoundException;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.speech.RecognizerIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class QuestionScreen extends Activity{

private final static String STORETEXT = "storetext.txt";
private final static String STOREID = "storeid.txt";
private Button sendButton;
private MediaPlayer stateQuestion;
private TextView idt, idn;
private EditText userResponse;
private CountDownTimer timer;
private final int REQ_CODE_SPEECH_INPUT = 100;
Context context;

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

Button endButton = (Button) this.findViewById(R.id.end_shift);
sendButton = (Button) this.findViewById(R.id.QSSubmit);
idt = (TextView) this.findViewById(R.id.idnumber);
idn = (TextView) this.findViewById(R.id.numberid);
userResponse = (EditText) this.findViewById(R.id.user_response);
stateQuestion = MediaPlayer.create(this, R.raw.get);
context = getApplicationContext();

Log.e("BDUKSSS","Setting Audio Stream");
stateQuestion.setAudioStreamType(AudioManager.STREAM_MUSIC);
stateQuestion.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("BDUKSSS", "I crashed here Q.Q");
return false;
}
});
Log.e("BDUKSSS", "Crash check #1");
callSpeech();

try {
InputStream inp = openFileInput(STOREID);

if (inp != null) {
InputStreamReader temp = new InputStreamReader(inp);

BufferedReader read = new BufferedReader(temp);

String str;

StringBuilder buf = new StringBuilder();

while ((str = read.readLine()) != null) {

buf.append(str+"\n");

}
inp.close();

idn.setText(buf.toString());
}
} catch (IOException t) {
Toast.makeText( this, "Exception: " + context.toString(), Toast.LENGTH_LONG).show();
}

endButton.setOnClickListener(onClickListener);
sendButton.setOnClickListener(onClickListener);
}

//Start speech input call
private void callSpeech() {
stateQuestion.reset();
//stateQuestion.start();
Log.e("BDUKSSS", "Prepare started");
stateQuestion.prepareAsync();

stateQuestion.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {

Log.e("BDUKSSS", "An error occurred");

return false;
}
});

stateQuestion.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.e("BDUKSSS", "Prepare Completed");
stateQuestion.start();
Log.e("BDUKSSS", "Speech called");
}

});

stateQuestion.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.e("BDUKSSS", "Playback completed");
stateQuestion.stop();
Log.e("BDUKSSS", "Playback stopped");
stateQuestion.reset();
Log.e("BDUKSSS", "Playback reset");
promptSpeechInput();




Log.e("BDUKSSS", "Starting Timer");
timer = new CountDownTimer(50000, 1000) {

@Override
public void onTick(long millisUntilFinished) {}

@Override
public void onFinish() {
callSpeech();
} }.start();

}
});
}

// Show google speech input dialog
private void promptSpeechInput() {
Log.e("BDUKSSS", "Ask for input");
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));

try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), getString(R.string.speech_not_supported), Toast.LENGTH_SHORT).show();
}
}

// Receive speech input
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
userResponse.setText(result.get(0));
}

sendButton.performClick();

break;
}
}
}

private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.end_shift:
// When end shift button is pressed
Log.e("BDUKSSS", "Return to Main");

String IDText = String.valueOf(idt.getText());
String IDNumber = String.valueOf(idn.getText());
String popup = "Goodbye " + IDText + " " + IDNumber;
Log.e("BDUKSSS", "Return to Main2");
Toast toast = Toast.makeText(context, popup, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 0);
toast.show();
Log.e("BDUKSSS", "Return to Main3");

timer.cancel();
stateQuestion.release();
Log.e("BDUKSSS", "Return to Main4");
Intent goBack = new Intent(QuestionScreen.this, MainActivity.class);
startActivity(goBack);
Log.e("BDUKSSS", "Return to Main5");
finish();
Log.e("BDUKSSS", "Return to Main6");


break;

case R.id.QSSubmit:
// When Question Screen Submit button is pressed
Log.e("BDUKSSS", "Submit Result");

String Date = DateFormat.getDateTimeInstance().format(new Date());

EditText editText = (EditText) findViewById(R.id.user_response);
String message = String.valueOf(editText.getText()) + " " + Date + "\n";

Toast paste = Toast.makeText(context, message + " sent", Toast.LENGTH_LONG);
paste.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 0);
paste.show();

try {
OutputStreamWriter out =
new OutputStreamWriter(openFileOutput(STORETEXT, MODE_APPEND));

out.write(message);

out.close();
} catch (Throwable t) {
Toast.makeText( QuestionScreen.this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}

break;
}

}
};
}


Logcat



03-01 16:12:48.585 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy W/dalvikvm﹕ VFY: unable to resolve virtual method 11360: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy W/dalvikvm﹕ VFY: unable to resolve virtual method 11366: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy W/dalvikvm﹕ VFY: unable to resolve virtual method 9054: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy W/dalvikvm﹕ VFY: unable to resolve virtual method 367: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy W/dalvikvm﹕ VFY: unable to resolve virtual method 389: Landroid/content/res/TypedArray;.getType (I)I
03-01 16:12:48.685 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-01 16:12:48.855 1933-1933/com.example.requiem.bdukssstudy D/﹕ HostConnection::get() New Host Connection established 0xb97e2a80, tid 1933
03-01 16:12:49.175 1933-1933/com.example.requiem.bdukssstudy W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-01 16:12:49.175 1933-1933/com.example.requiem.bdukssstudy D/OpenGLRenderer﹕ Enabling debug mode 0
03-01 16:12:58.255 1933-1933/com.example.requiem.bdukssstudy E/BDUKSSS﹕ Starting Shift
03-01 16:12:58.365 1933-1933/com.example.requiem.bdukssstudy E/BDUKSSS﹕ Setting Audio Stream
03-01 16:12:58.365 1933-1933/com.example.requiem.bdukssstudy E/BDUKSSS﹕ Crash check #1
03-01 16:12:58.365 1933-1933/com.example.requiem.bdukssstudy E/BDUKSSS﹕ Prepare started
03-01 16:12:58.365 1933-1933/com.example.requiem.bdukssstudy E/MediaPlayer﹕ prepareAsync called in state 1
03-01 16:12:58.365 1933-1933/com.example.requiem.bdukssstudy D/AndroidRuntime﹕ Shutting down VM
03-01 16:12:58.365 1933-1933/com.example.requiem.bdukssstudy W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0d7ab20)
03-01 16:12:58.385 1933-1933/com.example.requiem.bdukssstudy D/dalvikvm﹕ GC_FOR_ALLOC freed 189K, 9% free 3150K/3448K, paused 14ms, total 17ms
03-01 16:12:58.395 1933-1933/com.example.requiem.bdukssstudy E/AndroidRuntime﹕ FATAL EXCEPTION: main



Aucun commentaire:

Enregistrer un commentaire