Want to play sound on viewpager Swipe(想要在 viewpager 上播放声音 滑动)
问题描述
我正在开发 android 应用程序,该应用程序可以在图像滑动时播放背景声音.我经历了多个教程&找到代码 &我尝试了同样的方法,但它对我不起作用.这是我尝试过的代码..
i am working on android app which can play background sound whenever image is swipes. I have gone through multiple tutorials & found codes & i tried for the same but it won't work for me. Here is my code which i tried..
package com.android.learning_numbers;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class Numbers extends Activity {
private static final OnPageChangeListener change = null;
public static MediaPlayer mp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(change);}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.sun,R.drawable.sky,R.drawable.moon,};
private int[] mAudio= new int[]{R.raw.sun,R.raw.sky,R.raw.moon,};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Numbers.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
public OnPageChangeListener change = new OnPageChangeListener() {
@Override public void onPageSelected(int position) {
Log.d(ACTIVITY_SERVICE, "OnPageSelected");
if
(mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Numbers.this, mAudio[position]);
mp.start();
}
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override public void onPageScrollStateChanged(int state) {}
};
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
我认为这里的声音不是由图像滑动类调用的.我的代码有错误吗?因为这里 OnPageChangeListener 根本不适合我.解决方案是什么?如何修改适合我的声音代码?
As i think here sound is not called by class on image swipe. Is there any error in my code? Because here OnPageChangeListener is not working for me at all. What is the solution for this? How do i modify my sound code that works for me?
推荐答案
试试这个代码 &根据您的需要进行更改..它对我有用..
Try this code & make changes according to your needs.. It works for me..
public class Alphabets extends Activity {
MediaPlayer mp;
MediaPlayer BG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setOnPageChangeListener(player);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
BG = MediaPlayer.create(this, R.raw.jingle);
BG.setLooping(true);
BG.setVolume(100, 100);
BG.start();
mp = MediaPlayer.create(Alphabets.this, mAudio[0]);
mp.start();
}
@Override
protected void onPause() {
super.onPause();
BG.pause();
}
@Override
protected void onResume() {
super.onResume();
BG.start();
}
private int[] mAudio = new int[]{R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,
R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,};
private int imageArra[] = {R.drawable.ab,R.drawable.bc,R.drawable.cd,R.drawable.de,R.drawable.ef,R.drawable.fg,R.drawable.gh,R.drawable.hi,R.drawable.ij,R.drawable.jk,R.drawable.kl,R.drawable.lm,R.drawable.mn,
R.drawable.no,R.drawable.op,R.drawable.pq,R.drawable.qr,R.drawable.rs,R.drawable.st,R.drawable.tu,R.drawable.uv,R.drawable.vw,R.drawable.wx,R.drawable.xy,R.drawable.yz,R.drawable.zz};
public OnPageChangeListener player = new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg1)
{
mp = MediaPlayer.create(Alphabets.this, mAudio[arg1]);
mp.stop();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.numbers, menu);
return true;
}
}
这篇关于想要在 viewpager 上播放声音 滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:想要在 viewpager 上播放声音 滑动
- GPS状态的广播接收器? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- UITextView 内容插图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01