首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法暂停Audiotrack对象: android

无法暂停Audiotrack对象: android
EN

Stack Overflow用户
提问于 2012-10-16 02:18:32
回答 2查看 2.1K关注 0票数 3

在我的应用程序中,我已经成功录制了音频,现在我正在使用以下函数播放它:

代码语言:javascript
复制
    public void playfile()
{
    System.out.println("Play Pressed");     
     speechLength = (int)(file.length()/2);
     speech2 = new short[speechLength];
     try
     {
        // Create a DataInputStream to read the audio data back from the saved file.
        InputStream is = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);

        int i = 0;              
        while(i <  speechLength)
        {
            speech2[i] = dis.readShort();
            i++;
        }

        // Close the input streams.
        is.close();
        bis.close();
        dis.close();  

        // Create a new AudioTrack object using the same parameters as the AudioRecord
        // object used to create the file.
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                16000, 
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, 
                bufferSizeAT, 
                AudioTrack.MODE_STREAM);   

        //Start playback


        runner.start();
        //Updating Progress Bar on UI Thread
        //updateProgressBar();
     } //main try
    catch (Throwable t){} 
}
 Thread runner= new Thread (){
        public void run()
        {

              // Do stuff.
            audioTrack.play();
            audioTrack.setStereoVolume(50, 50);
            audioTrack.write(speech2, 0, speech2.length);

        }
    };

但是当单击按钮时,我想暂停正在播放的录制,但在恢复时什么也没有发生。下面是相同的代码:

代码语言:javascript
复制
       final Button p3_button=(Button)myView.findViewById(R.id.btn_play);
    p3_button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {       
            if(p3_button.getText().equals("Pause"))
            {

                p3_button.setText("Resume");
                audioTrack.pause();


            }
            else if(p3_button.getText().equals("Resume"))
            {
                p3_button.setText("Pause");

                audioTrack.play();


            }
            else
            {
                p3_button.setText("Pause");
                count = 0;
                isPlayPressed = true;
                playing = true;
                playfile();
            }
        }});

任何帮助都是最好的。在这个问题上,谷歌没有太多的帮助。我几乎完成了我的应用程序,但现在在最后一刻,我正在忍受这个issue..Please救救我!!

EN

回答 2

Stack Overflow用户

发布于 2012-10-22 02:09:02

不能使用==比较字符串

将像这样的p3_button.getText()=="Pause"代码行更改为p3_button.getText().equals("Pause")

如果这解决了您的问题,请尝试。

票数 2
EN

Stack Overflow用户

发布于 2013-05-30 01:39:27

我发现问题是track.write阻塞了。下面是一个现在似乎可以工作的WavTrack类:

代码语言:javascript
复制
package org.this_voice.zhongguomozhou;


import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Environment;
import java.io.InputStream;
import java.io.IOException;
import java.lang.Thread;

public class WavTrack {
/**
Make sure you set the sample rate, stereo/mono and encoding
correctly for your audio files.
**/

public static final boolean STOPPED = true;
public AudioTrack at;
public Context mContext;
public int minBufferSize;
public byte[] music = null;
public int size = 0;
public int position = 0;
public InputStream is;
public boolean paused = false;

public WavTrack(Context context) {
    mContext = context;
    minBufferSize = AudioTrack.getMinBufferSize(16000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
    at = new AudioTrack(AudioManager.STREAM_MUSIC, 16000,
        AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
        minBufferSize, AudioTrack.MODE_STREAM);
    is = mContext.getResources().openRawResource(R.raw.test01);

    try{
        size = (int)is.available();
    } catch (IOException e) {
        e.printStackTrace();
    }

    at.play();

    new Thread (new Runnable(){
        public void run() {
            int i = 0;
            try{
                music = new byte[512];
                while(((i = is.read(music)) != -1) && !paused){
                    at.write(music, 0, i);
                    position += i;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}


public void pause(){
    is.mark(position);
    paused = true;
    at.pause();
}

public void stop(){
    at.stop();
    at.release();
}

public void play() {
    paused = false;
    try {
        is.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }
    at.play();

    new Thread (new Runnable(){
        public void run() {
            int i = 0;
            try{
                music = new byte[512];
                while(((i = is.read(music)) != -1) && !paused){
                    at.write(music, 0, i);
                    position += i;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12901612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档