• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps add audio for each image in the gallery!

wounded

Newbie
Hi everyone,
i have created a gallery with 9 images,i want to add sound to each image in the gallery,and these audios different from each.i have 9 items in res/raw .
i faced many mistakes,but did't acheive.:confused:
i hope to get help from you.:)
thnak you
 
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
TextView txtView=(TextView)view.findViewById(R.id.txt_view);
String fname=txtView.getText().toString().toLowerCase();
int resID=getResources().getIdentifier(fname, "raw", getPackageName());

MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
}
});

(?)
 
or how about this :

public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
MediaPlayer mPlayer2;
if(position==1)
{
mPlayer2= MediaPlayer.create(this, R.raw.song1);
mPlayer2.start();
}else it() .....
}
 
or how about this :

public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
MediaPlayer mPlayer2;
if(position==1)
{
mPlayer2= MediaPlayer.create(this, R.raw.song1);
mPlayer2.start();
}else it() .....
}


i got this error in:: .create(this, R.raw.one);

The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new AdapterView.OnItemClickListener(){}, int)


And what do you deduce from that?
 
herm :
Playing from a Raw Resource Perhaps the most common thing to want to do is play back media (notably sound) within your own applications. Doing this is easy:
  1. Put the sound (or other media resource) file into the res/raw folder of your project
  2. Create an instance of MediaPlayer, referencing that resource using MediaPlayer.create, and then call start() on the instance.
  3. To stop playback, call stop(). If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again. (create() calls prepare() the first time.)
To pause playback, call pause(). Resume playback from where you paused with start().

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

This method calls prepare for you, before you use it again you'll have to null the media player state by calling reset() and then after setting the new datasource call prepare().
 
thanks for the reply,i already use res/raw for the sounds .mp3 ,this my code to step 2 and still same error

public​
void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
//Toast.makeText(getBaseContext(),

//" " + (arg2+1) + "",

// Toast.LENGTH_SHORT).show();

imageView.setImageResource(pics[arg2]);
MediaPlayer mPlayer2;
if(arg2==0)
{
mPlayer2= MediaPlayer.create(
this, R.raw.zero);
mPlayer2.start();

}
else if(arg2==1)
{
mPlayer2= MediaPlayer.create(
this,R.raw.one);
mPlayer2.start();

}

..ets
 
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1); mp.start();
 
i got this error in:: .create(this, R.raw.one);

The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new AdapterView.OnItemClickListener(){}, int)


And what do you deduce from that?

To avoid hanging your UI thread, spawn another thread to prepare the MediaPlayer and notify the main thread when done. However, while you could write the threading logic yourself, this pattern is so common when using MediaPlayer that the framework supplies a convenient way to accomplish this task by using theprepareAsync() method. This method starts preparing the media in the background and returns immediately. When the media is done preparing, the onPrepared()method of the MediaPlayer.OnPreparedListener, configured throughsetOnPreparedListener() is called.
 
thank you in advance KJohns for your help
it works now..but one more thing ,another gallery contain about 30 images i cant use if-statment for the sound,it inaproprite solution.
 
Back
Top Bottom