我正在开发一个应用程序,我想在按钮点击时开始录制语音,同时也想在视频视图中播放视频。 现在该应用程序适用于androdi 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