aspose file tools*
The moose likes Android and the fly likes How to play audio using this code in Grid View Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to play audio using this code in Grid View" Watch "How to play audio using this code in Grid View" New topic
Author

How to play audio using this code in Grid View

Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
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();
}
}

});
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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).


Ping & DNS - my free Android networking tools app
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
Well, do you have a valid sound file of that name in the proper directory location?
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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

Joined: Mar 28, 2011
Posts: 30
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
You can pass the Context to your OnItemClickListener implementation as a constructor argument.
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
And what do you deduce from that?
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
Null pointer exception... DO I have to context prob is null?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to play audio using this code in Grid View