How to play short sounds while swiping pages ? - android

I have problem with ViewPager and can't find answer on this site or via a Google search.How to play short sounds while swiping pages? How can I change my code, I want to add mediaplayer/Play sound . (The sounds in the raw folder) Thanks,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

Use SoundPool. It is specifically designed to play short sounds (esp in games, multimedia apps).
here is a tutorial - http://content.gpwiki.org/index.php/Android:Playing_Sound_Effects_With_SoundPool

An example:
private static final int rId = R.raw.sound;
private int sid=-1;
private boolean loaded = false;
SoundPool soundPool;
private SoundPool.OnLoadCompleteListener listener =
new SoundPool.OnLoadCompleteListener(){
#Override
public void onLoadComplete(SoundPool soundPool, int sid, int status){ // could check status value here also
if (this.sid == sid) {
this.loaded = true;
}
}
};
public void initSound() {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
soundPool.setOnLoadCompleteListener(listener);
this.sid = soundPool.load(getApplicationContext(), rId, 1);
}
public void SoundPlay() {
new Thread(new Runnable() {
#Override
public void run() {
if (loaded) {
soundPool.play(this.sid, 1.0, 1.0, 1, 0, 1f);
}
}).start();
}
So you would add a constructor like:
MyPagerAdapter() {
initSound();
}
and to play each time in instantiateItem() with SoundPlay();
I have not tested the above code as is, but use similar in my own stuff.

Related

Limit area of screen for swipe gesture detection

I am using gesture detection to swipe between HUD views containing objects like seekbars overlaid on my app screen.
Is there a way to limit or set a portion of the screen to listen for detect gestures.
I am using a Custom ViewPager that looks like this.
main class.
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends Activity {
static GLSurfaceView mView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = (GLSurfaceView)findViewById(R.id.glview);
mView.setZOrderOnTop(false);
mView.setEGLContextClientVersion(2);
mView.setRenderer(new GLLayer(this));
mView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
mView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // use with mView.requestRender() for each user change
// Create and set adapter
CustomPagerAdapter adapter = new CustomPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
// Add page change listener to pager component
myPager.setOnPageChangeListener(new CustomPageChangeListener(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
customPageChangeListener
public class CustomPageChangeListener implements OnPageChangeListener, OnClickListener {
private Button buttonBlue = null;
private Button buttonYellow = null;
private Button buttonRed = null;
private ColorPicker colorPicker;
private Activity context = null;
/**
* Constructor
*/
public CustomPageChangeListener(Activity context) {
this.context = context;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0 : {
buttonBlue = (Button) context.findViewById(R.id.buttonBlue);
buttonBlue.setOnClickListener(this);
colorPicker = (ColorPicker) context.findViewById(R.id.colorPicker1);
// other controls
break;
}
case 1 : {
buttonYellow = (Button) context.findViewById(R.id.buttonYellow);
buttonYellow.setOnClickListener(this);
//other controls
break;
}
case 2 : {
buttonRed = (Button) context.findViewById(R.id.buttonRed);
buttonRed.setOnClickListener(this);
// other controls
break;
}
}
}
and the adapter...
public class CustomPagerAdapter extends PagerAdapter {
public Object instantiateItem(final View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.page_1;
break;
}
case 1: {
resId = R.layout.page_2;
break;
}
case 2: {
resId = R.layout.page_3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public int getCount() {
return 3;
}
Many thanks.
First idea.
Try to watch your x and y value :
x = event.getX();
y = event.getY();
cut gestures on 100px on horisontaly:
if(x>100) return false;
else
return viewPager.onTouchEvent(event);

GallaryView change Images on button Click a

Currently I am working with GalleryView and changing images that is working perfectly fine.
But I want to disable swipe of GalleryView and change the loaded images on button click.
How can i achieve this functionality.
My current Class is As Follow
public class PopUpWindow extends Activity {
TextView closebtn;
Integer[] pics = {
R.drawable.account_icon,
R.drawable.coffee_icon,
R.drawable.help_icon,
R.drawable.menu_icon,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setUpDialogProperties();
this.setFinishOnTouchOutside(false);
setContentView(R.layout.pop_up_dialog);
fetchUI();
}
private void fetchUI(){
closebtn = (TextView)findViewById(R.id.okBtn);
closeDialog();
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
ga.setUnselectedAlpha(0.0f);
}
private void closeDialog(){
closebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void setUpDialogProperties() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
android.view.WindowManager.LayoutParams lp = getWindow()
.getAttributes();
lp.gravity = Gravity.BOTTOM;
getWindow().setAttributes(lp);
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return pics.length;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setLayoutParams(new Gallery.LayoutParams(150,150));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
Thanks in advance
case R.id.leftButton:
int position = mGallery.getSelectedItemPosition() - 1;
if (position < 0)
return;
mGallery.setSelection(position);
break;
case R.id.rightButton:
position = mGallery.getSelectedItemPosition() + 1;
if (position >= mGallery.getCount())
return;
mGallery.setSelection(position);
break;
I will setup the onclick for your next and previous button

How to refer to activity element from viewpager

I try to write an app, that is using slide effect for activities and found that ViewPager from Compatibility Package is what I need. I successfully made the slide, it works in the way i want, but my problem is that I am not able to refer to the elements in my Activity.
This is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
if (Fibonacci.getInstance().number != 0)
displayResults();
ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
btnCalculate.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do button click
}
});
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.activity_dark;
break;
case 1:
resId = R.layout.activity_pale;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
My problem is that I receive NullPointerEception for the following line:
ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
I know it's because in setContentView I set up main.xml, and I want to refer to an element in activity_dark.xml or activity_pale.xml. Unfortunately i am not able to figure out how to refer to the element. Any hints would be appreciated! Thank you!
You should better use Fragments for this
http://developer.android.com/guide/components/fragments.html
but if you want do it like this, then try to set listener after page changed:
myPager.setOnPageChangeListener(new OnPageChangeListener(){
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0) {
ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
btnCalculate.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do button click
}
});
}
});

Viewpager not working correctly

I want to show message and play sound on viewpager when page selected. I am seeing next one message.If page selected (Picture1 - Sound2, Picture2 - Sound3...)
I want to play sound when page selected (Picture1-Sound1, Picture2-Sound2,....) Where is the problem?
Thanks,
Viewpager;
package com.example.messages;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class Pictures extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picturespage);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
};
Mypageradapter;
package com.example.messages;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class MyPagerAdapter extends PagerAdapter{
SoundPool snd;
int sound1,sound2,sound3;
boolean loaded = false;
public int getCount() {
return 6;
}
public Object instantiateItem(View collection, int position) {
View view=null;
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
snd = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
snd.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
sound1 = snd.load(collection.getContext(), R.raw.sound1, 1);
sound2 = snd.load(collection.getContext(), R.raw.sound2, 1);
sound3 = snd.load(collection.getContext(), R.raw.sound3, 1);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.picture1;//
view = inflater.inflate(resId, null);
if (loaded) {
snd.play(sound1, 1, 1, 1, 0, 1f);
Toast.makeText(collection.getContext(), "Sound1", Toast.LENGTH_SHORT).show();
}
break;
case 1:
resId = R.layout.picture2; //
view = inflater.inflate(resId, null);
if (loaded) {
snd.play(sound2, 1, 1, 1, 0, 1f);
Toast.makeText(collection.getContext(), "Sound2", Toast.LENGTH_SHORT).show();
}
break;
case 2:
resId = R.layout.picture3;//
view = inflater.inflate(resId, null);
if (loaded) {
snd.play(sound3, 1, 1, 1, 0, 1f);
Toast.makeText(collection.getContext(), "Sound3", Toast.LENGTH_SHORT).show();
}
break;
case 3:
resId = R.layout.picture4;//
view = inflater.inflate(resId, null);
Toast.makeText(collection.getContext(), "Test4", Toast.LENGTH_SHORT).show();
break;
case 4:
resId = R.layout.picture5;//
view = inflater.inflate(resId, null);
Toast.makeText(collection.getContext(), "Test5", Toast.LENGTH_SHORT).show();
break;
case 5:
resId = R.layout.picture6;//
view = inflater.inflate(resId, null);
Toast.makeText(collection.getContext(), "Test6", Toast.LENGTH_SHORT).show();
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}
#SuppressWarnings("unused")
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
private void setVolumeControlStream(int streamMusic) {
// TODO Auto-generated method stub
}
#SuppressWarnings("unused")
private Context getBaseContext() {
// TODO Auto-generated method stub
return null;
}
#SuppressWarnings("unused")
private PagerAdapter findViewById(int myfivepanelpager) {
// TODO Auto-generated method stub
return null;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
public static Integer getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}
Please try set on a ViewPager setOnPageChangeListener and in onPageSelected you will be able to get concret page.

Pressing back button to viewpager

I have a viewpager working well, when I click on a view/page it opens a new activity, but when I want to press back to go to the viewpager the app closes. How would I get the viewpager to open properly to the current page when I press the back button?
I know it is supposed to so here is the viewpager code:
public class LogoQuizActivity extends Activity {
private
class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
//Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(currentPage);
myPager.setPadding(0, 150, 0, 0);
PageListener pageListener = new PageListener();
myPager.setOnPageChangeListener(pageListener);
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null; // this comes first
int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
view = inflater.inflate(resId, null);
break;
case 1:
resId = R.layout.left;
view = inflater.inflate(resId, null);
break;
case 2:
resId = R.layout.middle;
view = inflater.inflate(resId, null);
break;
case 3:
resId = R.layout.right;
view = inflater.inflate(resId, null);
break;
case 4:
resId = R.layout.farright;
view = inflater.inflate(resId, null);
break;
}
view.setTag(position);
view.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int position = (Integer) view.getTag();
Log.v("Log_tag", "Here image is clicked" + position);
if(position == 0){
Intent intent = new Intent(LogoQuizActivity.this,
LevelOne.class);
startActivity(intent);
}
if(position == 1){
Intent intent = new Intent(getBaseContext(),
LevelTwo.class);
startActivity(intent);
}
if(position == 2){
Intent intent = new Intent(getBaseContext(),
LevelThree.class);
startActivity(intent);
}
if(position == 3){
Intent intent = new Intent(getBaseContext(),
LevelFour.class);
startActivity(intent);
}
if(position == 4){
Intent intent = new Intent(getBaseContext(),
LevelFive.class);
startActivity(intent);
}
}
});
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
#Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
#Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
}
I'm not sure if it's to do with restore state method or return null in parcelable savestate. It could be completely obvious so thanks in advance!
Solved this by using FLAG_ACTIVITY_CLEAR_TOP on my second and third activities after I call the new intent.

Categories

Resources