0

I implemented a file chooser on a media player which returns the file paths for an .mp3 and an .srt on the externalSD. The audio plays fine. But when I call addTimedTextSource with the path to the .srt, it throws a null pointer exception. So, I put in an If(file.exists). It also returns a null. I tried moving the file to the internal SD with the same result. Any ideas?

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);
    buttonPause = (Button) findViewById(R.id.buttonPause);
    buttonPlay = (Button) findViewById(R.id.buttonPlay);

    Bundle bundle = getIntent().getExtras();
    if(bundle!=null) {
        String removeString = "file:";
        soundPath = bundle.getString("soundFile");
        subPath = bundle.getString("subFile");
        subPath = removeString(subPath,removeString);
        soundFile = new File(soundPath);
        subFile = new File(subPath);
    }
    player = new MediaPlayer();
    try {
        player.setDataSource(soundPath);
        player.setOnErrorListener(this);
        player.setOnPreparedListener(this);
        player.prepareAsync();

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public void onPrepared(MediaPlayer mp){
    mp.setOnTimedTextListener(this);
    if(subFile.exists() {
        try {
            player.addTimedTextSource(subFile.getAbsolutePath(), MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
        } catch (IOException e) {
            Log.v("Hey Here is a Problem: ", e.getMessage());
        }
        TrackInfo[] ti = player.getTrackInfo();
        for (int i = 0; i < ti.length; i++) {
            if (ti[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
                player.selectTrack(i);
                break;
            }
        }
    }else{
        onBackPressed();
    }
    mp.start();
}
Mars7272
  • 51
  • 4
  • 1
    Show some code, show some stacktrace... – ci_ Mar 02 '15 at 23:14
  • We will see once you provide more details. If even `file.exists()` produces null pointer exception, that means the file object itself is null – Lamorak Mar 02 '15 at 23:21
  • Sorry about that. I have added the Code. – Mars7272 Mar 03 '15 at 22:10
  • Hmm, I think I may have misunderstood something. subFile.exists() is returning true, but you're still getting a NullPointerExeception with player.addTimedTextSource. Is that correct? – iheanyi Mar 04 '15 at 19:57

1 Answers1

0

Edit: Rereading your question, it appears that subfile.exists() returns true, but you get a null pointer exception when you try to call player.addTimedTextSource().

Assuming player is not null, I'd like you to try this:

string fileStr = subfile.getAbsolutePath();
player.addTimedTextSource(fileStr, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);

I suspect that you may be running into an issue with the overloads of addTimedTextSource().


Old answer:

So, I put in an If(file.exists). It also returns a null. I tried moving the file to the internal SD with the same result. Any ideas?

There is no file at the provided path. Since you have moved the file, seems like you're mistyping the file name.

iheanyi
  • 3,107
  • 2
  • 23
  • 22
  • I'm actually pulling the file name from a file chooser class. I do the same with the audio file and it works fine. – Mars7272 Mar 04 '15 at 19:43