質問

i have some audio files (m4a/mp3) in res/raw folder. and my activity has a listview which inflates such that each list item has a textview, an imageview and a button. i want an audio file to play upon clicking this button. How do i achieve this? i tried MediaPlayer.create(this, R.raw.example); but this line of code shows an error since its in the ListAdapter class and not the Activity class. Any help would be appreciated, any hint on how to do it and i can do the coding.`

役に立ちましたか?

解決

You can obtain the Context in several ways.

You can get it from the current view in the getView() method of the adapter:

view.getContext();

Or you can pass the context to the adapter's constructor, remembering that a good practice involves using a WeakReference, so that you do not generate leaks.

Once you do have the Context, the code you are using just works:

player = MediaPlayer.create(context, R.raw.example);
player.prepare();

If you are using a WeakReference, your code will be:

player = MediaPlayer.create(mContext.get(), R.raw.example);
player.prepare();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top