user1927448
user1927448

Reputation: 41

trying Using Mediaplayer within a Fragment

I'm a beginner of programming, and trying to use MediaPlayer in a Fragment, but have the same problem following:

Using Mediaplayer within a Fragment

this person seemed solve the problem so quickly, but I couldn't understand. please somebody help me

What does it mean by "Use getActivity()" and "called after onAttach()"?
I put "getActivity()" in MediaPlayer.create() method like below:

MediaPlayer mp = MediaPlayer.create( getActivity , resId ); 

Then compile error was disappeared but I got error when I try to turn on the application on a device. What should I do?

Does the Class have to extends Activity, Though I want to use Fragment class ?
Does the Class have to have internal class which extends fragment??
I'm confusing.

I found the following URL...
http://developer.android.com/guide/components/fragments.html How do I add a Fragment to an Activity with a programmatically created content view

Upvotes: 2

Views: 6165

Answers (2)

Marcin S.
Marcin S.

Reputation: 11191

Use getActivity().getBaseContext()

Upvotes: 2

pvyParts
pvyParts

Reputation: 231

did you put the ()

MediaPlayer mp = MediaPlayer.create( getActivity() , resId );

the getActivity gives the Media Player the context it needs. think of it like using this from within a activity

//in an activity you would do this
MediaPlayer mp = MediaPlayer.create( this , resId );

Upvotes: 2

Related Questions