如何在点击按钮播放声音

编程入门 行业动态 更新时间:2024-10-19 06:27:08
本文介绍了如何在点击按钮播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我的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/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/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="schemas.android/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="schemas.android/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(); } }

更多推荐

如何在点击按钮播放声音

本文发布于:2023-11-14 15:17:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587776.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   声音   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!