MediaPlayer 无法正常工作 (Android)?
MediaPlayer not working properly (Android)?
我正在尝试在 android 中制作一个简单的音板。我几乎完成了困难的部分,但是在完成应用程序之前我还需要解决一个问题。
我创建了一个嵌套的 switch 语句,它通过它的 id 识别 ImageView,在那个 switch 语句中(所以假设 "case R.id.prank")我告诉应用程序当你按下 ImageView 时要做什么,动作启动当动作停止时。
现在我对 ImageViews 或任何东西没有任何问题,我认为它与媒体播放器有关,但我不是 100% 确定。基本上当我按下第一个 ImageView 时会发生什么,第一个和第二个声音播放。当我按下第二个按钮时,只播放第二个声音。
我想让第一个ImageView播放第一个声音,第二个ImageView播放第二个声音。有人知道我做错了什么吗?
public class MainActivity extends AppCompatActivity implements OnTouchListener {
ImageView bmw_image;
ImageView prank;
MediaPlayer bmw_mp, prank_mp;
int bmw_id;
int prank_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prank = (ImageView) findViewById(R.id.prank);
bmw_image = (ImageView) findViewById(R.id.bmw_image);
bmw_mp = MediaPlayer.create(this, R.raw.dikke_bmw_jongens_cut);
prank_mp = MediaPlayer.create (this, R.raw.prank_cut);
bmw_image.setOnTouchListener(this);
prank.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.bmw_image:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
bmw_mp.start();
ImageView view = (ImageView) v;
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
case R.id.prank:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
prank_mp.start();
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
{
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
break;
}
return true;
}
}
你忘记为第一种情况写 break
所以在第一个声音之后第二个也会播放。
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
ImageView bmw_image;
ImageView prank;
MediaPlayer bmw_mp, prank_mp;
int bmw_id;
int prank_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prank = (ImageView) findViewById(R.id.prank);
bmw_image = (ImageView) findViewById(R.id.bmw_image);
bmw_mp = MediaPlayer.create(this, R.raw.dikke_bmw_jongens_cut);
prank_mp = MediaPlayer.create(this, R.raw.prank_cut);
bmw_image.setOnTouchListener(this);
prank.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.bmw_image: {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
bmw_mp.start();
ImageView view = (ImageView) v;
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
break;
}
case R.id.prank: {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
prank_mp.start();
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
break;
}
}
return true;
}
}
我正在尝试在 android 中制作一个简单的音板。我几乎完成了困难的部分,但是在完成应用程序之前我还需要解决一个问题。
我创建了一个嵌套的 switch 语句,它通过它的 id 识别 ImageView,在那个 switch 语句中(所以假设 "case R.id.prank")我告诉应用程序当你按下 ImageView 时要做什么,动作启动当动作停止时。
现在我对 ImageViews 或任何东西没有任何问题,我认为它与媒体播放器有关,但我不是 100% 确定。基本上当我按下第一个 ImageView 时会发生什么,第一个和第二个声音播放。当我按下第二个按钮时,只播放第二个声音。
我想让第一个ImageView播放第一个声音,第二个ImageView播放第二个声音。有人知道我做错了什么吗?
public class MainActivity extends AppCompatActivity implements OnTouchListener {
ImageView bmw_image;
ImageView prank;
MediaPlayer bmw_mp, prank_mp;
int bmw_id;
int prank_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prank = (ImageView) findViewById(R.id.prank);
bmw_image = (ImageView) findViewById(R.id.bmw_image);
bmw_mp = MediaPlayer.create(this, R.raw.dikke_bmw_jongens_cut);
prank_mp = MediaPlayer.create (this, R.raw.prank_cut);
bmw_image.setOnTouchListener(this);
prank.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.bmw_image:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
bmw_mp.start();
ImageView view = (ImageView) v;
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
case R.id.prank:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
prank_mp.start();
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
{
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
break;
}
return true;
}
}
你忘记为第一种情况写 break
所以在第一个声音之后第二个也会播放。
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
ImageView bmw_image;
ImageView prank;
MediaPlayer bmw_mp, prank_mp;
int bmw_id;
int prank_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prank = (ImageView) findViewById(R.id.prank);
bmw_image = (ImageView) findViewById(R.id.bmw_image);
bmw_mp = MediaPlayer.create(this, R.raw.dikke_bmw_jongens_cut);
prank_mp = MediaPlayer.create(this, R.raw.prank_cut);
bmw_image.setOnTouchListener(this);
prank.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.bmw_image: {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
bmw_mp.start();
ImageView view = (ImageView) v;
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
break;
}
case R.id.prank: {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
prank_mp.start();
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
view.getDrawable().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
break;
}
}
return true;
}
}