0

I want to create a MediaPlayer inside my Fragment but I don't want to specify the Uri when at the time I create a player. When I try to use the default constructor (new Mediaplayer()) it doesn't work within a Fragment (only within the Activity) and using Mediaplayer.create() can't be called with just the Context and without an Uri.

Of course, I could create the MediaPlayer inside my main activity and then pass it into the Fragment but that seems a bit dirty. Is there a clean way for me to create my MediaPlayer?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • sure: use `setDataSource` later on – pskink May 08 '18 at 06:06
  • @pskink : The problem isn't passing the DataSource into the MediaPlayer but creating it. – Christian May 08 '18 at 06:51
  • it has default constructor, hasn't it (`new MedialPalyer()`)? so what problem can you see? – pskink May 08 '18 at 06:52
  • @pskink : The default constructor only works when called within a Context. – Christian May 08 '18 at 07:25
  • @pskink : Unfortunately, the fact that I don't pass a Context param doesn't mean that it doesn't interact with the Context. If it would take a Context param it would be easy to pass another Context into it. – Christian May 08 '18 at 07:29
  • This is rather unclear. Please explain exactly how the default constructor isn't working for you. Some of your statements aren't really making sense. For example, "The default constructor only works when called within a Context." Well, that's just not true. What's leading you to think that? Are you getting some sort of compilation error? Or maybe a runtime Exception? – Mike M. May 08 '18 at 07:50
  • @MikeM. If I create the MediaPlayer in the Fragment it shows a black background. If I instead create it in the Activity it the video plays normally. – Christian May 08 '18 at 08:05
  • If this is a follow-up to [your previous question](https://stackoverflow.com/q/50181565), it sounds like you might've forgotten to add the `Callback` on the `SurfaceView`'s `SurfaceHolder`, or possibly to `setDisplay()` on the `MediaPlayer` in `surfaceCreated()`. – Mike M. May 08 '18 at 08:18
  • 1
    Just ran a quick test. No difference in a `Fragment` than in an `Activity`, as long as you get everything. – Mike M. May 08 '18 at 08:32

0 Answers0