在Android媒体播放器中添加字幕轨道

时间:2012-07-25 06:22:07

标签: android android-mediaplayer

嗨,我是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中显示字幕轨道的想法。

感谢

1 个答案:

答案 0 :(得分:2)

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
   mp.addTimedTextSource(...
}