在我的Android应用程序开始一个新的声音之前停止prevoius声音 [英] Stop the prevoius sound before starting a new sound in my android app
问题描述
这code不工作..谁能帮助我。当我运行此code是给了一个错误,说部队密切application.I要开始一个新的声音结果
每次点击。在这里我使用的图像作为我的按钮的
包com.kle;进口java.io.IOException异常;进口com.kle.Kid.R;进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
//进口android.widget.Button;
进口android.widget.ImageView;
公共类十三扩展活动实现OnClickListener {INT标志= 1;
//按钮B1;
MediaPlayer的MP1,MP2,MP3,MP4,MP5,MP6,MP7,MP8,MP9,MP10;
ImageView的IV;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.thirteen); IV =(ImageView的)findViewById(R.id.imageView1); ImageView的此搜索=(ImageView的)findViewById(R.id.imageView1);
ImageView的IMAGE2 =(ImageView的)findViewById(R.id.imageView2);
ImageView的图像3 =(ImageView的)findViewById(R.id.imageView3);
ImageView的图片4 =(ImageView的)findViewById(R.id.imageView4);
ImageView的图像5 =(ImageView的)findViewById(R.id.imageView5);
ImageView的图片6 =(ImageView的)findViewById(R.id.imageView6);
ImageView的图片7 =(ImageView的)findViewById(R.id.imageView7);
ImageView的图片8 =(ImageView的)findViewById(R.id.imageView8);
ImageView的图片9 =(ImageView的)findViewById(R.id.imageView9);
ImageView的image10 =(ImageView的)findViewById(R.id.imageView10); // B1 =(按钮)findViewById(R.id.button1);
MP1 = MediaPlayer.create(Thirteen.this,R.raw.one);
MP2 = MediaPlayer.create(Thirteen.this,R.raw.two);
MP3 = MediaPlayer.create(Thirteen.this,R.raw.three);
MP4 = MediaPlayer.create(Thirteen.this,R.raw.four);
MP5 = MediaPlayer.create(Thirteen.this,R.raw.five);
MP6 = MediaPlayer.create(Thirteen.this,R.raw.six);
MP7 = MediaPlayer.create(Thirteen.this,R.raw.seven);
MP8 = MediaPlayer.create(Thirteen.this,R.raw.eight);
MP9 = MediaPlayer.create(Thirteen.this,R.raw.nine);
MP10 = MediaPlayer.create(Thirteen.this,R.raw.ten);
image1.setOnClickListener(本);
image2.setOnClickListener(本);
image3.setOnClickListener(本);
image4.setOnClickListener(本);
image5.setOnClickListener(本);
image6.setOnClickListener(本);
image7.setOnClickListener(本);
image8.setOnClickListener(本);
image9.setOnClickListener(本);
image10.setOnClickListener(本);
//b1.setOnClickListener(this);
}公共无效的onClick(视图v){
// TODO自动生成方法存根
开关(v.getId()){ 案例R.id.imageView1:{
iv.setImageResource(R.drawable.kidone);
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
mp6.stop();
mp7.stop();
mp8.stop();
mp9.stop();
mp10.stop(); 尝试{
。MP1 prepare();
}赶上(IllegalStateException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
mp1.seekTo(0);
mp1.start();
打破;
}
案例R.id.imageView2:{
iv.setImageResource(R.drawable.kidtwo);
mp1.stop();
mp3.stop();
mp4.stop();
mp5.stop();
mp6.stop();
mp7.stop();
mp8.stop();
mp9.stop();
mp10.stop(); 尝试{
。MP2 prepare();
}赶上(IllegalStateException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
mp2.seekTo(0);
mp2.start(); 打破;
}
案例R.id.imageView3:{
iv.setImageResource(R.drawable.kidthree); mp2.stop();
mp1.stop();
mp4.stop();
mp5.stop();
mp6.stop();
mp7.stop();
mp8.stop();
mp9.stop();
mp10.stop(); 尝试{
。MP3 prepare();
}赶上(IllegalStateException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
mp3.seekTo(0);
mp3.start(); 打破;
}
首先,您必须检查你的logcat的例外情况,这样它会更容易为你找到问题的根源。
现在,您的问题:你停止这是不是在玩一个媒体播放器...
停止媒体播放器前添加一个检查:
私人无效stopPlaying(MediaPlayer的MP)
{
如果(MP =空&安培;!&安培; mp.isPlaying())
{
mp.stop();
}
}
和调用:
stopPlaying(MP1);
stopPlaying(MP2);
stopPlaying(MP3);
...
(虽然我会使用一个数组为这种编码preFER因为这将使code更具可读性,更容易维护 - 使用循环)。
This code is not working.. Can anyone help me out. when i had run this code is gave an error saying force close application.I want to start a new sound with
each click. here i am using images as my buttons.
package com.kle;
import java.io.IOException;
import com.kle.Kid.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.Button;
import android.widget.ImageView;
public class Thirteen extends Activity implements OnClickListener{
int flag=1;
//Button b1;
MediaPlayer mp1,mp2,mp3,mp4,mp5,mp6,mp7,mp8,mp9,mp10;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.thirteen);
iv=(ImageView) findViewById(R.id.imageView1);
ImageView image1=(ImageView) findViewById(R.id.imageView1);
ImageView image2=(ImageView) findViewById(R.id.imageView2);
ImageView image3=(ImageView) findViewById(R.id.imageView3);
ImageView image4=(ImageView) findViewById(R.id.imageView4);
ImageView image5=(ImageView) findViewById(R.id.imageView5);
ImageView image6=(ImageView) findViewById(R.id.imageView6);
ImageView image7=(ImageView) findViewById(R.id.imageView7);
ImageView image8=(ImageView) findViewById(R.id.imageView8);
ImageView image9=(ImageView) findViewById(R.id.imageView9);
ImageView image10=(ImageView) findViewById(R.id.imageView10);
//b1= (Button) findViewById(R.id.button1);
mp1= MediaPlayer.create(Thirteen.this, R.raw.one);
mp2= MediaPlayer.create(Thirteen.this, R.raw.two);
mp3= MediaPlayer.create(Thirteen.this, R.raw.three);
mp4= MediaPlayer.create(Thirteen.this, R.raw.four);
mp5= MediaPlayer.create(Thirteen.this, R.raw.five);
mp6= MediaPlayer.create(Thirteen.this, R.raw.six);
mp7= MediaPlayer.create(Thirteen.this, R.raw.seven);
mp8= MediaPlayer.create(Thirteen.this, R.raw.eight);
mp9= MediaPlayer.create(Thirteen.this, R.raw.nine);
mp10= MediaPlayer.create(Thirteen.this, R.raw.ten);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
image7.setOnClickListener(this);
image8.setOnClickListener(this);
image9.setOnClickListener(this);
image10.setOnClickListener(this);
//b1.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imageView1: {
iv.setImageResource(R.drawable.kidone);
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
mp6.stop();
mp7.stop();
mp8.stop();
mp9.stop();
mp10.stop();
try {
mp1.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp1.seekTo(0);
mp1.start();
break;
}
case R.id.imageView2: {
iv.setImageResource(R.drawable.kidtwo);
mp1.stop();
mp3.stop();
mp4.stop();
mp5.stop();
mp6.stop();
mp7.stop();
mp8.stop();
mp9.stop();
mp10.stop();
try {
mp2.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp2.seekTo(0);
mp2.start();
break;
}
case R.id.imageView3: {
iv.setImageResource(R.drawable.kidthree);
mp2.stop();
mp1.stop();
mp4.stop();
mp5.stop();
mp6.stop();
mp7.stop();
mp8.stop();
mp9.stop();
mp10.stop();
try {
mp3.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp3.seekTo(0);
mp3.start();
break;
}
First of all you must check your logcat for the exceptions so that it would be easier for you to find the source of the problem. Now for your problem: you're stopping a media player which isn't playing... Add a check before stopping the media player:
private void stopPlaying(MediaPlayer mp)
{
if (mp != null && mp.isPlaying())
{
mp.stop();
}
}
And call:
stopPlaying(mp1);
stopPlaying(mp2);
stopPlaying(mp3);
...
(Although I would prefer using an array for this kind of coding as it would make the code far more readable and easier to maintain - using loops).
这篇关于在我的Android应用程序开始一个新的声音之前停止prevoius声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!