播放声音的按钮,点击机器人 [英] Play sound on button click android

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

问题描述

我试过很多教程,YouTube视频,#1的答案,他们没有工作。

你能帮助我吗?我如何获得一个按钮,从原材料播放声音时,点击?我刚刚创建了一个按钮,ID为按钮1 ,但无论code我写的,都是错误的。

 进口android.media.MediaPlayer;

公共类BasicScreenActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_basic_screen);
    }

    按钮一次=(按钮)this.findViewById(R.id.button1);
    MediaPlayer的= NP;
    MP = MediaPlayer.create(这一点,R.raw.soho);
    zero.setOnCliclListener(新View.OnClickListener())

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.basic_screen,菜单);
        返回true;
    }



}
 

解决方案

这是在原来的职位提供的code中的最重要的部分。

 按钮一次=(按钮)this.findViewById(R.id.button1);
最后的MediaPlayer MP = MediaPlayer.create(这一点,R.raw.soho);
one.setOnClickListener(新OnClickListener(){

    公共无效的onClick(视图v){
        mp.start();
    }
});
 

要解释它一步一步:

 按钮一次=(按钮)this.findViewById(R.id.button1);
 

首先是在播放声音中使用的按钮的初始化。我们使用活动的 findViewById ,传递我们赋予它的ID(在本例中的情况: R.id.button1 ),以获得我们所需要的按钮。我们投它作为一个按钮,这样很容易把它分配给变量一个,我们正在初始化。更多的解释了这是如何工作超出范围的这个答案。 给它如何工作的一个简短的洞察。

 最后MediaPlayer的MP = MediaPlayer.create(这一点,R.raw.soho);
 

这是如何初始化 的MediaPlayer 。在MediaPlayer符合 Singleton设计模式。为了得到一个实例,我们调用它的<一个href="http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context,%20int)"相对=nofollow> 创建() 方法并传递给它的背景下,我们要播放的声音的资源ID,在这种情况下, R.raw.soho 。我们将它声明为最后。乔恩斯基特提供的,为什么我们这里这样做一个很好的解释。

  one.setOnClickListener(新OnClickListener(){

    公共无效的onClick(视图v){
        // code
    }
});
 

最后,我们建立了我们的previously初始化按钮就行了。播放声音的按钮,点击!要做到这一点,我们设置 OnClickListener 我们的按钮一个。里面只有一个方法,的onClick(),其中包含有什么指示该按钮应该做的上点击的。

 公共无效的onClick(视图v){
    mp.start();
}
 

要播放声音,我们称之为MediaPlayer的 的start() 方法。此方法开始声音的再现。

在那里,你现在可以播放按钮点击声在Android的!


的原贴

第一。你应该把你的语句块中,在这种情况下,onCreate方法。

二。您初始化按钮为变量的一个,那么你使用一个变量,并设置其onClickListener一个不完整的onClickListener。使用变量的一个的setOnClickListener。

三,把逻辑发挥的onClick里面的声音。

在总结:

 进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;

公共类BasicScreenActivity延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_basic_screen);

        按钮一次=(按钮)this.findViewById(R.id.button1);
        最后的MediaPlayer MP = MediaPlayer.create(这一点,R.raw.soho);
        one.setOnClickListener(新OnClickListener(){

            公共无效的onClick(视图v){
                mp.start();
            }
        });
    }
}
 

I tried many tutorials, Youtube videos, Stackoverflow answers, none of them work.

Could you help me please? 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();
    }
});

To explain it step by step:

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

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);

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
    }
});

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();
}

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

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


Original Post

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

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.

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

In summary:

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();
            }
        });
    }
}

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

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