机器人 - 如何使一个按钮,点击播放声音文件每次都被pressed? [英] android - how to make a button click play a sound file every time it been pressed?

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

问题描述

我打开一个新的项目 -

现在我希望做的是这样的 - 通过该按钮pressing我想要一个MP3文件播放 - 也该按钮pressed比声音文件将开始从它开始再次玩,每次 - 让我们说,MP3是10秒长,我pressed的按钮,它的演奏和4秒后,我$ P $再次pssed按钮比声音将被再次播放。

现在我想知道的是 - 1 - 我应该在哪里把mp3文件?

2,什么code我必须为了添加按钮时pssed比MP3文件$ P $将播放(我们称之为MP3文件click_sound.mp3)?

3我需要添加到code,以便声音将再次出场,每次我都会pressed按钮?

这是MainActivity.java的code -

 包com.example.test1;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;

公共类MainActivity延伸活动{

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

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

}
 

和这是activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/播放/>

< / RelativeLayout的>
 

解决方案
  1. 您应该放在/资产的文件夹中的MP3文件。

  2. 把里面的的onCreate这code()的setContentView方法()

     最后MediaPlayer的熔点为新的MediaPlayer();
    按钮B =(按钮)findViewById(R.id.button1);
    
    b.setOnClickListener(新OnClickListener(){
    
        @覆盖
        公共无效的onClick(视图v){
    
            如果(mp.isPlaying())
            {
                mp.stop();
            }
    
            尝试 {
                mp.reset();
                AssetFileDescriptor AFD;
                AFD = getAssets()openFd(AudioFile.mp3)。
                mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                MP prepare()。
                mp.start();
            }赶上(IllegalStateException异常E){
                e.printStackTrace();
            }赶上(IOException异常E){
                e.printStackTrace();
            }
    
    
    
        }
    });
     

    3.sound将再次每次preSS按钮时播放。您不必编写任何额外的$ C $下的。

请注意,AudioFile.mp3是MP3文件的名称/资产的文件夹

希望这回答是有帮助的:)

I have open a new project -

Now what I would like to do is this - By pressing on the button I want an mp3 file being played - and also that each time the button is pressed than the sound file will start playing from the start of it once again - so let's say that the mp3 is 10 sec long, and I pressed the button and it's playing and after 4 sec I pressed the button again than the sound will be played again.

Now what I would like to know is- 1- Where should I put the mp3 file?

2-what code do I have to add in order that when the button is pressed than the mp3 file will be played (let's call the mp3 file click_sound.mp3)?

3- What I need to add to the code in order that the sound will be played again each time I will pressed the button?

This is the code of the MainActivity.java -

package com.example.test1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

and this is the activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play" />

</RelativeLayout>

解决方案

  1. You should put mp3 file in /assets folder.

  2. put this code inside onCreate() method after setContentView()

    final MediaPlayer mp = new MediaPlayer();
    Button b = (Button) findViewById(R.id.button1); 
    
    b.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
            if(mp.isPlaying())
            {  
                mp.stop();
            } 
    
            try {
                mp.reset();
                AssetFileDescriptor afd;
                afd = getAssets().openFd("AudioFile.mp3");
                mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
    
        }
    });
    

    3.sound will be played again each time you press button. You don't have to write any extra code for that.

Note that AudioFile.mp3 is the name of the mp3 file in /assets folder

Hope this answer is helpful:)

这篇关于机器人 - 如何使一个按钮,点击播放声音文件每次都被pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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