addTimedTextSource上的Android空指针



我在媒体播放器上实现了一个文件选择器,它返回externalSD上.mp3和.srt的文件路径。音频播放良好。但是,当我使用.srt的路径调用addTimedTextSource时,它会抛出一个空指针异常。所以,我放入了一个If(file.exists)。它还返回一个null。我尝试将文件移动到内部SD,结果相同。有什么想法吗?

  @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();
}

编辑:重读您的问题,子文件.exists()似乎返回true,但当您尝试调用player.addTimedTextSource().时,会得到一个空指针异常

假设玩家不是空的,我想让你试试这个:

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

我怀疑您可能遇到了addTimedTextSource()重载的问题。


老答案:

所以,我放入了一个If(file.exists)。它还返回一个null。我试过了以相同的结果将文件移动到内部SD。有什么想法吗?

提供的路径中没有文件。由于您已经移动了文件,所以您似乎键入了错误的文件名。

最新更新