Reputation: 284
i have the following code:
animalGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
animalView.setImageResource(symbolIds[arg2]);
String imageName = getResources().getResourceEntryName(
symbolIds[arg2]);
Toast.makeText(getApplicationContext(), imageName,
Toast.LENGTH_LONG).show();
int audioId = getResources().getIdentifier(
imageName, null, getPackageName());
try {
MediaPlayer mp = MediaPlayer.create(
getApplicationContext(), audioId);
mp.start();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"not working", Toast.LENGTH_LONG).show();
}
}
});
When i run this code then i am getting correct imageName
.
All My mp3 files are in raw
folder.
i want to play mp3 file according to imagename.
suppose imageName='hardweell" then the hardweell.mp3 is must be play
The media is not playing. It is going to catch every time.
So how can i solve this issue?
Below is logs where file is crashed:
01-12 18:01:55.518: I/View(26260): Touch down dispatch to android.widget.ImageView{42813e88 VFED.... ..S..... 0,0-450,200}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=101.81146, y[0]=115.70969, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=20852614, downTime=20852614, deviceId=3, source=0x1002 }
01-12 18:01:55.524: I/View(26260): Touch down dispatch to android.widget.Gallery{42761308 VFED.... ......I. 0,0-450,200 #7f09000e app:id/animalGallery}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=101.81146, y[0]=115.70969, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=20852614, downTime=20852614, deviceId=3, source=0x1002 }
01-12 18:01:55.526: D/VelocityTracker(26260): Couldn't open '/dev/touch' (Permission denied)
01-12 18:01:55.526: D/VelocityTracker(26260): tpd read x fail: Bad file number
01-12 18:01:55.526: D/VelocityTracker(26260): tpd read y fail: Bad file number
01-12 18:01:55.602: I/View(26260): Touch up dispatch to android.widget.Gallery{42761308 VFED.... ......I. 0,0-450,200 #7f09000e app:id/animalGallery}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=101.81146, y[0]=115.70969, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=20852703, downTime=20852614, deviceId=3, source=0x1002 }
01-12 18:01:55.603: V/Provider/Settings(26260): from settings cache , name = sound_effects_enabled , value = 0
01-12 18:01:55.612: W/ResourceType(26260): No package identifier when getting value for resource number 0x00000000
01-12 18:01:55.629: D/GraphicBuffer(26260): create handle(0x60e6f328) (w:544, h:960, f:1)
01-12 18:01:55.634: D/OpenGLRenderer(26260): prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x616f2f80>
01-12 18:01:55.635: D/OpenGLRenderer(26260): finish <0x616f2f80>
01-12 18:01:55.685: D/GraphicBuffer(26260): create handle(0x60e74e70) (w:112, h:66, f:1)
01-12 18:01:58.045: D/dalvikvm(26260): threadid=11: exiting
01-12 18:01:58.045: D/dalvikvm(26260): threadid=11: bye!
01-12 18:01:59.114: D/GraphicBuffer(26260): close handle(0x60e74e70) (w:112 h:66 f:1)
01-12 18:01:59.173: D/GraphicBuffer(26260): create handle(0x61d828a8) (w:144, h:66, f:1)
01-12 18:02:02.618: D/GraphicBuffer(26260): close handle(0x61d828a8) (w:144 h:66 f:1)
01-12 18:02:34.102: D/ActivityThread(26260): ACT-AM_ON_PAUSE_CALLED ActivityRecord{426311c8 token=android.os.BinderProxy@426309e0 {com.example.kidzgk/com.example.kidzgk.AnimalsLearnPage}}
01-12 18:02:34.122: D/ActivityThread(26260): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@426309e0
01-12 18:02:34.124: D/ActivityThread(26260): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@426309e0
01-12 18:02:34.131: V/InputMethodManager(26260): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{426324a8 V.E..... R....... 0,0-540,960} ic=null tba=android.view.inputmethod.EditorInfo@428d09d0 controlFlags=#100
Upvotes: 0
Views: 618
Reputation: 132982
If file is inside raw
folder then prepare media player by passing video id using R.raw.<filename>
as:
int audioId = getResources().getIdentifier(imageName, null, getPackageName());
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),audioId);
Upvotes: 1