2014-11-03 64 views
0

我正在構建Android應用程序,它正在使用恢復/暫停功能進行m4a錄製。 可以嗎?如果是的話,讓我知道解決方案。如何在Android中實現簡歷/暫停錄音?

感謝

+2

「我正在構建Android應用程序」......太好了,分享你迄今爲止所擁有的。 – 323go 2014-11-03 22:06:41

回答

0

如果需要播放音頻/視頻資源可以用MediaPlayer對象,你可以用它,如果你有特別的needs.Specially,顯示你需要一個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; 
    } 

} 

玩家將片段/活性內部incapsulated。假設有一個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(); 
    } 




} 

一定要小心,一旦你完成了他們或釋放該媒體資源當活動/片段因獲得鎖定而被銷燬時。如果你不這樣做,你可能會損害其他應用程序或操作系統的行爲。

顯然,如果你需要更復雜的功能,你必須添加它們。這只是一個非常簡單的球員的例子。請記住,某些文件和/或設備可能會導致不兼容。

+0

感謝您的幫助,此時我需要錄音功能(暫停/恢復,播放) – 2014-11-04 05:47:00