Ibrahim Iqbal
Ibrahim Iqbal

Reputation: 594

Play audio from fragment

I'm trying to play audio from within a fragment but it's giving me errors on Media Player. The error is like this

Cannot resolve method 'create(com.xyz.packageName.ConnectFragment,int)'

Code line is this

MediaPlayer mPlayer = MediaPlayer.create(ConnectFragment.this,R.raw.thenights);

Here what should be in context's portion of constructor parameter - (ConnectFragment.this,R.raw.thenights).

Error after building project is

Error:(39, 25) error: variable declaration not allowed here

Thanks in advance.

Upvotes: 0

Views: 1427

Answers (2)

Bhavesh Desai
Bhavesh Desai

Reputation: 743

Try this,

MediaPlayer mediaPlayer = MediaPlayer.create(getContext(), R.raw.thenights);  

//OR
MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), R.raw.thenights); 

//OR
MediaPlayer mediaPlayer = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.thenights);

Upvotes: 2

antonis_st
antonis_st

Reputation: 468

MediaPlayer accepts applicationcontext so you can do

MediaPlayer mPlayer = MediaPlayer.create(getActivity().getApplicationContext(),R.raw.thenights);

Upvotes: 0

Related Questions