java - 如何将音频文件修剪到不到1秒?

标签 java android audio trim

在声音编辑器中,我将音频文件裁剪到不到1秒。每当我尝试在其他地方使用它时。如果将文件填充为1秒左右,请保留空白空间。因此,每当我希望单击按钮时就播放声音。如果音乐文件尚未播放完毕(包括空音频),则单击另一个按钮时将不会播放声音。有没有办法在Java / Android中做到这一点

package com.dose.apps.brainnoodles;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;




public class Test1 extends MainActivity implements View.OnClickListener {
      /*
  *Icremented Variables
  */

    Integer questionIndex = 0;
    Integer questionValue = 1;
    Integer score1 = 0;



    int iOne = 1;
    int iZero = 0;
    //Non-incremented variables

    MediaPlayer buttonClickSound;

    String halfQues8 = "They got upset when he took ____ stuff.";

    TextView answerViewA, answerViewB, answerViewC, questionView, questionView2, questionNumber;

    ImageView button1, button2, button3;



    /*
     *Questions Array
    */


    String[] questions1 =
            {
                    "How many half days are in 1 day?", //Array 0
                    "4 pears were on sale for $5. How much is each pear?", //Array 1 /
                    "What is the temperature right below 0°F?", //Array 2 //
                    "How long it take to reach 60 miles if going 60mph?", //Array 3 //
                    "How long does it take for Summer to get to the following Summer?", //Array  4
                    "What is 100% of 100?", //Array  5//
                    "What weighs more. 1 pound of feathers, or 1 pound of metal?", //Array 6 //
                    "Which word should be on the empty line?", //Array 7 //
                    "How many licks does it take to get to the tootsie roll center of a tootsie pop(No biting)?", //Array 8
                    "Which statement is true?", //Array 9

            };

    /*
     * Button A
     */
    String[] answerTextA1 = {
            "2", //Array 0
            "2.25", //Array 1
            "-1°C",  //Array 2
            "2 hours", //Array 3
            "12 months", //Array 4
            "100", //Array 5
            "1 pound of feathers", //Array 6
            "their", //Array 7
            "20", //Array 8
            "Dense clouds = more sunlight " //Array 9
    };


    /*
    * Button B
    */
    String[] answerTextB1 = {
            "1", //Array 0
            "1.25", //Array 1
            "-1°F",  //Array 2
            " 1.5 hours",  //Array 3
            " 12 years", //Array 4
            " 10,000", //Array 5
            " 1 pound of metal", //Array 6//
            "there",  //Array 7
            "Less than 20 ", //Array 8
            "Dense clouds = less sunlight", //Array 9

    };

    /*
    * Button C
    */


    String[] answerTextC1 = {
            "1.5", //Array 0
            "1.50", //Array 1
            "1°F",  //Array 2
            "1 hour", //Array 3
            "1.2 years", //Array 4
            "200", //Array 5
            "They weigh the same",  // Array / 6
            "they're",  // Array 7
            "More than 20", //Array 8
            "Both A and B" //Array 9
    };


    /*
    *User's Answer Choices
     */

    public String[] usersAnswers1 = {
            "", //Array 0
            "", //Array 1
            "", //Array 2
            "", //Array 3
            "", //Array 4
            "", //Array 5
            "", //Array 6
            "", //Array 7
            "", //Array 8
            "" //Array 9
    };

    /*
    * Is the user answers true or false
     */

   public  String[] isATOF1 = {
           "", //Array 0
           "", //Array 1
           "", //Array 2
           "", //Array 3
           "", //Array 4
           "", //Array 5
           "", //Array 6
           "", //Array 7
           "", //Array 8
           "", //Aray 9
   };



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

        // Button Initializations //

        button1 = ((ImageView) findViewById(R.id.button1));
        button2 = ((ImageView) findViewById(R.id.button2));
        button3 = ((ImageView) findViewById(R.id.button3));
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

        // TextView Initializations //


        answerViewA = ((TextView) findViewById(R.id.textView1));
        answerViewB = ((TextView) findViewById(R.id.textView2));
        answerViewC = ((TextView) findViewById(R.id.textView3));
        questionView = ((TextView) findViewById(R.id.questionView));
        questionView2 = ((TextView) findViewById(R.id.questionView2));
        questionNumber = (TextView) findViewById(R.id.questionNumber);

        String questionValueS = Integer.toString(questionValue);
        questionNumber.setText("Question " + questionValueS + ".");
        answerViewA.setText(answerTextA1[questionIndex]);
        answerViewB.setText(answerTextB1[questionIndex]);
        answerViewC.setText(answerTextC1[questionIndex]);
        questionView.setText(questions1[questionIndex]);

