Android MediaPlayer:应该已经设置了字幕控制器:KitKat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21790824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:04:37  来源:igfitidea点击:

MediaPlayer : Should have subtitle controller already set: KitKat

androidandroid-mediaplayerandroid-4.4-kitkat

提问by bneigher

I am having an odd issue where my audio file sometimes plays and sometimes does not play. The catch is that when it decides to not play, the DDMS gives me an:

我有一个奇怪的问题,我的音频文件有时播放有时不播放。问题是,当它决定不玩时,DDMS 会给我一个:

E/MediaPlayer﹕ Should have subtitle controller already set

Because this is one-to-one with the music not playing, I have determined that this is probably the issue...

因为这是一对一的,没有播放音乐,我已经确定这可能是问题所在......

If the music is not playing and I hit the volume button it begins to play. If I wait about 30 seconds of no-play, it begins to start again (not looping).

如果音乐没有播放而我按下音量按钮,它就会开始播放。如果我等待大约 30 秒的无播放,它会重新开始(不循环)。

Whats going on here? I am on KitKat using

这里发生了什么?我在 KitKat 上使用

        player = new MediaPlayer();
        AssetFileDescriptor afd = null;
        try {
            afd = getAssets().openFd("Theme.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.setLooping(true); //restart playback end reached
        //player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
        player.start(); //start play back

回答by Will Angley

Looking at a previous discussionon StackOverflow, and the referenced Android commitwhere this was introduced, the code above might not completely initialize the MediaPlayerobject.

看看之前关于 StackOverflow 的讨论,以及引用的Android 提交,上面的代码可能没有完全初始化MediaPlayer对象。

The KitKat example codefor media playback suggests that you should call:

媒体播放的KitKat 示例代码建议您应该调用:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

immediately after you construct the MediaPlayer, and before you call its setDataSourcemethod.

在您构造MediaPlayer, 之后和调用其setDataSource方法之前。

回答by user3777321

I had the same issue and I fixed it by adding the following right after instantiating MediaPlayer.

我遇到了同样的问题,我在实例化 MediaPlayer 后通过添加以下内容来修复它。

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        if (mp == mediaPlayer) {
                            mediaPlayer.start();
                        }
                    }
                });

Previously I was implementing MediaPlayer.OnPreparedListener and overriding onPrepared() but it didn't work.

以前我正在实施 MediaPlayer.OnPreparedListener 并覆盖 onPrepared() 但它不起作用。

I hope this helps!

我希望这有帮助!

回答by bneigher

Its been a long time since I was working on this app. Here is what I ended up doing to get this to work. (Tested on KitKat and Lollipop). I think switching from MediaPlayer to APMediaPlayer was part of the trick.

自从我开发这个应用程序以来已经有很长时间了。这是我最终为使其工作而做的事情。(在 KitKat 和 Lollipop 上测试)。我认为从 MediaPlayer 切换到 APMediaPlayer 是技巧的一部分。

@Override
public void onDestroy() {
    if(player != null) {
        player.release();
        player = null;
    }
    super.onDestroy();
}


@Override
public void onStart() {
    super.onStart();
    if(player != null) {
        player.start();
    }
    else {
        player = new APMediaPlayer(this); //create new APMediaPlayer
        player.setMediaFile("Theme.mp3"); //set the file (files are in data folder)
        player.start(); //start play back
        player.setLooping(true); //restart playback end reached
        player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
    }

}

@Override
public void onResume() {
    super.onResume();
    if(player != null) {
        player.start();
    }

}

回答by wblaschko

This should fix your problem (did for me): Replace the line that says "player.start()" following the rest of your code with an async callback like so:

这应该可以解决你的问题(为我做的):用一个异步回调替换你的代码后面的“player.start()”行,如下所示:

player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }
});

This error is just a Log.e, not a real error. It shouldn't cause your player to not play, I'm guessing it's just because the player hadn't finished preparing when you try to call start().

此错误只是 Log.e,而不是真正的错误。它不应该导致您的播放器无法播放,我猜这只是因为您尝试调用 start() 时播放器还没有完成准备。

E/MediaPlayer﹕ Should have subtitle controller already set

回答by Lokesh G

set in manifest file may help you

在清单文件中设置可能会帮助你

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />