停止当前音频文件以防止同时出现多个文件

时间:2016-05-10 20:21:05

标签: java android audio

我正在尝试在Android Studio中制作一种音板应用。现在我有三个按钮:其中两个播放特定的音频文件,另一个停止音频。一切正常,除非您播放一个音频文件而另一个音频文件已经播放,它们同时播放,停止按钮不再适用于其中任何一个。你必须退出应用程序。在播放第二个文件之前,我无法弄清楚如何让第一个文件退出。到目前为止,这是我的代码:

package com.something.example.com;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {

    private Button startLie, startTruth,
            stopScan;
    private MediaPlayer play;

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

        startLie = (Button) findViewById(R.id.startlie);
        startLie.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                play = MediaPlayer.create(MainActivity.this, R.raw.lie_detector);
                play.start();
                play.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer play) {
                        play.release();
                    }
                });
            }
        });

        stopScan = (Button) findViewById(R.id.stopScan);
        stopScan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                play.stop();
                };
            });

        startTruth = (Button) findViewById(R.id.starttruth);
        startTruth.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                play = MediaPlayer.create(MainActivity.this, R.raw.truth_detector);
                play.start();
                play.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer play) {
                        play.release();
                    }
                });
            }
        });

1 个答案:

答案 0 :(得分:0)

所以我似乎终于可以让它工作了。我基本上创建了一个stopPlaying函数(放在我文件的末尾。然后每次在音频文件播放之前调用它。这里是代码,这次还有几个按钮。

CREATE TABLE avro_table_willi (a string, b long )
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'
STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat'
TBLPROPERTIES (
  'avro.schema.literal'='{
    "type": "record",
    "name": "my_record",
    "fields": [
      {"name": "a", "type": "int"},
      {"name": "b", "type": "long", "logicalType": "timestamp-millis" }

          ]}');