如何在Android中实现恢复/暂停录音?

时间:2014-11-03 21:56:06

标签: android audio mobile record

我正在构建使用恢复/暂停功能进行m4a录制的Android应用。 有可能吗?如果是,请告诉我解决方案。

由于

1 个答案:

答案 0 :(得分:0)

如果您需要播放音频/视频资源,可以使用MediaPlayer对象,如果您有特殊需要,可以将其包装。特别是,要显示视频,您需要使用SurfaceView及其持有者。无论如何,它的工作原理如下:

假设您的媒体资源位于原始文件夹中,媒体播放器可以是这样的:

import android.content.Context;
import android.media.MediaPlayer;

public class MyPlayer {

    private MediaPlayer mPlayer;
    private SurfaceHolder mSurfaceHolder;

    public void play(Context c) {

        if (mPlayer == null) {

            mPlayer = MediaPlayer.create(c, R.<YOUR RESOURCE NAME>);
            mPlayer.setDisplay(mSurfaceHolder);

            //Sets a listener to the mPlayer variable that set's it to null once the playing is finished
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                        stop();
                }
            });

        }
        mPlayer.start();
    }

    public void pause() {
        if (mPlayer.isPlaying()) {
            mPlayer.pause();
        }
    }

    public void stop() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    public void setSurface(SurfaceHolder sh) {
      mSurfaceHolder = sh;
    }

}

玩家将被封装在片段/活动中。假设有一个带有SurfaceView作为“屏幕”的xml布局和三个用于媒体控制的按钮,你的片段就像​​:

public class PlayerFragment extends Fragment {

    private Button mPlayButton;
    private Button mPauseButton;
    private Button mStopButton;
    private SurfaceView mSurface;
    private SurfaceHolder mHolder;
    private MyPlayer mPlayer = new MyPlayer();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

        //Gets the view and sets the player to be able to play a video resource
        View v = inflater.inflate(R.layout.<YOUR XML LAYOUT>, parent, false);
        mSurface = (SurfaceView) v.findViewById(R.id.surface);
        mHolder = mSurface.getHolder();
        mHolder.setKeepScreenOn(true);
        mPlayer.setSurface(mHolder);

        //Inflates the play button and sets on it a listener (created as an anonymous class)
        mPlayButton = (Button) v.findViewById(R.id.playButton);
        mPlayButton.setOnClickListener(new View.OnClickListener() {

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

        //Inflates the pause button and sets on it a listener (created as an anonymous class)
        mPauseButton = (Button) v.findViewById(R.id.pauseButton);
        mPauseButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPlayer.pause();    
            }
        });

        //Inflates the stop button and sets on it a listener (created as an anonymous class)
        mStopButton = (Button) v.findViewById(R.id.stopButton);
        mStopButton.setOnClickListener(new View.OnClickListener() {

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

        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPlayer.stop();
    }




}

一旦完成媒体资源,或者在获得锁定后销毁活动/片段,请务必小心并释放媒体资源。如果不这样做,您可能会损害其他应用程序或操作系统行为。

显然,如果您需要更复杂的功能,则必须添加它们。这只是一个非常简单的玩家的例子。还要记住,某些文件或设备可能会导致不兼容。