Store audio files in an array - android

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

Related

Playing sequential videos from array in Android Studio

I am trying to play a series of videos in an array in Android, however when the below code runs only the last element / video of the array plays.
How can I loop through the array and play the videos one after the other?
I get the feeling that the continuation of the loop happens immediately after the videoView.start() command so only the last one plays.
Here is an approximation of my code...
VideoView videoView = (VideoView) findViewById(R.id.videoView);
String file_location = "path/to/my/files/"; // external storage
String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements
for(String filepath: filepaths){
String path = file_location + filepath;
videoView.setVideoPath(path);
videoView.start();
}
I have tried adding the setOnCompletionListener and putting the continue inside the onCompletion but the error is "continue outside of loop"
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
continue;
}
});
How can I play each video sequentially with as little / no gap in between?
Don't use loop because you are supposed to set the path of the next video only after previous one finished so instead of loop create a field called currentPlayingIndex make it increment after each video finishes and then set path from that ....
Like this
private int currentPlayingIndex; // Keep this as gloabal variable
VideoView videoView = (VideoView) findViewById(R.id.videoView);
String file_location = "path/to/my/files/"; // external storage
String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements
String path = file_location + filepaths[currentPlayingIndex];
videoView.setVideoPath(path);
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
currentPlayingIndex++; //Increment index here
if(currentPlayingIndex < filepaths.length)
{
String newPath = file_location + filepaths[currentPlayingIndex];
videoView.setVideoPath(newPath);
videoView.start();
}else {
//Add logic here when all videos are played
}
}
});
Note: I am not sure how long it will take to switch between videos ...
The first: You need to play *.mp4 in the array of index 0.
Then when the video play complet,use the player play the *.mp4 in the array of index next.
My English expression may not be very good, I hope you can understand

MediaPlayer with setAudioStreamType(AudioManager.STREAM_RING) not playing android

I am working on an Audio calling app and trying to play .mp3 file stored in raw folder.
I wanted this this file to be played as an Ringtone
For this I used mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
When I use this stream the .mp3 is not played (or may be playing I am not able to hear the sound )
If I don't specify any AudioStreamType , then the file is played as an MUSIC stream , which I don't want
Note: Volume of every type (Ringtone,Notification,Call,Music) is set to full in my device
My code
public MediaPlayer playRingtone(int fileName, boolean loopRingtone) {
mediaPlayer = MediaPlayer.create(mContext, fileName);
if (android.os.Build.VERSION.SDK_INT >= 21) {
AudioAttributes.Builder b = new AudioAttributes.Builder();
b.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
b.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN);
mediaPlayer.setAudioAttributes(b.build());
} else {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
}
mediaPlayer.setLooping(loopRingtone);
mediaPlayer.start();
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnCompletionListener(this);
return mediaPlayer;
}

Accessing an audio file in the RAW folder

I have created a new Java file within my project and i am trying to access a war audio file in my Raw folder. Does anyone have an idea what i am doing wrong?
MediaPlayer mediaPlayer;
Context context;
public Audio()
{
int resID = context.getResources().getIdentifier("morse","raw",context.getPackageName());
context.getResources().openRawResource(resID);
mediaPlayer = MediaPlayer.create(this.context, resID);
}
You have failed to start the media player. Try the following code,
MediaPlayer mediaPlayer;
Context context;
public Audio()
{
int resID = context.getResources().getIdentifier("morse","raw",context.getPackageName());
context.getResources().openRawResource(resID);
mediaPlayer = MediaPlayer.create(this.context, resID);
//start the media player
mediaPlayer.start();
}
Hope it helps!!!...
Instead of this:
int resID = context.getResources().getIdentifier("morse","raw",context.getPackageName());
context.getResources().openRawResource(resID);
mediaPlayer = MediaPlayer.create(this.context, resID);
Try This:
mediaPlayer = MediaPlayer.create(Your_Activity_Name.this,
R.raw.Audio_File_Name);
mediaPlayer.start();
Rename your audio file to .mp3 or .wav or some other audio formats.
Add your audio file to raw folder in res, res/raw/audio.mp3.
Clean your project.
Use the following:
mp = MediaPlayer.create(this, R.raw.audio);
mp.start();

Check which audio file is opened in MediaPlayer

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.

How to play music files in raw folder continuously

Creating music player related application,In raw folder having 5 mp3 files.want to play continuously and individually ,
else if (theText == song3) {
mediaPlayer = MediaPlayer.create(this, R.raw.hiii);
} else if (theText == song4) {
mediaPlayer = MediaPlayer.create(this, R.raw.hiiii);
}
This is how i am playing individual songs but how to play 5 songs continuously.Help me.
1.make an array of id if songs in raw folder.
int arr[]={R.id.hiii,R.id.Hiiii,...........}
2.use this array in your code
mediaPlayer=MediaPlayer.create(this, arr[i]);
3.implement setOnCompletionListener(Context); to start another song after one is over and loop it

Categories

Resources