我正在使用 PhoneGap (Cordova) 并尝试播放单个声音文件,然后在导航到不同页面时正确释放资源。
这就是我播放声音文件的方式
var my_media = null;
function playAudio(src) {
// Create Media object from src
if(my_media == null){
my_media = new Media(src, onSoundSuccess, onSoundError);
}
// Play audio
my_media.play();
}
// Audio - onSuccess Callback
//
function onSoundSuccess() {
console.log("Audio played");
}
// Audio - onError Callback
//
function onSoundError(error) {
console.log('sound error - code: ' + error.code);
console.log('sound error - message: ' + error.message);
if(my_media != null){
my_media.release();
}
}
这是我离开页面时调用的代码
if(my_media != null){
my_media.release();
}
但是,在离开页面并释放资源后,我总是在 logcat 中收到以下错误
10-09 12:09:07.843: D/MediaPlayer(32246): release() in
10-09 12:09:07.853: D/MediaPlayer(32246): release() out
10-09 12:09:07.853: V/MediaRecorderJNI(32246): native_reset
10-09 12:09:07.853: V/MediaRecorderJNI(32246): getMediaRecorder E
10-09 12:09:07.853: V/MediaRecorderJNI(32246): process_media_recorder_call
10-09 12:09:07.853: V/MediaRecorderJNI(32246): release
10-09 12:09:07.863: V/MediaRecorderJNI(32246): setMediaRecorder E: mr = null
10-09 12:09:07.863: V/MediaRecorderJNI(32246): setMediaRecorder X
10-09 12:09:07.903: D/CordovaWebView(32246): >>> loadUrl(file:///android_asset/www/page.html)
10-09 12:09:07.903: D/CordovaWebView(32246): >>> loadUrlNow()
10-09 12:09:07.903: W/System.err(32246): java.net.SocketException: Socket closed
10-09 12:09:07.903: W/System.err(32246): at org.apache.harmony.luni.platform.OSNetworkSystem.accept(Native Method)
10-09 12:09:07.903: W/System.err(32246): at dalvik.system.BlockGuard$WrappedNetworkSystem.accept(BlockGuard.java:262)
10-09 12:09:07.903: W/System.err(32246): at org.apache.harmony.luni.net.PlainSocketImpl.accept(PlainSocketImpl.java:95)
10-09 12:09:07.903: W/System.err(32246): at java.net.ServerSocket.implAccept(ServerSocket.java:264)
10-09 12:09:07.903: W/System.err(32246): at java.net.ServerSocket.accept(ServerSocket.java:150)
10-09 12:09:07.903: W/System.err(32246): at org.apache.cordova.CallbackServer.run(CallbackServer.java:195)
10-09 12:09:07.903: W/System.err(32246): at java.lang.Thread.run(Thread.java:1027)
显然我做错了什么,但我不确定它可能是什么。
我尝试不释放使错误消失的媒体对象,但是在使用该应用程序一段时间后,由于资源堆积且未正确释放而出现错误。
任何帮助将非常感激。
谢谢