2017-07-25 148 views
0

我正在開發一個應用程序,在該應用程序中,我想要在按鈕單擊時開始錄製語音,並且還希望同時在視頻視圖中明星播放視頻。 現在,該應用程序可以在Android 4.2和Android 5.1上運行,但不適用於Android 7.0等最新設備。 下面是代碼當在視頻視圖中播放視頻時,語音不會錄製在android

public class MainActivity extends Activity 
{ 
    Button start,stop,play; 
Boolean recording=false; 
String press_value="exit"; 



private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder"; 
    boolean let_start_recording=true; 

    ImageView startIV,profileIV; 

    boolean recording_is_in_progress=false; 
    VideoView vv ; 
    MediaPlayer mPlayer; 
    RelativeLayout mainRel; 

    ///new media player 
    String AudioSavePathInDevice = null; 
     MediaRecorder mediaRecorder ; 
     Random random ; 
     String RandomAudioFileName = "ABCDEFGHIJKLMNOP"; 
     public static final int RequestPermissionCode = 1; 
     MediaPlayer mediaPlayer ; 




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


    startIV=(ImageView)findViewById(R.id.imageView1); 
    profileIV=(ImageView)findViewById(R.id.imageView3); 

    vv = (VideoView)findViewById(R.id.your_video_view); 
    mainRel=(RelativeLayout)findViewById(R.id.main_rel); 


    recording_is_in_progress=false; 
    StopAnimation(); 

    profileIV.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
     if(let_start_recording) 
      { 
       let_start_recording=false; 
       startIV.setImageResource(R.drawable.recorder_stop_circle); 
       startRecording(); 
       recording_is_in_progress=true; 
       StartAnimation(); 

      } 
      else 
      { 
       let_start_recording=true; 
       startIV.setImageResource(R.drawable.recorder_start_circle); 
       stopRecording(); 
       recording_is_in_progress=false; 

       StopAnimation();//Stop Animation once we press button 

      } 
     } 

    }); 

    //START & STOP Recording 
    startIV.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
     if(let_start_recording) 
      { 
       let_start_recording=false; 
       startIV.setImageResource(R.drawable.recorder_stop_circle); 
       startRecording(); 
       recording_is_in_progress=true; 
       StartAnimation(); 

      } 
      else 
      { 
       let_start_recording=true; 
       startIV.setImageResource(R.drawable.recorder_start_circle); 
       stopRecording(); 
       recording_is_in_progress=false; 

       StopAnimation();//Stop Animation once we press button 

      } 
     } 

    }); 



    }//EOF Oncreate 


//(((( START ANIMATION)))) 
public void StartAnimation() 
{ 

// vv.setVisibility(View.VISIBLE); 
    startIV.setVisibility(View.VISIBLE); 
    mainRel.setVisibility(View.VISIBLE); 
    profileIV.setVisibility(View.GONE); 

     //Video Loop 
    vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     public void onCompletion(MediaPlayer mp) { 
      vv.start(); //need to make transition seamless. 
     } 
    }); 

// Uri uri = Uri.parse(R.drawable.sheep_video); 
    Uri uri= Uri.parse("android.resource://com.pac.myapp/raw/sheep_video"); 

    vv.setVideoURI(uri); 
    vv.requestFocus();  
    vv.start(); 

    vv.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.sheep_video)); 


    vv.setOnPreparedListener(new OnPreparedListener() 
    { 
     @Override 
     public void onPrepared(MediaPlayer mp) 
     { 
      mp.setLooping(true); 
      /// showtoast("video compelted"); 
     } 
    }); 

} 

public void StopAnimation() 
{ 
    if(vv.isPlaying()) 
     vv.stopPlayback(); 

    //vv.setVisibility(View.INVISIBLE); 
    startIV.setVisibility(View.GONE); 
    mainRel.setVisibility(View.INVISIBLE); 
    profileIV.setVisibility(View.VISIBLE); 


} 

private String getFilename() 
{ 
    String filepath = Environment.getExternalStorageDirectory().getPath(); 
    File file = new File(filepath, AUDIO_RECORDER_FOLDER); 

    if (!file.exists()) 
    { 
     file.mkdirs(); 
    } 


    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String filename = dateFormat.format(new Date()); 
    filename="Voice "+filename+".wav"; 

    return (file.getAbsolutePath() + File.separator + filename); 
    //return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".wav"); 
} 

private void startRecording() 
{ 

    AudioSavePathInDevice = getFilename(); 
      // Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + 
      // CreateRandomAudioFileName(5) + "AudioRecording.3gp"; 

      MediaRecorderReady(); 

      try { 
      mediaRecorder.prepare(); 
      mediaRecorder.start(); 
      } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 

} 

private void stopRecording() 
{ 
    mediaRecorder.stop(); 
} 


    public void MediaRecorderReady(){ 
     mediaRecorder=new MediaRecorder(); 
     mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
     mediaRecorder.setOutputFile(AudioSavePathInDevice); 
    } 


public void onBackPressed() 
{ 
    if(recording_is_in_progress=true) 
    { 
     //if(recorder!=null) 
     //stopRecording(); 
     finish(); 
    } 
} 
public void home(View v) 
{ 
//Intent i=new Intent(v.getContext(),MainActivity.class); 
//startActivity(i); 
//finish(); 

    } 
    public void voice_list(View v) 
    { 
    if(recording_is_in_progress==false) 
    { 
    Intent i=new Intent(v.getContext(),SavedVoiceList.class); 
    startActivity(i); 
    finish(); 
} 

} 
    public void about_us(View v) 
    { 
//showtoast("status = "+recording_is_in_progress); 

if(recording_is_in_progress==false) 
{ 
    Intent i=new Intent(v.getContext(),AboutUsActivity.class); 
    startActivity(i); 
    finish(); 
} 
} 
public void showtoast(String str) 
{ 
    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); 
} 
    }//EOF Activit 

回答

0

也許你有要求android.permission.RECORD_AUDIO權限才能的Android版本6之後,記錄它。

請檢查the document