user1205088
user1205088

Reputation: 229

Cannot play mpg file in android

I want to play .mpg file from MediaPlayer in android. But I am not able to play. This is my code.

    File file =  new File("abc.mpg");
    MediaPlayer mp = MediaPlayer.create(MainActivity.this, Uri.fromFile(file.getAbsoluteFile()));
    mp.seekTo(0);
    mp.start();

I get null pointer error on mp.seekTo(0) function.

Upvotes: 1

Views: 1671

Answers (3)

SphericalCow
SphericalCow

Reputation: 365

I don't think android supports mpg format. You can always build a decoder yourself or look at some libs for it. See if exo player has formats you need. Also checkout Vitamio SDK

Upvotes: 1

krunal shah
krunal shah

Reputation: 364

mpeg can be compressing the video over a range of different formats/algorithms/codecs and some are supported some are not. 3gp is just one and it is supported (although a very poor format).

Try encoding a video yourself that you'll see all different options. Usually mp4 on H264 works flawlessly on mobiles.

Upvotes: 1

Grobbed
Grobbed

Reputation: 337

Are you sure that you have the correct path specified. Try debugging it by making an if statement:

File file =  new File("abc.mpg");
MediaPlayer mp = MediaPlayer.create(MainActivity.this, Uri.fromFile(file.getAbsoluteFile()));

if(file != null && mp != null) {
     mp.seekTo(0);
     mp.start();
}

Let me know what happens, if the problem goes away it means either mp or file are equal to null.

Upvotes: 1

Related Questions