Should have subtitle controller already set Mediaplayer error Android
A developer recently added subtitle support to VideoView.
When the MediaPlayer
starts playing a music (or other source), it checks if there is a SubtitleController and shows this message if it's not set.
It doesn't seem to care about if the source you want to play is a music or video. Not sure why he did that.
Short answer: Don't care about this "Exception".
Edit :
Still present in Lollipop,
If MediaPlayer
is only used to play audio files and you really want to remove these errors in the logcat, the code bellow set an empty SubtitleController
to the MediaPlayer
.
It should not be used in production environment and may have some side effects.
static MediaPlayer getMediaPlayer(Context context){
MediaPlayer mediaplayer = new MediaPlayer();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
return mediaplayer;
}
try {
Class<?> cMediaTimeProvider = Class.forName( "android.media.MediaTimeProvider" );
Class<?> cSubtitleController = Class.forName( "android.media.SubtitleController" );
Class<?> iSubtitleControllerAnchor = Class.forName( "android.media.SubtitleController$Anchor" );
Class<?> iSubtitleControllerListener = Class.forName( "android.media.SubtitleController$Listener" );
Constructor constructor = cSubtitleController.getConstructor(new Class[]{Context.class, cMediaTimeProvider, iSubtitleControllerListener});
Object subtitleInstance = constructor.newInstance(context, null, null);
Field f = cSubtitleController.getDeclaredField("mHandler");
f.setAccessible(true);
try {
f.set(subtitleInstance, new Handler());
}
catch (IllegalAccessException e) {return mediaplayer;}
finally {
f.setAccessible(false);
}
Method setsubtitleanchor = mediaplayer.getClass().getMethod("setSubtitleAnchor", cSubtitleController, iSubtitleControllerAnchor);
setsubtitleanchor.invoke(mediaplayer, subtitleInstance, null);
//Log.e("", "subtitle is setted :p");
} catch (Exception e) {}
return mediaplayer;
}
This code is trying to do the following from the hidden API
SubtitleController sc = new SubtitleController(context, null, null);
sc.mHandler = new Handler();
mediaplayer.setSubtitleAnchor(sc, null)
Android force close Should have subtitle controller already set
Plse try below code :
package com.zihadrizkyef.belajaraudiocapture;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button btnPlay, btnStop, btnStart;
MediaRecorder myAudioRecorder;
String outputFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (Button) findViewById(R.id.btnRecordPlay);
btnStart = (Button) findViewById(R.id.btnRecordStart);
btnStop = (Button) findViewById(R.id.btnRecordStop);
btnPlay.setEnabled(false);
btnStop.setEnabled(false);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gpp";
//Creating MediaRecorder and specifying audio source, output format, encoder & output format
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IOException | IllegalStateException e) {
e.printStackTrace();
}
btnStart.setEnabled(false);
btnStop.setEnabled(true);
Toast.makeText(MainActivity.this, "Recording started", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
/*myAudioRecorder = null;*/
btnPlay.setEnabled(true);
btnStart.setEnabled(true);
btnStop.setEnabled(false);
Toast.makeText(MainActivity.this, "Audio recorded successfully", Toast.LENGTH_SHORT).show();
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(MainActivity.this, "Playing audio", Toast.LENGTH_SHORT).show();
}
});
}
}
In here what i done is that i created MediaRecorder and specifying audio source, output format... all those things for each Start Button click
Error E/MediaPlayer: Should have subtitle controller already set Causes No Audio To Play
After much research I have concluded that the codec used to encode the audio file was corrupt. m4a works for Android but it seems some codecs don't work properly (the header portion can get messed up as far as I can tell).
As such I converted all to mp3 and it works fine now.
Related Topics
Android - Drawable with Rounded Corners at the Top Only
Android: Scrolling an Imageview
Android Webview Style Background-Color:Transparent Ignored on Android 2.2
How to Determine If Network Type Is 2G, 3G or 4G
Change Toolbar Color in Appcompat 21
How to Add a Library (Android-Support-V7-Appcompat) in Intellij Idea
Selector on Background Color of Textview
Textwatcher for More Than One Edittext
How to Rename the Android Package Name
Should Have Subtitle Controller Already Set Mediaplayer Error Android
Securityexception: Caller Uid Xxxx Is Different Than the Authenticator's Uid
Android Studio:How to Uninstall APK (Or Execute Adb Command) Automatically Before Run or Debug
How to Fix "Unexpected Element <Queries> Found in <Manifest>" Error
Xamarin.Forms Listview: Set the Highlight Color of a Tapped Item
Monodroid: Error When Calling Constructor of Custom View - Twodscrollview
Changing Background Color of Selected Item in Recyclerview