我已经阅读了这两篇文章:
Should have subtitle controller already set Mediaplayer error Android
这个谈论忽略&抑制错误。不解决我的问题。
这一个:
MediaPlayer : Should have subtitle controller already set: KitKat
没有有效答案。
我遇到的问题是:音频用于播放,但我们必须更改我们的框架(我们删除了CrossWalk),现在音频不再播放。事实上,删除CrossWalk与我的媒体播放器代码无关。
所以我得到的错误连续发生两次。在我的代码中,我这样做:
_activeMediaPlayer.prepare();
_activeMediaPlayer.start();
当调用这两行中的任何一行时,它会抛出错误:
E / MediaPlayer:应该已经设置了字幕控制器
设置流类型不会以任何方式影响此类型。此外,它不会抛出异常但它也不会触发OnCompletionListener
_activeMediaPlayer.setOnCompletionListener
(
new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
_activeMediaPlayer = null;
Log.d("AudioPlayer", "Completed audio playing");
}
}
);
你知道为什么音频不能播放吗? (100%可以确认手机音频正常工作且音量最大)。这段代码以前工作了!
更新 这是我的整个音频播放课程。这个使用完美地工作。它播放的音频文件没有改变路径或格式。它确实停止了工作,没有抛出任何错误。
public class AudioPlayer
{
//region Variables
private static final String AUDIO_TYPE_DOWNLOADED = "downloaded";
private static final String AUDIO_TYPE_BUNDLED = "bundled";
private static final String AUDIO_BUNDLED_PATH = "www/audio/";
private static final String AUDIO_DOWNLOADED_PATH = "file:///data/data/co.app.app/Documents/";
private static AudioManager _audioManager;
private static AudioPlayer _singleton;
private static Activity _activity;
private static ArrayList<MediaPlayer> _audioList;
private static MediaPlayer _activeMediaPlayer = null;
//endregion
//region Initialization
/*
* Must be called before using Singleton. Will initialize with activity.
*/
public static void initialize(Activity activity)
{
_activity = activity;
_singleton = new AudioPlayer();
_audioList = new ArrayList<>();
_audioManager = SINGLETON().createAudioManager();
}
/**
* Access this to play audio.
*/
public static AudioPlayer SINGLETON()
{
if (_singleton == null)
{
System.out.println("Error audio helper not initialized, call Initialize first");
return null;
}
return _singleton;
}
/**
* Creates an audio manager.
*/
private AudioManager createAudioManager()
{
final AudioManager audioManager =
(AudioManager) _activity.getBaseContext().getSystemService(Context.AUDIO_SERVICE);
return audioManager;
}
//endregion
//region Audio Focus
/**
* Listener for audio focus changes.
*/
private AudioManager.OnAudioFocusChangeListener createAudioFocusChangeListener()
{
return new AudioManager.OnAudioFocusChangeListener()
{
public void onAudioFocusChange(int focusChange)
{
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
{
//TODO leave in case features are desired here
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
//TODO leave in case features are desired here
}
}
};
}
/*
* Sets audio focus to duck others.
*/
private int requestAudioFocus(AudioManager audioManager,
AudioManager.OnAudioFocusChangeListener audioFocusListener)
{
// Use the music stream and request permanent focus.
return audioManager.requestAudioFocus
(
audioFocusListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
);
}
//endregion
//region Playing & Queueing Audio
/*
* Enqueue audio for play.
*/
private static void enqueueAudio(String path, String audioType)
{
MediaPlayer mediaPlayer = new MediaPlayer();
if (audioType.equals(AUDIO_TYPE_BUNDLED))
{
try
{
AssetFileDescriptor afd =
_activity.getAssets().openFd(AUDIO_BUNDLED_PATH + path + ".m4a");
mediaPlayer.setDataSource
(
afd.getFileDescriptor(),
afd.getStartOffset(),
afd.getLength()
);
} catch (IOException e)
{
System.out.println("Error bundled file not found ");
e.printStackTrace();
return;
}
} else
{
Uri uri = Uri.parse(AUDIO_DOWNLOADED_PATH + path + ".m4a");
try
{
mediaPlayer.setDataSource(_activity,uri);
} catch (IOException e)
{
e.printStackTrace();
System.out.println("Could not find audio at " + path);
return;
}
}
_audioList.add(mediaPlayer);
}
/**
* Enqueues all audio then plays it.
* Example format: String json = "{\"audio\":[{\"path\":\"a\",\"type\":\"bundled\"},
* {\"path\":\"444-1438314705229\",\"type\":\"downloaded\"}]}";
*/
public static void playAudio(String json)
{
try
{
JSONObject rootJson = new JSONObject(json);
JSONArray arrayJson = rootJson.getJSONArray("audio");
for (int i = 0; i < arrayJson.length(); i++)
{
JSONObject audioJson = (JSONObject) arrayJson.get(i);
enqueueAudio(audioJson.getString("path"), audioJson.getString("type"));
}
} catch (Exception e)
{
System.err.println("JSON parsing error " + e.getMessage());
if (Bridge.audioCallback != null)
{
Bridge.audioCallback.error(e.getMessage());
}
}
playQueuedAudio();
}
/**
* Plays the top most queued audio (is called by the onComplete listener)
*/
private static void playQueuedAudio()
{
final AudioManager.OnAudioFocusChangeListener audioFocusListener =
SINGLETON().createAudioFocusChangeListener();
int result = SINGLETON().requestAudioFocus(_audioManager, audioFocusListener);
if (_audioList.isEmpty())
{
_audioManager.abandonAudioFocus(audioFocusListener);
if (Bridge.audioCallback != null)
{
Bridge.audioCallback.success();
}
return;
}
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
System.err.println("Could not gain audio focus, not playing audio");
return;
}
try
{
_activeMediaPlayer = _audioList.get(0);
_audioList.remove(0);
_activeMediaPlayer.prepare();
_activeMediaPlayer.start();
_activeMediaPlayer.setOnCompletionListener
(
new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
_activeMediaPlayer = null;
playQueuedAudio();
}
}
);
} catch (IOException e)
{
String message = "Unable to open media file: " + e.getMessage();
System.err.println(message);
if(Bridge.audioCallback != null)
{
Bridge.audioCallback.error(message);
}
}
}
/**
* Stops audio.
*/
public static void stopAudio(CallbackContext callbackContext)
{
if (_activeMediaPlayer != null && _activeMediaPlayer.isPlaying())
{
_activeMediaPlayer.stop();
}
_audioList.clear();
_activeMediaPlayer = null;
callbackContext.success();
}
//endregion
}
最后一个注释
作为测试,我创建了一个测试活动,只要安装了应用程序就将其写入其中:
MediaPlayer mediaPlayer = MediaPlayer.create(context,R.raw.test);
mediaPlayer.start();
这也行不通。这里正在发生一些事情......
答案 0 :(得分:1)
经过大量研究后,我得出结论,用于编码音频文件的编解码器已损坏。 m4a适用于Android,但似乎有些编解码器无法正常工作(标题部分可能会被我搞砸了)。
因此我将所有内容转换为mp3,现在效果很好。
答案 1 :(得分:0)
这是在模拟器(Nexus S API 22)中运行音频应用程序时发生的。
我做了一个很酷的启动,错误消失了。不再崩溃。