SoundPool: sample 1 not READY - android

The problem SoundPool when I call it in fragments onCreateView, onStart or onResume application to slow down.
And when I try to get the ID by pressing the button, the sound does not appear Logcat writes "SoundPool: sample 1 not READY"
please help me,Any ideas? Thanks!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_carnivore, container, false);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Sound.createOldSoundPool();
} else {
Sound.createNewSoundPool();
}
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
mBearSound = Sound.loadSound(getContext(),"bear.wav");
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
mCatSound= Sound.loadSound(getContext(),"cat.wav");
mCheetahSound= Sound.loadSound(getContext(),"cheetah.wav");
mCoyoteSound= Sound.loadSound(getContext(),"coyote.wav");
mCrocodileSound= Sound.loadSound(getContext(),"crocodile.wav");
mDogSound= Sound.loadSound(getContext(),"dog.wav");
mFoxSound= Sound.loadSound(getContext(),"fox.mp3");
mHyenaSound= Sound.loadSound(getContext(),"hyena.wav");
mJaguarSound= Sound.loadSound(getContext(),"jaguar.wav");
mKittenSound= Sound.loadSound(getContext(),"kitten.wav");
mLeopardSound= Sound.loadSound(getContext(),"leopard.wav");
mLionSound= Sound.loadSound(getActivity(),"lion.wav");
mPuppySound= Sound.loadSound(getActivity(),"puppy.wav");
mRattleSnakeSound= Sound.loadSound(getActivity(),"rattlesnake.mp3");
mSnakeSound= Sound.loadSound(getActivity(),"snake.wav");
mTigerSound= Sound.loadSound(getActivity(),"tiger.wav");
mWolfSound= Sound.loadSound(getActivity(),"wolf.wav");
int idList[]={R.id.imageButtonAlligator,R.id.imageButtonBat,R.id.imageButtonBear,R.id.imageButtonBobCat,R.id.imageButtonCat,
R.id.imageButtonCheetah,R.id.imageButtonCoyote,R.id.imageButtonCrocodile,R.id.imageButtonDog,R.id.imageButtonFox,
R.id.imageButtonHyena,R.id.imageButtonJaguar,R.id.imageButtonKitten,R.id.imageButtonLeopard,R.id.imageButtonLion,
R.id.imageButtonPuppy,R.id.imageButtonRattlesnake,R.id.imageButtonSnake,R.id.imageButtonTiger,R.id.imageButtonWolf};
for (int id :idList){
ImageButton imageButton = (ImageButton)view.findViewById(id);
imageButton.setOnClickListener(onClickListener);
}
return view;
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonAlligator:
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCheetah:
Sound.playSound(mCheetahSound);
Toast.makeText(view.getContext(),R.string.Cheetah, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCoyote:
Sound.playSound(mCoyoteSound);
Toast.makeText(view.getContext(),R.string.Coyote, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCrocodile:
Sound.playSound(mCrocodileSound);
Toast.makeText(view.getContext(),R.string.Crocodile, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonDog:
Sound.playSound(mDogSound);
Toast.makeText(view.getContext(),R.string.Dog, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonFox:
Sound.playSound(mFoxSound);
Toast.makeText(view.getContext(),R.string.Fox, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonHyena:
Sound.playSound(mHyenaSound);
Toast.makeText(view.getContext(),R.string.Hyena, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonJaguar:
Sound.playSound(mJaguarSound);
Toast.makeText(view.getContext(),R.string.Jaguar, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonKitten:
Sound.playSound(mKittenSound);
Toast.makeText(view.getContext(),R.string.Kitten, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLeopard:
Sound.playSound(mLeopardSound);
Toast.makeText(view.getContext(),R.string.Leopard, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLion:
Sound.playSound(mLionSound);
Toast.makeText(view.getContext(),R.string.Lion, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonPuppy:
Sound.playSound(mPuppySound);
Toast.makeText(view.getContext(),R.string.Puppy, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonRattlesnake:
Sound.playSound(mRattleSnakeSound);
Toast.makeText(view.getContext(),R.string.RattleSnake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonSnake:
Sound.playSound(mSnakeSound);
Toast.makeText(view.getContext(),R.string.Snake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonTiger:
Sound.playSound(mTigerSound);
Toast.makeText(view.getContext(),R.string.Tiger, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonWolf:
Sound.playSound(mWolfSound);
Toast.makeText(view.getContext(),R.string.Wolf, Toast.LENGTH_SHORT).show();
break;
}
}
};
When you click on the sound does not work when the method is called Sound.loadSound (..., ...), and Logcat writes "SoundPool: sample 1 not READY"
switch (v.getId()) {
case R.id.imageButtonAlligator:
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
mBearSound = Sound.loadSound(getContext(),"bear.wav");
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
mCatSound= Sound.loadSound(getContext(),"cat.wav");
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
this is th Static Method Sound
public class Sound {
public static SoundPool mSoundPool;
public static AssetManager mAssetManager;
public static int mStreamID;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void createNewSoundPool() {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
}
#SuppressWarnings("deprecation")
public static void createOldSoundPool() {
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}
public static int playSound(int sound) {
if (sound > 0) {
mStreamID = mSoundPool.play(sound, 1, 1, 1, 0, 1);
}
return mStreamID;
}
public static int loadSound(Context context , String fileName) {
mAssetManager = context.getAssets();
AssetFileDescriptor afd;
try {
afd = mAssetManager.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Не могу загрузить файл " + fileName,
Toast.LENGTH_SHORT).show();
return -1;
}
return mSoundPool.load(afd, 1);
}
}

Before doing anything with the soundpool, after you instantiate it, you need to set a listener to know when its actually ready to be used, it takes time to load the sound into memory: do this:
boolean loaded = false;//class variable
//in onCreate:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
then before you use the soundPool you need to check if Loaded is true.
For example:
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}

Related

Headset buttons controls

Now what is happening is it is also controlling other music players. If i have a default music player after starting that if i start playing using my music player and then i press play/pause button in headset it controls default music player.
i have implemented a heaset broadcastreceiver like this,
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
Log.d("TAG", "TAG: KEYCODE_HEADSETHOOK");
if (MusicService.isPlaying()) {
MusicService.player.pause();
} else {
MusicService.player.start();
}
new MusicService().showNotification();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
Log.d("TAG", "TAG: KEYCODE_MEDIA_PLAY");
if (MusicService.isPlaying()) {
MusicService.player.pause();
} else {
MusicService.player.start();
}
new MusicService().showNotification();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
Log.d("TAG", "TAG: KEYCODE_MEDIA_PAUSE");
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.d("TAG", "TAG: KEYCODE_MEDIA_NEXT");
if (MusicService.player.isPlaying()) {
MusicService.player.stop();
}
if (MusicService.songPosn < (MusicService.song.size() - 1)) {
MusicService.songPosn = MusicService.songPosn + 1;
if (SingleInstance.getInstance().getIndexCount()>= 1)
SingleInstance.getInstance().setIndexCount(SingleInstance.getInstance().getIndexCount()-1);
} else {
// play first song
MusicService.songPosn = 0;
}
new MusicService().playSong(true);
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.d("TAG", "TAG: KEYCODE_MEDIA_PREVIOUS");
if (MusicService.player.isPlaying()) {
MusicService.player.stop();
}
if (MusicService.songPosn > 0) {
MusicService.songPosn = MusicService.songPosn - 1;
} else {
// play last song
MusicService.songPosn = MusicService.song.size() - 1;
}
new MusicService().playSong(true);
break;
}
If you are trying to get the hedsethook event in a activity follow the below code :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
// Headset key detected
// Do your stuff here
return true;// To let the os know that the event is consumed here
}
}
return super.dispatchKeyEvent(event);
}
Make sure to returt true to consume the event .To get it done in a service use BroadCast receiver as follows:
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
filter.setPriority(1000); //Sets receiver priority
registerReceiver(r, filter);

How to change background of button on next click

Currently I have a list of TextViews whose background get changed when click for the first time. But I also want when user clicks on the second time its background should again change and vice versa.
With my existing code I am only able to change background for the first click, when I am clicking it again its background is not changing.
Here is my code:
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (count <= 2) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText2:
if (count <= 2) {
goals.add(mGoal2.getText().toString());
mGoal2.setTextColor(getResources().getColor(R.color.black));
mGoal2.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText3:
if (count <= 2) {
goals.add(mGoal3.getText().toString());
mGoal3.setTextColor(getResources().getColor(R.color.black));
mGoal3.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText4:
if (count <= 2) {
goals.add(mGoal4.getText().toString());
mGoal4.setTextColor(getResources().getColor(R.color.black));
mGoal4.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText5:
if (count <= 2) {
goals.add(mGoal5.getText().toString());
mGoal5.setTextColor(getResources().getColor(R.color.black));
mGoal5.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText6:
if (count <= 2) {
goals.add(mGoal6.getText().toString());
mGoal6.setTextColor(getResources().getColor(R.color.black));
mGoal6.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText7:
if (count <= 2) {
goals.add(mGoal7.getText().toString());
mGoal7.setTextColor(getResources().getColor(R.color.black));
mGoal7.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnGoal:
Intent intent = new Intent(this, fiteness_level_selection.class);
try {
obj.put("SelectedGoal", goals);
} catch (Exception e) {
e.printStackTrace();
}
intent.putExtra("GoalJson", obj.toString());
startActivity(intent);
break;
}
So can anybody suggest me any easy way to achieve it.
Set a boolean array in your activity.
//suppose you've 3 textviews within listview
public boolean isTransparent[] = {false,false,false};
And then do this in your onClick method.
public void onOnclick(View v){
switch (v.getId()) {
//foreach textview do this
case R.id.textView1 :
int color;
if (isTransparent[0])
color = getResources().getColor(android.R.color.transparent);
else
color = getResources().getColor(R.color.white);
isTransparent[0]=!isTransparent[0];
textview1.setBackgroundColor(color);
break;
case R.id.textView2:
// now check for isTransparent[1]
and so on ...
...
}
}
As What I am understand is you want Change background for that you have take bool variable and in Button click you have to check the bool value.
Boolean click_firsttime = false;
Inside your Button click
use this way
If(click_firsttime == false){
//perform task
// and set bool value to true
click_firsttime = true;
}else{
// perform task and set bool value
click_firsttime = false;
}
First create a boolean :
private boolean foo = true;
Then change your code like this :
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (foo) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
foo = false;
} else {
// set background to another color
foo = true;
}
break;

assign null to a class static variable cause NullPointerException

public class ClassHoler {
public static List<Car> carsHolder;
}
In an Activity I used like this.
#Override
public void onClick(View v) {
ClassHoler.carsHolder = null;
}
In my app I got a java.lang.NullPointerException on ClassHoler.carsHolder = null;
How could be this ?
LogCat:
java.lang.NullPointerException
com.souche.fengche.ui.activity.workbench.customer.AddCustomerActivity.void onClick(android.view.View)(SourceFile:282)
android.view.View.performClick(View.java:4472)
android.view.View$PerformClick.run(View.java:18795)
android.os.Handler.handleCallback(Handler.java:808)
android.os.Handler.dispatchMessage(Handler.java:103)
android.os.Looper.loop(Looper.java:193)
android.app.ActivityThread.main(ActivityThread.java:5332)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
dalvik.system.NativeStart.main(Native Method)
Source File:
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(name.getWindowToken(), 0);
switch (v.getId()) {
case R.id.gentle_man:
check(gentleManRadio);
unCheck(ladyRadio);
break;
case R.id.lady:
check(ladyRadio);
unCheck(gentleManRadio);
break;
case R.id.source:
getSources();
mPicker.showAtLocation(parent, Gravity.CENTER, 0, 0);
break;
case R.id.customer_requirement_remark:
Intent intent = new Intent(this, AddRequireRemarkActivity.class);
intent.putExtra(Constant.USER_ID, userID);
startActivityForResult(intent, ADD_REMARK);
break;
case R.id.yes:
check(yesRadio);
unCheck(noRadio);
watchedCars.setVisibility(View.VISIBLE);
line.setVisibility(View.VISIBLE);
break;
case R.id.no:
check(noRadio);
unCheck(yesRadio);
watchedCars.setVisibility(View.GONE);
line.setVisibility(View.GONE);
break;
case R.id.watched_cars:
intent = new Intent(this, AddWatchedCarActivity.class);
intent.putExtra(Constant.CAR_ID, mWatchedCarsString);
startActivity(intent);
break;
case R.id.level:
getLevels();
break;
case R.id.next_visit_time:
mSelectDate.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
break;
case R.id.save_my_cusotmer:
if (name.getText().toString().trim().length() > 5) {
name.setError("名字不能超过五个字");
return;
}
//clear class holder
282 ------> ClassHoler.carsHolder = null;
mLoadingDialog.show();
saveMyCustomer();
break;
}
}
only use is here:
private void initView() {
mLoadingDialog = new LoadingDialog(this);
List<Car> cars;
if (ClassHoler.carsHolder == null) {
cars = new ArrayList<>();
} else {
cars = ClassHoler.carsHolder;
ClassHoler.carsHolder = null;
}
}
Is there some extreme condition my code got NullPointerException ?

handle media player error gracefully

I am trying to handle a media player error, when the error would occur, I want to toast a message and then play next song , however whenever the error occurs it would just crash.
here is the code I am trying
public class player1 extends ActionBarActivity implements View.OnClickListener,OnErrorListener{
MediaPlayer mp;
ArrayList<File> mySongs;
int position;
Uri u;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlist");
position = b.getInt("pos", 0);
u = Uri.parse(Uri.encode(mySongs.get(position).toString()));
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errorWhat;
switch(what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errorWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errorWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default:
errorWhat = "!";
}
String errorExtra;
switch(extra){
case MediaPlayer.MEDIA_ERROR_IO:
errorExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errorExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errorExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errorExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errorExtra = "!";
}
Toast.makeText(player1.this,
"Error" + "\n"
+ errorWhat + "\n"
+ errorExtra,
Toast.LENGTH_LONG).show();
//release
mp.stop();
mp.release();
initMediaPlayer();
return true;
};
private void initMediaPlayer() {
Toast.makeText(player1.this,
"playomng next song",
Toast.LENGTH_LONG).show();
position = (position + 1)%mySongs.size();
u = Uri.parse(Uri.encode(mySongs.get(position).toString()));
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
}
}
How can I manage to handle these errors without forcing the media player to close?
Rather than just putting a band aid on your code it may be wiser to find where the actual problem is. If you can find what line(s) you're receiving an error you can simply wrap the code in a
try{
//code that crashes here
}
catch(Exception e){
Toast.makeText(context, "Media error", LENGTH_SHORT).show();
}
EDIT:
Be aware that simply preventing a crash via a try/catch can cause bugs itself. If you can post your logcat we may be able to figure out what your actual error is.
Without logcat we can not say much, however you did not set the
player to listen to the errors mp.setOnErrorListener(this);.
I think your crash might be i.getExtras(); it can be null, therefore calling b.getParcelableArrayList("songlist"); might just crash your application.

Long-press followed by swipe on different controls in Android app

I have an image button which has a click and a long click hooked up to it. On a long-press I display a custom vertical seek bar. All the functionality I need is fully functional with the exception of one requirement. I need to be able to start a swipe-up gesture after long pressing the button without lifting my finger. The slide gesture should operate on my vertical seek bar. I can already do this but I need to lift my finger after long pressing the button. Here's what I have so far
public class MainActivity : Activity
{
private ImageButton m_jump1;
private VerticalSeekBar m_seekUp;
private TextView m_progress;
private string[] m_values;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
m_values = new[] { "-5 Sec", "-15 Sec", "-1 Min", "-5 Min", "-15 Min", "-30 Min", "-1 H", "-6 H" };
m_seekUp = FindViewById<VerticalSeekBar>(Resource.Id.SliderUp);
m_seekUp.ProgressChanged += OnProgressChanged;
m_seekUp.Touch += OnTouch;
m_jump1 = FindViewById<ImageButton>(Resource.Id.Jump1);
m_jump1.Click += OnJump;
m_jump1.LongClick += OnShowSliderUp;
m_progress = FindViewById<TextView>(Resource.Id.ProgressTextView);
m_progress.Text = m_values[m_seekUp.Progress];
}
public override bool OnTouchEvent(MotionEvent e)
{
if (e.Action == MotionEventActions.Down)
{
if (m_seekUp.Visibility == ViewStates.Visible)
{
var rect = new Rect(m_seekUp.Left, m_seekUp.Top, m_seekUp.Right, m_seekUp.Bottom);
if (!rect.Contains((int)e.RawX, (int)e.RawY))
{
m_seekUp.Progress = 1;
m_seekUp.Visibility = ViewStates.Invisible;
m_progress.Visibility = ViewStates.Invisible;
}
}
}
return base.OnTouchEvent(e);
}
private void OnJump(object sender, EventArgs args)
{
if (m_seekUp.Visibility == ViewStates.Visible)
{
m_seekUp.Visibility = ViewStates.Invisible;
m_progress.Visibility = ViewStates.Invisible;
}
}
private void OnShowSliderUp(object sender, EventArgs args)
{
if (m_seekUp.Visibility == ViewStates.Invisible)
{
m_seekUp.Visibility = ViewStates.Visible;
m_progress.Visibility = ViewStates.Visible;
}
}
private void OnTouch(object sender, View.TouchEventArgs e)
{
switch (e.Event.Action)
{
case MotionEventActions.Up:
m_seekUp.Progress = 1;
m_seekUp.Visibility = ViewStates.Invisible;
m_progress.Visibility = ViewStates.Invisible;
break;
case MotionEventActions.Move:
e.Handled = false;
break;
case MotionEventActions.Down:
e.Handled = false;
break;
}
}
private void OnProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
{
switch (e.Progress)
{
case 0:
m_progress.Text = m_values[e.Progress];
break;
case 1:
m_progress.Text = m_values[e.Progress];
break;
case 2:
m_progress.Text = m_values[e.Progress];
break;
case 3:
m_progress.Text = m_values[e.Progress];
break;
case 4:
m_progress.Text = m_values[e.Progress];
break;
case 5:
m_progress.Text = m_values[e.Progress];
break;
case 6:
m_progress.Text = m_values[e.Progress];
break;
case 7:
m_progress.Text = m_values[e.Progress];
break;
}
}
}
I am working with Xamarin but any answers in Java will work too, I will convert the code myself

Categories

Resources