播放声音按钮点击android

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

如何在点击时从原始播放声音?我只是创建了一个按钮 button1 ,但无论我写的是什么代码,都是错误的。

How do I get a button to play a sound from raw when click? I just created a button with id button1, but whatever code I write, all is wrong.

import android.media.MediaPlayer; public class BasicScreenActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_screen); } Button one = (Button)this.findViewById(R.id.button1); MediaPlayer = mp; mp = MediaPlayer.create(this, R.raw.soho); zero.setOnCliclListener(new View.OnClickListener() ) @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.basic_screen, menu); return true; } }

推荐答案

这是原始帖子中提供的代码中最重要的部分。

This is the most important part in the code provided in the original post.

Button one = (Button) this.findViewById(R.id.button1); final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho); one.setOnClickListener(new OnClickListener(){ public void onClick(View v) { mp.start(); } });

逐步解释:

Button one = (Button) this.findViewById(R.id.button1);

首先是用于播放声音的按钮的初始化。我们使用活动的 findViewById ,传递我们分配给它的Id(在这个例子中: R.id.button1 ),得到我们需要的按钮。我们把它作为一个 Button ,以便很容易将它分配给我们正在初始化的变量一个。解释更多的这如何工作超出了这个答案的范围。 此简要介绍了其工作原理。

First is the initialization of the button to be used in playing the sound. We use the Activity's findViewById, passing the Id we assigned to it (in this example's case: R.id.button1), to get the button that we need. We cast it as a Button so that it is easy to assign it to the variable one that we are initializing. Explaining more of how this works is out of scope for this answer. This gives a brief insight on how it works.

final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);

这是如何初始化 MediaPlayer 。 MediaPlayer遵循 Singleton设计模式。要获取实例,请将其 create() 方法,并传递上下文和我们想播放的声音的资源ID,在这种情况下 R.raw.soho 。我们将它声明为 final 。 Jon Skeet提供了一个很好的解释,为什么我们这样做这里。

This is how to initialize a MediaPlayer. The MediaPlayer follows the Singleton Design Pattern. To get an instance, we call its create() method and pass it the context and the resource Id of the sound we want to play, in this case R.raw.soho. We declare it as final. Jon Skeet provided a great explanation on why we do so here.

one.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //code } });

最后,我们设置我们之前初始化的按钮会做什么。播放声音按钮点击!为此,我们设置 OnClickListener 按钮一个。里面只有一个方法, onClick()其中包含按钮应该在点击 按钮。

Finally, we set what our previously initialized button will do. Play a sound on button click! To do this, we set the OnClickListener of our button one. Inside is only one method, onClick() which contains what instructions the button should do on click.

public void onClick(View v) { mp.start(); }

要播放声音,我们调用MediaPlayer的 start() 方法。

To play the sound, we call MediaPlayer's start() method. This method starts the playback of the sound.

在此,您现在可以在Android上点击按钮播放声音。

There, you can now play a sound on button click in Android!

首先。你应该把你的语句放在一个块中,在这种情况下是onCreate方法。

First. You should put your statements inside a block, and in this case the onCreate method.

第二。您将按钮初始化为变量一个,然后您使用一个变量并将其onClickListener设置为不完整的onClickListener。使用变量 one 作为setOnClickListener。

Second. You initialized the button as variable one, then you used a variable zero and set its onClickListener to an incomplete onClickListener. Use the variable one for the setOnClickListener.

第三,将逻辑放在onClick中播放声音。

Third, put the logic to play the sound inside the onClick.

总结:

import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class BasicScreenActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_screen); Button one = (Button)this.findViewById(R.id.button1); final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho); one.setOnClickListener(new OnClickListener(){ public void onClick(View v) { mp.start(); } }); } }

更多推荐

播放声音按钮点击android

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

发布评论

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

>www.elefans.com

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