【问题标题】:Voice does not record when playing video in video view in android在 android 的视频视图中播放视频时不录制语音
【发布时间】:2017-07-25 14:14:03
【问题描述】:

我正在开发一个应用程序,我想在单击按钮时开始录制语音,同时还想开始在视频视图中播放视频。 现在该应用程序可以在 android 4.2 和 android 5.1 上运行,但不能在 android 7.0 等最新设备上运行。 这是代码

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

【问题讨论】:

    标签: android media-player android-videoview


    【解决方案1】:

    您可能必须请求android.permission.RECORD_AUDIO 权限才能在 Android 6 版本之后进行录制。

    请查看the document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2017-10-17
      • 2014-02-12
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多