Is there any way to programmatically know which audio file opened?
I am using an array of audio files.
My code:
int[] audios = {R.raw.no, R.raw.yes};
Random r = new Random();
audio = MediaPlayer.create(this,audios[r.nextInt(audios.length)]);
audio.start();
Check which audio had been loaded:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
if (audio.equals(R.raw.no)) {
text.setText("No is loaded");}
else {text.setText("Another file is loaded");}
break;
}
But this dont work..
Add an int value for getting info about randomly opened files.
int currentaudio;
currentaudio = audios[r.nextInt(audios.length)];
It will output which file is loaded.
Related
How do i get the list of all saved text files in (storage/emulated/0/Notes, in this case "Notes" is my folder where all my saved text files are located) and make them available in a new activity(generate a textView for every single file to be printed). i want all this to happen when for instance an "Open" button is clicked. THANKS in advance.
Try this code..
// take below variable as global level of class
public List<String> filesNames=new ArrayList<>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// gets the files in the directory it fatch internal storage
File fileDirectory = new File(Environment.getDataDirectory()+"/YourDirectory/");
// below code for fatch sd card files on define folder;
File fileDirectorySD = new File(Environment.getExternalStorageDirectory()+"/YourDirectory/");
// lists all the files into an array
File[] dirFiles = fileDirectory.listFiles();
if (dirFiles.length != 0) {
// loops through the array of files, outputing the name to console
for (int ii = 0; ii < dirFiles.length; ii++) {
String fileOutput = dirFiles[ii].toString();
filesNames.add(fileOutput);
System.out.println(fileOutput);
}
}
}
});
and if you are run app in marshmallow above device then give read permission and also handling permission model..
so I'm building an interface on Android, where the user enters a word and then IF the word exists in the databank, then a button appears and when the user clicks on it "OnClick= buttonClicked" - the sound is played.
There are lots of examples that deal with a specific string names as parameters for the MediaPlayer, but I haven't seen any yet that deals with a string variable instead.
Here is what I mean:
If you want to play this specific audio that plays an airplane sound. You would create a method that handles the button click like that:
//play airplance.mp3 inside of res/raw:
public void buttonClicked(View view) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.airplane);
mp.start();
}
that works.
now, if instead of specifying the String name, i'd like to use a String variable myInput:
EditText editText = (EditText) findViewById(R.id.input);
String myInput = editText.getText().toString();
how should I define it in the media player? cause the following doesn't work:
public void buttonClicked(View view) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.myInput);
mp.start();
}
I also tried by using getResources().getIdentifier():
public void buttonClicked(View view) {
int resID=getResources().getIdentifier(myInput, "raw", this.getPackageName());
MediaPlayer mp = MediaPlayer.create(this, resID);
mp.start();
}
but also here the app crashes!
anybody can help? would very much appreciate that!
After long struggle I finally found the answer.
If myInput is the variable, then it is possible to use a String of the path , parse it to Uri and finally pass it into create(). I also used try-catch because I noticed that if the file is not found in the folder, then the app crashes. Here is the solution that worked for me:
String uriPath = "android.resource://" + getPackageName() + "/raw/" + myInput;
Uri uri = Uri.parse(uriPath);
mp = MediaPlayer.create(this, uri);
try {
mp.start();
}
catch (Exception e) {}
Im working on an some app where I need to take 3 images before I submit them and the related data.
I have the following code :
Listeners :
View.OnClickListener imgListeners = new View.OnClickListener() {
#Override
public void onClick(View v) {
String sSuffix = "_PIC";
String sElementName = poleId.getText().toString();
switch (v.getId()) {
case R.id.ImagePoleId:
sSuffix = "_ID";
break;
case R.id.ImagePole:
sSuffix = "_POLE";
break;
case R.id.ImagePoleBulbs:
sSuffix = "_BULBS";
break;
}
takeCameraPic(sElementName, sSuffix + ".jpg");
}
};
Setting the listeners :
idImgButton.setOnClickListener(imgListeners);
poleImgButton.setOnClickListener(imgListeners);
bulbsImgButton.setOnClickListener(imgListeners);
And the function used in the listeners :
private void takeCameraPic(String elementName ,String picNameSuffix) {
if (elementName.equals("")) return;
idImgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), elementName + picNameSuffix);
Uri idImgUri = Uri.fromFile(idImgFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, idImgUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 0);
}
When I enter the app, everything works fine, the camera pops up, I can take the image and go back.
But when I look for the image in the entire internal storage, its just not there. I've checked the DCIM \ PICTURES folders and nothing.
The only picture that was saved using the app says in the pic information that it was saved on the following folder :
/storage/emulated/0/Pictures
Try using Environment.DIRECTORY_DCIM instead of Environment.DIRECTORY_PICTURES this gets you to
/storage/emulated/0/DCIM
folder. You can then create Pictires folder and save image there.
I am using the jAudioTagger library to edit metadata of songs from my music player. The function to edit the tags is:
save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
File src = new File(filepath);
songName=song.getText().toString();
artistName=artist.getText().toString();
albumName=album.getText().toString();
try{
TagOptionSingleton.getInstance().setAndroid(true);
AudioFile f = AudioFileIO.read(src);
f.setTag(new ID3v23Tag());
Tag tag=f.getTag();
tag.setField(FieldKey.TITLE,songName);
tag.setField(FieldKey.ARTIST,artistName);
tag.setField(FieldKey.ALBUM,albumName);
f.commit();
Toast.makeText(getApplicationContext(),"Done",Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
}
});
However, after updating the tags, the change is not reflected anywhere. Even after refreshing my music library, the changes are not shown. How do I update the MediaStore to reflect the changes I've made to the tags so that all music players have access to the edited tags?
Instead of audioFile.commit(); use AudioFileIO.write(audioFile);
If it doesn't work, use this method to scan your media
private void scanMedia(String path) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
getContext().sendBroadcast(scanFileIntent);
}
I am working on an offline android quiz app and i want to store audio files in an array and play audio for different questions in a quiz. Can array be used to store audio files ? If Yes,How?
Thanx to all who tried, i solved this probllem myself:
int [] songs;
MediaPlayer mediaPlayer;
int current_index = 0;
mediaPlayer = MediaPlayer.create(this, songs[current_index]);
Button play_button = (Button)this.findViewById(R.id.play);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mediaPlayer.start();
current_index ;
}
});
Store the audio file in the array as shown and put the audio files in raw folder
res/raw
Array of audio
int[] clips= { R.raw.button_a, R.raw.button_2, R.raw.button_3, R.raw.button_4, R.raw.button_5, R.raw.button_6 };
Pick up the audio from this array