• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to play audio using this code in Grid View

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I have been following this code on andriod developer site. This code displays pictures in a grid. What I want to do now is to play different sond each time user clicks on the image. How can I do that?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//
Intent myIntent = null;
if(position==0){
//Play some sound...
//MediaPlayer sound= MediaPlayer.create(this,R.raw.alif);
// sound.start();

}
if(position==1){
//Want to play some sound here...
Toast.makeText(Test.this, "" + "Baa",Toast.LENGTH_SHORT).show();
}
}

});
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what the MediaPlayer.create and sound.start methods do, provided that raw/alif points to an audio file in a suitable format (maybe an MP3).
 
Ajoo Bar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
if(position==0){
//Play some sound...
MediaPlayer sound= MediaPlayer.create(this,R.raw.alif);
sound.start();

}

I get a compile error Invalid argument create(). Can I call it inside onItemClick method
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, do you have a valid sound file of that name in the proper directory location?
 
Ajoo Bar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I have a valid file, I can run using the same code out side the setOnItemClickListener() method, but I cant run it inside this method. I ge this error


The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new AdapterView.OnItemClickListener(){}, int)
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the first parameter must be a Context; if you use "this" inside of a OnItemClickListener, then that's not a Context.
 
Ajoo Bar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. So what should I pass instead of this.? Sorry It may be a stupid question, really trying to understand this concept
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can pass the Context to your OnItemClickListener implementation as a constructor argument.
 
Ajoo Bar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Here is what I did.

Created Instance variable
protected Context context;

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//
Intent myIntent = null;
MediaPlayer bismiallh2 =null;
if(position==0){
Toast.makeText(Alphabet.this, "" + "Alif",Toast.LENGTH_SHORT).show();
bismiallh2 = MediaPlayer.create(context,R.raw.alif);
bismiallh2.start();
}
if(position==1){
Toast.makeText(Alphabet.this, "" + "Baaa",Toast.LENGTH_SHORT).show();
bismiallh2 = MediaPlayer.create(context,R.raw.alif);
bismiallh2.start();

}
}

});
When I click on the image I get an error.
Logcat
04-06 17:10:20.181: DEBUG/AndroidRuntime(495): Shutting down VM
04-06 17:10:20.181: WARN/dalvikvm(495): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): FATAL EXCEPTION: main
04-06 17:10:20.720: ERROR/AndroidRuntime(495): java.lang.NullPointerException
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.media.MediaPlayer.create(MediaPlayer.java:662)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at com.bismiallah.alphabet.Alphabet$1.onItemClick(Alphabet.java:42)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.os.Handler.handleCallback(Handler.java:587)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.os.Looper.loop(Looper.java:123)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at java.lang.reflect.Method.invoke(Method.java:507)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 17:10:20.720: ERROR/AndroidRuntime(495): at dalvik.system.NativeStart.main(Native Method)
04-06 17:10:20.729: WARN/ActivityManager(62): Force finishing activity com.bismiallah.alphabet/.Alphabet
04-06 17:10:21.320: WARN/ActivityManager(62): Activity pause timeout for HistoryRecord{407592b0 com.bismiallah.alphabet/.Alphabet}

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what do you deduce from that?
 
Ajoo Bar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Null pointer exception... DO I have to context prob is null?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, *something* is null, that's for sure. You can do some debugging to find out *what* is null, or you can try to reason from the code you posted - which does not include anything that would set the context variable to anything other than null.
 
You showed up just in time for the waffles! And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic