2

嗨,我是 Android 开发的新手。我想在我的 android 应用程序中为媒体播放器添加字幕。我已经搜索并找到了一个方法,即 addTimedTextSource(...) 但它给出了错误 java.lang.NoSuchMethodError: android.media.MediaPlayer.addTimedTextSource

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.video_view);
        mp = new MediaPlayer();
//        vw = (VideoView)findViewById(R.id.video_intent);
        Intent intent = getIntent();
        path = intent.getStringExtra("path");
        Log.d("File path ::::::::::::::::::",path);

        try {
            mp.setDataSource("file://"+path);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp.start();
        String mimeType = getMimeType("file://mnt/sdcard/BarbieGirl.srt");
//      mp.selectTrack(index);
        try {
            mp.addTimedTextSource(path, mimeType);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub

    }

谁能告诉我如何使用它或任何关于如何在 android 中显示字幕轨道的想法。

谢谢

4

1 回答 1

3

如果您收到该错误,可能是因为您在低于 SDK 16 (Jelly Bean) 的设备上运行此代码。您应该将其包装在 SDK 门中:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
   mp.addTimedTextSource(...
}
于 2012-07-28T19:38:20.597 回答