Android音频不播放

时间:2014-08-02 18:08:13

标签: java android audio

我的应用程序的一部分有两个按钮,当点击其中任何一个时,它们每个都发出声音。除了第二次按下“下一步”按钮外,声音播放完美。我可以按下这个按钮100次,它总共可以工作99次,但由于某种原因我第二次点击它,音频永远不会播放,我无法弄清楚原因。它总是在第二次按下它不播放。

package com.example.otapp;

import java.util.ArrayList;
import java.util.List;

import com.example.otapp.*;
import com.example.otapp.R.raw;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.os.Build;

public class DisplayMessageActivity extends MainActivity {

    ViewGroup relativeLayout;

    private Button taskButton;
    private Button nextButton;
    private String taskImageName, taskText;
    private String highlightedImageName;
    private int taskCount;
    private String list;
    private Intent blah;
    private MediaPlayer check, next;
    private int textWidth;
    TextView taskTextView;

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

        // set the task number to the first task
        taskCount = 1;

        // Get access to the buttons
        taskButton = (Button) findViewById(R.id.task);
        nextButton = (Button) findViewById(R.id.next);

        Intent intent = getIntent();

        // see which list user wants to display
        list = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        View taskButtonTemp = taskButton;
        View nextButtonTemp = nextButton;

        taskTextView = (TextView) findViewById(R.id.textView);

        taskTextView.setTextSize(20);

        RelativeLayout.LayoutParams params2 = (android.widget.RelativeLayout.LayoutParams) taskButtonTemp.getLayoutParams();
        RelativeLayout.LayoutParams params3 = (android.widget.RelativeLayout.LayoutParams) nextButtonTemp.getLayoutParams();

        params2.height = Screen_Height/3;
        params2.width = Screen_Height/3;
        params3.height = Screen_Height/3;
        params3.width = Screen_Height/3;

        taskButtonTemp.setLayoutParams(params2);
        nextButtonTemp.setLayoutParams(params3);

        // get the first task
        getTask(taskCount);

        // put the first task on the screen
        taskButton.setBackgroundResource(getResources().getIdentifier(taskImageName, "drawable", getPackageName()));

        // check if buttons are clicked
        taskButton.setOnClickListener(onClickListener);
        nextButton.setOnClickListener(onClickListener);

        blah = new Intent(this, MainActivity.class);
        blah.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // set the 'next' button invisible initially
        nextButton.setVisibility(View.GONE);

        check = MediaPlayer.create(DisplayMessageActivity.this, raw.check);
        next = MediaPlayer.create(DisplayMessageActivity.this, raw.next);
    }

    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            // do different things for each different button
            switch(v.getId()) {
                // if you press the task button
                case R.id.task:

                    // play sound
                    check.start();

                    // show the next button
                    nextButton.setVisibility(View.VISIBLE);
                    break;

                // if the next button is pressed
                case R.id.next:

                    // play sound
                    next.start();

                    // next task
                    taskCount++;

                    // get next task
                    getTask(taskCount);

                    // if we havent went through the task list
                    if(taskImageName.equals("finished") == true)
                    {
                        // if we did, go back to main menu
                        startActivity(blah);
                    }

                    // show the task
                    taskButton.setBackgroundResource(getResources().getIdentifier(taskImageName, "drawable", getPackageName()));

                    // hide the next button
                    nextButton.setVisibility(View.GONE);
                    break;
            }
        }
    };

    private String getTask(int taskCount) {

        // based on whatever list we are supposed to show
        switch(list) {

            case "Get Up":

                // get the current task we are supposed to show from that list
                switch(taskCount) {
                    case 1:
                        taskImageName = "alarm";
                        taskTextView.setText("Turn Off The Alarm");
                        break;
                    case 2:
                        taskImageName = "bed";
                        taskTextView.setText("Get Out of Bed");
                        break;
                    case 3:
                        taskImageName = "toilet";
                        taskTextView.setText("Go To The Toilet");
                        break;
                    case 4:
                        taskImageName = "toothbrush";
                        taskTextView.setText("Brush Your Teeth");
                        break;
                    case 5:
                        taskImageName = "sink";
                        taskTextView.setText("Wash Your Hands And Face");
                        break;
                    case 6:
                        taskImageName = "finished";
                        break;
                }
            break;

            case "Get Dressed":

                switch(taskCount) {
                    case 1:
                        taskImageName = "pyjamas";
                        taskTextView.setText("Take Off Pyjamas");
                        break;
                    case 2:
                        taskImageName = "underwear2";
                        taskTextView.setText("Take Off Underwear");
                        break;
                    case 3:
                        taskImageName = "underwear";
                        taskTextView.setText("Put On Clean Underwear");
                        break;
                    case 4:
                        taskImageName = "vest";
                        taskTextView.setText("Put On Vest");
                        break;
                    case 5:
                        taskImageName = "shirt";
                        taskTextView.setText("Put On Shirt");
                        break;
                    case 6:
                        taskImageName = "tie";
                        taskTextView.setText("Put On Tie");
                        break;
                    case 7:
                        taskImageName = "jumper";
                        taskTextView.setText("Put On Jumper");
                        break;
                    case 8:
                        taskImageName = "socks";
                        taskTextView.setText("Put On Socks");
                        break;
                    case 9:
                        taskImageName = "trousers";
                        taskTextView.setText("Put On Pants");
                        break;
                    case 10:
                        taskImageName = "shoes";
                        taskTextView.setText("Put On Shoes");
                        break;
                    case 11:
                        taskImageName = "brush";
                        taskTextView.setText("Brush Your Hair");
                        break;
                    case 12:
                        taskImageName = "finished";
                        break;
                }
            break;
        }

        return taskImageName;
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

Hey Jake我在github上有一个按钮播放器非常好用。你可以看看我在那堂课里做了什么。

https://github.com/rbeltran/breathe/blob/master/src/com/jphat/Breathe/util/ButtonPlayer.java

它可能是您需要的releasePlayer.prepare()方法或您可能想要调用的release()。