new android dev hopeful here. I've been attempting to implement an app that, upon holding down the "ok" button, it will play the default ringtone in a loop.
What I have so far
package com.tick.helloAndroid;
import net.technologichron.android.control.NumberPicker;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
//import android.widget.TextView;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity implements OnTouchListener {
private int REQUEST_ENABLE_BT = 1;
private MediaPlayer mp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up the window layout
setContentView(R.layout.main);
final Button zero = (Button) this.findViewById(R.id.ok);
zero.setOnTouchListener(this);
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mp.setDataSource(this, alert);
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
case MotionEvent.ACTION_UP:
mp.pause();
}
return true;
}
}
From doing some searching on SO, this seems to work for some people, however, the line
mp.setDataSource(this, alert);
seems to not work. By not work, I mean that it forces an "Unhandled Exception Error", which upon doing a try-catch statement,
try {
mp.setDataSource(this, alert);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "args", Toast.LENGTH_LONG).show();
finish();
return;
} catch (SecurityException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "sec", Toast.LENGTH_LONG).show();
finish();
return;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "illega", Toast.LENGTH_LONG).show();
finish();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "io", Toast.LENGTH_LONG).show();
finish();
return;
}
compiles without any error but crashes when I try to run the app on the android emulator, it does not print any of the above catches' strings.
Any thoughts and ideas about what I have done incorrectly will be most appreciated.
edit: could it be from the fact that the emu does not have a ringtone?