        buttonClickSound = MediaPlayer.create(this, R.raw.button_click_sound);
    }


    @Override
    public void onClick(View v) {





//Question Balue 8 other half


        if (questionValue == 7){
            questionView2.setText(halfQues8);
        }

        if (questionValue != 7){
            questionView2.setText("");
        }
        if (questionValue == iOne) {
            if (v.getId() == R.id.button1) {
                usersAnswers1[iZero] = answerTextA1[iZero];
            }
            if (v.getId() == R.id.button2) {
                usersAnswers1[iZero] = answerTextB1[iZero];
            }
            if (v.getId() == R.id.button3) {
                usersAnswers1[iZero] = answerTextC1[iZero];
            }
        }


        /*
        *Determining if the users answers are True or False
         */

        if (questionValue == 1){
    if (v.getId() == R.id.button1){
        isATOF1[0] = "True";
        score1++;
    }else {
        isATOF1[0] = "False";
    }
}

        if (questionValue == 2){
            if (v.getId() == R.id.button2){
                isATOF1[1] = "True";
                score1++;
            }else {
                isATOF1[1] = "False";
            }
        }

        if (questionValue == 3){
            if (v.getId() == R.id.button2){
                isATOF1[2] = "True";
                score1++;
            }else {
                isATOF1[2] = "False";
            }
        }


        if (questionValue == 4){
            if (v.getId() == R.id.button3){
                isATOF1[3] = "True";
                score1++;
            }else {
                isATOF1[3] = "False";
            }
        }


        if (questionValue == 5){
            if (v.getId() == R.id.button1){
                isATOF1[4] = "True";
                score1++;
            }else {
                isATOF1[4] = "False";
            }
        }


        if (questionValue == 6){
            if (v.getId() == R.id.button1){
                isATOF1[5] = "True";
                score1++;
            }else {
                isATOF1[5] = "False";
            }
        }


        if (questionValue == 7){
            if (v.getId() == R.id.button3){
                isATOF1[6] = "True";
                score1++;
            }else {
                isATOF1[6] = "False";
            }
        }


        if (questionValue == 8){
            if (v.getId() == R.id.button1){
                isATOF1[7] = "True";
                score1++;
            }else {
                isATOF1[7] = "False";
            }
        }


        if (questionValue == 9){
            if (v.getId() == R.id.button3){
                isATOF1[8] = "True";
                score1++;
            }else {
                isATOF1[8] = "False";
            }
        }


        if (questionValue == 10){
            if (v.getId() == R.id.button2){
                isATOF1[9] = "True";
                score1++;
            }else {
                isATOF1[9] = "False";
            }
        }



            questionValue++;
            String questionValueS = Integer.toString(questionValue);

            questionIndex++;


            if (questionIndex < questions1.length)

            {



// make sure the index is in bounds & increment the index//


                questionView.setText(questions1[questionIndex]); // set the text to the next question



        /*
         * set the answerView(s) to the next answer options
         */
                answerViewA.setText(answerTextA1[questionIndex]);
                answerViewB.setText(answerTextB1[questionIndex]);
                answerViewC.setText(answerTextC1[questionIndex]);
                questionNumber.setText("Question " + questionValueS + ".");


                //Incrementing Needed to catch users Answers//

                iOne++;
                iZero++;



            } else {

                String score1S = Integer.toString(score1);

                Intent intent1 = new Intent(Test1.this, UsersAnswers1.class);
                intent1.putExtra("usersAnswers1", usersAnswers1);
                intent1.putExtra("isATOF1", isATOF1);
                intent1.putExtra("score1S", score1S);
                startActivity(intent1);
            }
        }

    }

最佳答案

您可以根据需要设置时间

final MediaPlayer buttonClickSound = MediaPlayer.create(this, R.raw.button_click_sound);
{
@Override
public void onClick(View v) {

 buttonClickSound.start();
        // wait less than 1 sec... then stop the player.
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                buttonClickSound.stop();
            }
        }, 900);//millisec.

关于java - 如何将音频文件修剪到不到1秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28920875/

相关文章:

java - 从相机设备拍摄的照片中获取信息

java - 键中有多个值的集合

java - 分割整数

android - 如何使用 OKHTTP 处理新证书?

android - 从 url 加载文件问题 - 连接被拒绝

android - 如何在安卓手机上静音相机快门声音

java - Apache Curator 双重锁定问题与多个服务

javascript - Android Studio Zxing Scanner,如果QR码的结果是URL,如何重定向到网页?

Android通知声音默认返回而不是播放自定义声音

linux - 比较录音