如何在点击按钮播放声音 [英] how to play a sound on click of button

查看:119
本文介绍了如何在点击按钮播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图上点击按钮的模拟器中播放声音文件,但我得到的消息应用程序播放音频意外停止

我的codeS是:

 包com.java4u.android;

进口android.app.Activity;
进口android.content.Intent;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;

公共类P1延伸活动{


//创建媒体播放器的实例
MediaPlayer的MP = MediaPlayer.create(这一点,R.raw.meow);
@覆盖
保护无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    按钮btnSound =(按钮)this.findViewById(R.id.playSound);

    btnSound.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
        startActivity(新的意向书(com.java4u.android.p1));
            mp.start();
        }
    });

        }

}
 


的Manifest.xml

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.java4u.android
      安卓版code =1
      机器人:VERSIONNAME =1.0>
    <使用-SDK安卓的minSdkVersion =8/>

    <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
        <活动机器人:P1名称=机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;

    < /用途>
< /舱单>
 

main.xml中

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
    <按钮的android:文本=播放声音
    机器人:ID =@ + ID / playSound
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>
    < /按钮>
< / LinearLayout中>
 

解决方案

试试这个:

 按钮BTN1 =(按钮)findViewById(R.id.xBtn1);

btn1.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        最后的MediaPlayer MP1 = MediaPlayer.create(getBaseContext(),R.raw.fruit_dance);
        mp1.start();
    }
}
 

i am trying to play sound file on the emulator on click of button but i am getting message "application play audio has stopped unexpectedly"

my codes are:

package com.java4u.android;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public  class p1 extends Activity {


// creating instance of media player
MediaPlayer mp=MediaPlayer.create(this,R.raw.meow);
@Override
protected void onCreate(Bundle savedInstanceState) 
{    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnSound=(Button)this.findViewById(R.id.playSound);

    btnSound.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        startActivity(new Intent("com.java4u.android.p1")); 
            mp.start();
        }
    }); 

        }

}


Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.java4u.android"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".p1" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:text="play audio" 
    android:id="@+id/playSound" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    </Button>
</LinearLayout>

解决方案

Try this:

Button btn1=(Button)findViewById(R.id.xBtn1);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.fruit_dance);  
        mp1.start();
    }
}

这篇关于如何在点击按钮播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