I'm using MediaRecorder class to record an audio file and I used
final String MEDIA_OUTPUT_FILE = "MyOutPutFile";
mediaRecorder.setOutputFile(MEDIA_OUTPUT_FILE);'
to give a name to my output file.
I also know how to Play an Audio File from Resources:
MediaPlayer mpRes = MediaPlayer.create(getApplicationContext(), R.raw.audiofile);
mpRes.start();'
My Question Is, Now, how to play the recorded file while I dont know where my MEDIA_OUTPUT_FILE is saved?
Instead of using constant as argument for mediaRecorder.setOutputFile() use a variable like below.
private String filePath;
filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
filePath += "/myrecording.mpeg";
Then pass the filePath variable as the argument as follow;
mediaRecorder.setOutputFile(filePath);
Then you can play the recorded file as follows;
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.start();
That's it.
Related
I want to create a media player with multiple sounds and play one at random when a button is clicked.
I tried using the MediaPlayer.create() function but I can only set one audio source.
I also tried using the setDataSource() function but this only seems to work for resources that are stored outside the application (as far as I know).
String meowsounds[] = new String[]{
"R.raw.meowcat1",
"R.raw.meowcat2",
"R.raw.meowcat3",
"R.raw.meowcat4",
"R.raw.meowcat5",
};
MediaPlayer mp = new MediaPlayer();
meow.setOnClickListener((view) ->{
Random random = new Random();
int r = random.nextInt(6-1) + 1;
try{
mp.setDataSource(meowsounds[r]);
mp.prepare();
mp.start();
}catch(Exception e){e.printStackTrace();}
});
This code doesn't give any error while running but doesn't play anything on click
EDIT: Updated code which works on first click but not on subsequent clicks:
meow.setOnClickListener((view) ->{
Random random = new Random();
int r = random.nextInt(6-1) + 1;
Toast.makeText(MainActivity.this,Integer.valueOf(r).toString(),Toast.LENGTH_SHORT).show();
try{
if(r==1)
fileDescriptor = MainActivity.this.getResources().openRawResourceFd(R.raw.meowcat1);
else if (r==2)
fileDescriptor = MainActivity.this.getResources().openRawResourceFd(R.raw.meowcat2);
else if (r==3)
fileDescriptor = MainActivity.this.getResources().openRawResourceFd(R.raw.meowcat3);
else if (r==4)
fileDescriptor = MainActivity.this.getResources().openRawResourceFd(R.raw.meowcat4);
else
fileDescriptor = MainActivity.this.getResources().openRawResourceFd(R.raw.meowcat5);
mp.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
mp.prepare();
mp.start();
}catch(Exception e){e.printStackTrace();}
});
R - is the class with constants. "R.raw.meowcat1" - is the simple string and it is not constant which consider link to file.
First, you need to create int array of raw constants, but not string array of simple strings.
Second, If you want to play file from raw, you need to create AssetFileDescriptor for this raw file and use in setDataSource method. For example:
AssetFileDescriptor fileDescriptor = getContext().getResources().openRawResourceFd(R.raw.meowcat1);
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
And don't forget please release codec in onDestroy method.
First I make a call to
get_fullPathToAudio();
to determine the path of where an audio file was downloaded.
public File get_fullPathToAudioAsFile()
{
File storageFile = this.getExternalFilesDir(null);
if (storageFile == null)
{
// no external storage so store on private path
storageFile = this.getFilesDir();
}
return storageFile;
}
public String get_fullPathToAudio() {
return get_fullPathToAudioAsFile() + File.separator + this.get_currentArticleAudioFileName();
}
String filePath = this.myApp.get_fullPathToAudio();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start();
All works well if I have an SD drive on my emulator but when I don't have an emulator the function
get_fullPathToAudioAsFile();
uses
getFilesDir();
instead of
getExternalFilesDir(null);
which returns a path to my private drive where my app is installed. I can see the file in
data/data/com.myapp/files/myfile.m4a
for example. Is there a different way to play an audio file if it's not on the SD drive?
The error messsage I get is
error(-1, -2147483648)
Could not open file null for playback
I solved it this way
FileInputStream fileInputStream = new FileInputStream(mFile);
mPlayer.setDataSource(fileInputStream.getFD());
mPlayer.prepare();
MediaPlayer error -2147483648 when playing file on internal storage
I am recording audio using Audio recorder (native app) of android.Using following code :
final Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
String path = Environment.getExternalStorageDirectory()
.toString();
File AudioFolder = new File(path + "/MyFolder/",
"RecordedAudio");
File audioFile = new File(AudioFolder, "myaudio.mp4");
Uri audioFileUri = Uri.fromFile(audioFile);
audioIntent.putExtra(MediaStore.EXTRA_OUTPUT, audioFileUri);
startActivityForResult(audioIntent, 0);
Audio recording is working fine with me. After successful recording It has been stored default in sdcard. I am not able to store it with my particular folder "MyFolder" that is located in sdcard so It might be store in /sdcard/Myfolder/RecordedAudio/myaudio.mp4.
What is going wrong with my above code.?
Please help me with your suggestions.
Thanks
I can't find in my books if there is way to convert string to resources path in android development.
For example:
String path = "R.raw.mediaFile";
MediaPlayer myPlayer;
myPlayer = MediaPlayer.create(this, ***<Some Expression>(path)***);
Please provide a short example.
Thanks in advance!
I think you're after
context.getResources().getIndentifier(...)
Converts a String reference to the int value. See here http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29
Another useful method once you've got the id is
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
You can set this as a datasource for the media player
myPlayer.setDatasource(aft.getFileDescriptor())
i'm trying to play a video with the sample of Video Player from Android developers: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html
I have a mp4 video (1.mp4) stored on the assets folder, but i dont know how to specify the path variable to point into that file
THis is the code i have:
public class DemoVideoActivity extends Activity {
/**
* TODO: Set the path variable to a streaming video URL or a local media
* file path.
*/
private String path = "";
private VideoView mVideoView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(
DemoVideoActivity.this,
"Please edit VideoViewDemo Activity, and set path"
+ " variable to your media file URL/path",
Toast.LENGTH_LONG).show();
} else {
/*
* Alternatively,for streaming media you can use
* mVideoView.setVideoURI(Uri.parse(URLstring));
*/
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
}
OK, This is solution for video from /res/raw folder, (Actually I got it from somewhere in SO and it works in my case)
Copy the video into your project's res/raw folder.
Video file must be in a supported format (3gp, wmv, mp4 ).
Make reference through the generated R statics - it will have the file extension removed: R.raw.my_video_file
VideoView videoView = (VideoView)this.findViewById(R.id.videoView)
String uri = "android.resource://" + getPackageName() + "/" + R.raw.my_video_file;
videoView.setVideoURI(Uri.parse(uri));
videoView.start();
And video from asset I never try that but, If you are use MediaPlayer then I think this will help you,
AssetFileDescriptor fileDes = getAssets().openFd(fileName);
player.setDataSource(fileDes.getFileDescriptor(),fileDes.getStartOffset(), fileDes.getLength());
Here player is media player's object.
Thanks.