my media recorder is recording video after recording i am previewing my video (fine).
now uploaded this video on my local http server .
and finally getting this from server to play in a videoview but it shows cannot play video.
any suggestion where actually mistake is...
Thanx for help.
MediaRecorder
this.mediaRecorder = new MediaRecorder();
this.mediaRecorder.setCamera(this.camera);
camera.unlock();
this.mediaRecorder.setCamera(camera);
this.mediaRecorder.setOrientationHint(90);
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
this.mediaRecorder.setProfile(camcorderProfile_HQ);
this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath());
this.mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
this.mediaRecorder.setMaxFileSize(5000000);
this.mediaRecorder.setPreviewDisplay(this.cameraPreview.getHolder().getSurface());
try {
this.mediaRecorder.prepare();
// start the actual recording
// throws IllegalStateException if not prepared
this.mediaRecorder.start();
Toast.makeText(this, R.string.recording, Toast.LENGTH_SHORT).show();
// enable the stop button by indicating that we are recording
this.toggleButtons(true);
} catch (Exception e) {
Log.wtf(TAG, "Failed to prepare MediaRecorder", e);
Toast.makeText(this, R.string.cannot_record, Toast.LENGTH_SHORT).show();
this.releaseMediaRecorder();
}
Video view
Uri uri = Uri.parse("/data/data/com.example.mediarecorder/files/"+ messageList.get(position)); //do not add any extension
video.setVideoURI(uri);
video.requestFocus();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
video.setKeepScreenOn(true);
System.out.println(uri + " "+ uri.getEncodedAuthority()+ " "+ uri.decode(data_type) + " "+ uri.getUserInfo() + " "+ uri.describeContents());
video.start();
You can't play video from internal storage via videoview, as the file does not have world readable permission. You have to create custom video player using surfaceView and media player class. Then you have to create a file descriptor of the file you need to play and pass that to mediaPlayer.setDataSource():
FileInputStream inputStream = new FileInputStream(videoPath);
//assume videoPath = getFilesDir().getAbsolutePath()) + File.separator + "Video.mp4"
mp.setDataSource(inputStream.getFD());
mp.prepareAsync();
try {
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mplayer) {
if (mp != null) {
mp.start();
}
}
});
Related
I have written an opensource camera for Google glass but some of the people who have used it have reported that the video recorded doesn't get saved properly for lengthy videos.
I couldn't find info regarding any such limitation in the Android documentation
So Upon checking it out i found that for videos greater than 26 minutes , the video file got saved in Glass and Its size was around 2.7 GB but its duration was 0:00. And it couldn't be played using any video player.
So i am wondering why is that? Why does the video get properly recorded for duration < 26 minutes and gets messed up for longer videos.
Code to start video Recording is
/**
* Initialize video recorder to record video
*/
private void initRecorder() {
try {
File dir = new File(Environment.getExternalStorageDirectory()
+ File.separator + Environment.DIRECTORY_PICTURES
+ File.separator + "My Videos");
if (!dir.exists()) {
dir.mkdirs();
}
videofile = new File(dir, "video.mp4");
recorder.setCamera(mCamera);
// Step 2: Set sources
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
recorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
recorder.setOutputFile(videofile.getAbsolutePath());
// Step 5: Set the preview output
recorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
recorder.setMaxDuration(3600* 1000);
recorder.setMaxFileSize(-1);
recorder.setOnErrorListener(new OnErrorListener() {
#Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.e("Error Recording", what+" Extra "+extra);
}
});
recorder.setOnInfoListener(new OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
endVideoRecording();
}
}
});
recorder.prepare();
recorder.start();
mOverlay.setMode(Mode.RECORDING);
} catch (Exception e) {
if (e != null && e.getMessage() != null)
Log.e("Error Starting CuXtom Camera for video recording",
e.getMessage());
}
}
According to my research it seems that Google glass is only capable of recording video under 2GB size so if you want to record any video whose size might be greater than that then i would advise you to divide the video into smaller parts and then at the end merge it with mp4 parser
I try call the default audio recorder with intent and send path to audio recorder than save voice in my path in sdcard.I use this code:
File audioFile = new File(Environment.getExternalStorageDirectory().getPath() + "/nabege" + File.separator + "audio"
+ File.separator + System.currentTimeMillis() + ".amr");
Uri audioUri = Uri.fromFile(audioFile);
record_audio_Intent.putExtra(MediaStore.EXTRA_OUTPUT, audioUri);
startActivityForResult(record_audio_Intent, 0);
This app save voice in default path. This path (nabege/audio) created in sdcard and exist.Please help
Sound recording via intent always saves the recorded file to root of sdcard (or to a default path). Also, this intent may not be supported in certain phone models.
You must use MediaRecorder directly if you want to save recorded files to your custom paths.
Like this:
MediaRecorder recorder = null;
private void startRecording(File file) {
if (recorder != null) {
recorder.release();
}
recorder = new MediaRecorder();
recorder.setAudioSource(AudioSource.MIC);
recorder.setOutputFormat(OutputFormat.THREE_GPP);
recorder.setAudioEncoder(AudioEncoder.AMR_WB);
recorder.setOutputFile(file.getAbsolutePath());
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
Log.e("giftlist", "io problems while preparing [" +
file.getAbsolutePath() + "]: " + e.getMessage());
}
}
I have a sound that is supposed to start when the activity starts onCreate because this is an alarm. THe problem is, the sounds doesn't start. I've checked my volume, it's on.
This is the code for the media player:
String tone = intent.getStringExtra("reminderTone");
MediaPlayer mPlayer = new MediaPlayer();
try {
Log.d("log", "try");
if (tone != null && !tone.equals("")) {
Log.d("log", "tone is not null");
Uri toneUri = Uri.parse(tone);
if (toneUri != null) {
Log.d("log", "mediaplayer starts");
mPlayer.setDataSource(this, toneUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.setLooping(true);
mPlayer.prepare();
mPlayer.start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
and this is the log output
08-09 10:55:33.316 20706-20706/? D/log﹕ try
08-09 10:55:33.317 20706-20706/? D/log﹕ tone is not null
08-09 10:55:33.317 20706-20706/? D/log﹕ mediaplayer starts
I've already checked "reminderTone" there is a String URI in it.
This is how the String URI looks like. "android.media.Ringtone#ab91882"
I used another source of sound to test, and this one works:
//play ringtone
final MediaPlayer sounds = MediaPlayer.create(this, R.raw.sample);
sounds.start();
Do you guys know what's wrong with this?
Context appContext = getApplicationContext();
MediaPlayer resourcePlayer = MediaPlayer.create(appContext, R.raw.my_audio);
MediaPlayer filePlayer = MediaPlayer.create(appContext, Uri.parse("file:///sdcard/localfile.mp3"));
MediaPlayer urlPlayer = MediaPlayer.create(appContext, Uri.parse("URL"));
MediaPlayer contentPlayer = MediaPlayer.create(appContext, Settings.System.DEFAULT_RINGTONE_URI);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("/sdcard/test.3gp");
mediaPlayer.prepare();
You need to check your URI, make sure you use the proper URI.
Note: URI string is different for every storage media that you are
calling, in my example I am assuming that you are looking for the
Music inside your raw folder.
int i = R.raw.sample; // we assume that we want to get the URI of our sound from our RAW folder.
String uri = android.resource://com.example.myproject/" + i;
For playing the music you can use this simple method
MediaPlayer player = null;
public void playMusic(int i) throws RemoteException {
try {
if (player == null)
player = new MediaPlayer();
player.reset(); // optional if you want to repeat calling this method
player.setDataSource(getApplicationContext(), uri);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The full working example can be downloaded here : Music Player Tutorial
I'm done my Android app, just need to add some looping background music. Here is my method for playing the song
public void playAudio(){
path = "android.resource://" + getPackageName() + "/" + R.raw.music1;
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path);
mp.prepare();
mp.setLooping(true);
mp.setVolume(100, 100);
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.d("NOTE WORKEING","NOT WORKING");
}
}
It's not working....It's going to catch everytime, and I don't know why. Please help me. Music1 is an mp3 file.
Thank you
Make sure your music1 file is in a Android playable Android format.
Then just use this code:
MediaPlayer mediaPlayer = MediaPlayer.create(YourActivity.this, R.raw.music1);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
mediaPlayer.start();
}
catch (Exception e) {
e.printStackTrace();
Log.d("NOT WORKEING","NOT WORKING");
}
You don't even need to call prepare() method.
I tested it with an mp3 file and it works perfectly.
If #Squonk's answer does not work, then the problem has to do with trying to start the music file before the MediaPlayer has prepared it. Try changing the MediaPlayer's start code to the following:
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
start();
}
});
Let me know if this helps, I have a good amount of experience with the MediaPlayer class and can help you troubleshoot.
Using mp.setDataSource(path) requires a valid filesystem path or URL.
In your case, path is a string representation of a Uri in which case you need to use a different approach.
Try setDataSource(Context context, Uri uri). You'll obviously need to provide a valid Context and parse your path variable into a Uri. Example...
mp.setDataSource(getApplicationContext(), Uri.parse(path));
Also change the path to...
path = "android.resource://" + getPackageName() + "/raw/music1";
The problem I am trying to solve is in an activity that needs to play back audio files. Most of the files will be user created (and saved into external storage), and therefore played with the following code (based on Google's example code):
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(filename);
mPlayer.prepare();
mPlayer.start();
Some of the audio files, though, are going to be included with the app, are usually played with the following code:
MediaPlayer mPlayer = MediaPlayer.create(getBaseContext(), R.raw.filename);
mPlayer.prepare();
mPlayer.start();
The issue is that I would like to be able to play the audio files in the raw folder in the same way that I am playing the user created files since I don't necessarily know which will be needed. I tried tried to get the URI of the audio file in the raw folder and play it with the following code:
Uri uri = Uri.parse("android/resource://com.my.package/" + R.raw.filename);
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(uri.toString());
mPlayer.prepare();
mPlayer.start();
But nothing happens. No error messages, no playing audio.
I outlined what I thought was the simplest solution, but I am willing to go another route if it accomplishes the task. Any assistance is greatly appreciated.
I've managed to get it working, here is the code I used.
mMediaPlayer = new MediaPlayer();
Uri mediaPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.filename);
try {
mMediaPlayer.setDataSource(getApplicationContext(), mediaPath);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
I see two problems I see with your code. The first is that "android/resource" needs to become "android.resource"
The second is that setDataSource(String) won't work in this case due to the fact that you need context to use raw files as otherwise it tries to open the file incorrectly. (See the top answer in MediaPlayer.setDataSource(String) not working with local files)
final int[] song = {R.raw.letmeloveyou,R.raw.aarti,R.raw.answer};
final MediaPlayer mp = new MediaPlayer();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
if (true == mp.isPlaying()) {
mp.stop();
mp.reset();
Toast.makeText(getBaseContext(), "Again Playing", Toast.LENGTH_LONG).show();
mp.setDataSource(Result.this, Uri.parse("android.resource://" + getPackageName() + "/" + song[position]));
mp.prepare();
mp.start();
} else {
Toast.makeText(getBaseContext(), "Playing", Toast.LENGTH_LONG).show();
mp.setDataSource(Result.this, Uri.parse("android.resource://" + getPackageName() + "/" + song[position]));
mp.prepare();
mp.start();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
});
When I used toString() in Uri it failed, when I used direct Uri it played well.
Uri musicUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceID);
Log.d(TAG, "musicUri: " + musicUri);
mediaPlayer.setDataSource(context, musicUri);