I'm developing an AudioBook player for Android. For the playing part, I've used MediaPlayer class which works perfectly for some minutes but then always suddenly fails and no voice could be heard.
Even if I stop and issue a new request (read new chapter) it doesn't work. However the passing of time seems natural (for example playing a 10 seconds clip takes 10 seconds to finish) but there is absolutely no voice!
Does someone have any clues what might cause this problem?
Here's the relevant parts of my MediaPlayer usage.
And here's the logcat around the place it mutes.
EDIT:
By analyzing the logcat, I found out that this part is showing after the muting has happened:
E/AudioFlinger( 146): no more track names available
E/AudioTrack( 146): AudioFlinger could not create track, status: -12
E/AudioSink( 146): Unable to create audio track
E/AwesomePlayer( 146): deleting Audio Player - start failed
E/MediaPlayer( 5558): error (-19, 0)
***V/MainActivity( 5558): onNewPage()
E/MediaPlayer( 5558): Error (-19,0)
E/Sensors ( 285): sensors_poll_context_t::pollEvents, line 202: receive event #### i=2, nb=0
E/Sensors ( 285): sensors_poll_context_t::pollEvents, line 202: receive event #### i=2, nb=0
E/Sensors ( 285): sensors_poll_context_t::pollEvents, line 202: receive event #### i=2, nb=0
E/Sensors ( 285): sensors_poll_context_t::pollEvents, line 202: receive event #### i=2, nb=0
***V/MediaPlayerWrapper( 5558): stop()
***D/MediaPlayerWrapper( 5558): Stopping player...
E/MediaPlayer( 5558): stop called in state 0
E/MediaPlayer( 5558): error (-38, 0)
The lines with three * at beginning are my messages, others are frameworks'.
The problem was simply because of not releasing the media player object; as these answers (a,b,c) suggested already.
mMediaPlayer.release();
Related
I'm trying to add HMS automatic speech recognition (ASR) to my app. I already have SpeechRecognizer implemented, but it requires GMS to work.
The current HMS implementation works on a non-huawei device with HMS core installed, but does not work on my Huawei Mediapad T5.
Things I've tried
The methods are called from different threads (main thread and graphics thread), so I've tried synchronizing the methods on a lock or posting a Runnable to the activity handler, without making much a difference. I.E., wrapping the functions in synchronized(lock) or activity.post.
Code:
fun init(activity: Activity)
speechRecognizer = MLAsrRecognizer.createAsrRecognizer(activity)
speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
.putExtra(
MLAsrCaptureConstants.LANGUAGE,
"en-US"
)
.putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_ALLINONE)
speechRecognizer?.setAsrListener(listener)
fun startListening()
speechRecognizer?.startRecognizing(speechIntent)
fun destroy()
speechRecognizer?.destroy()
Logs
4945-4945 W/HmsSpeechRecognitionHolder#c1cafe: init on Thread[main,5,main]
4945-4945 W/InputMethodManager: startInputReason = 1
4945-4945 W/HmsSpeechRecognitionHolder#c1cafe: startListening on Thread[main,5,main]
4945-4945 E/HaLogProvider: forbiddenHiLog openHa = false
4945-4945 E/HaLogProvider: forbiddenHiLog.getVenderCountry=ca
4945-4945 E/HaLogProvider: forbiddenHiLog openHa = false
4945-4945 E/MLASR_HaAdapter_MLKitAsr: mEventsToBeReported: has no response event isInfoGatherStart:falsemsg: 0
4945-4945 E/HwCustAudioRecordImpl: isOpenEC : false
634-985 E/HuaweiProcessing: ProcessingLib_Create: the algo have already been created.
634-985 W/EffectsFactory: EffectCreate() library huawei_processing: could not create fx Huawei Audio Preprocessing Effect, error -22
634-985 E/EffectFactoryHAL: Error creating effect e707d040-6b79-11e2-b16a-0002a5d5c51b: Invalid argument
721-8060 E/AudioEffect: set(): AudioFlinger could not create effect, status: -19
721-8060 W/AudioPolicyEffects: addInputEffects(): failed to create Fx huawei_pre_processing on source 1
634-8049 E/baidu_asr_interface: asr_baidu_set_parameters_data-not baidu asr
634-7993 W/DeviceHAL: Device 0x78c2d00000 get_mic_mute: Function not implemented
634-987 W/DeviceHAL: Device 0x78c2dc4680 get_mic_mute: Function not implemented
721-8009 W/HuaweiAudioFlinger: soundtrigger is now disable or not support, pls enable it first from setting
721-8009 W/APM_AudioPolicyManager: startInput(78) failed: other input 70 already started
721-8009 E/AudioFlinger: RecordThread::start error,setCallingAppName -1
4945-4945 E/AudioRecord: start() status -38
4945-4945 E/MLASR_A: getVendorCountry=ca
500-8480 W/libc: Set property "hw.wifi.dns_stat" to "99,14,14044,1,34759"
1472-1472 W/HwKeyguardDragHelper: AnimationBlocked
4945-5079 W/libEGL: EGLNativeWindowType 0x79e0317010 disconnect failed
1140-2290 E/WindowManager: win=Window{d80c651 u0 ProjectActivity} destroySurfaces: appStopped=true win.mWindowRemovalAllowed=false win.mRemoveOnExit=false
767-767 E/wificond: Failed to get NL80211_RATE_INFO_NOISE
767-767 E/wificond: Failed to get NL80211_RATE_INFO_SNR
767-767 E/wificond: Failed to get NL80211_STA_INFO_CNAHLOAD
1140-1316 E/WificondControl: Noise: 0, Snr: -1, Chload: -1
767-767 E/wificond: Failed to get NL80211_RATE_INFO_SNR
767-767 E/wificond: Failed to get NL80211_STA_INFO_CNAHLOAD
767-767 E/wificond: Failed to get NL80211_RATE_INFO_NOISE
767-767 E/wificond: Failed to get NL80211_RATE_INFO_SNR
767-767 E/wificond: Failed to get NL80211_STA_INFO_CNAHLOAD
1140-1316 E/WificondControl: Noise: 0, Snr: -1, Chload: -1
761-8466 W/ACodec: forcing OMX state to Idle when received shutdown in ExecutingState
769-8467 W/SimpleSoftOMXComponent: onChangeState mState= 3, mTargetState = 3, state = 2
769-8467 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions port buf count is not zero
769-1826 W/SimpleSoftOMXComponent: checkTransitions mState = 2, mTargetState = 1
721-8060 W/HuaweiAudioFlinger: soundtrigger is now disable or not support, pls enable it first from setting
1900-3437 E/HSM: BMNCaller:is not PermissionEnabled.
721-6695 W/AudioFlinger::EffectModule: EffectModule 0x7ba4f22a00 destructor called with unreleased interface
634-941 E/audio_hw_primary: in_remove_audio_effect error effect is null
634-941 W/StreamHAL: Error from HAL stream in function remove_audio_effect: Function not implemented
721-6695 E/AudioFlinger::EffectModule: Error when removing effect: -38
721-6695 W/AudioFlinger::EffectHandle: disconnect Effect handle 0x7ba4e45800 disconnected after thread destruction
1640-1796 W/AudioState: session release and not found sessionId: 81
4945-4945 W/HmsSpeechRecognitionHolder#c1cafe: destroy on Thread[main,5,main]
4945-8481 E/HwCustAudioRecordImpl: isOpenEC : false
4945-8481 E/HwCustAudioRecordImpl: isOpenEC : false
Things I found suspicious in the logs
634-985 E/HuaweiProcessing: ProcessingLib_Create: the algo have already been created.
634-985 W/EffectsFactory: EffectCreate() library huawei_processing: could not create fx Huawei Audio Preprocessing Effect, error -22
634-985 E/EffectFactoryHAL: Error creating effect e707d040-6b79-11e2-b16a-0002a5d5c51b: Invalid argument
721-8060 E/AudioEffect: set(): AudioFlinger could not create effect, status: -19
721-8060 W/AudioPolicyEffects: addInputEffects(): failed to create Fx huawei_pre_processing on source 1
721-8009 W/APM_AudioPolicyManager: startInput(78) failed: other input 70 already started
721-8009 E/AudioFlinger: RecordThread::start error,setCallingAppName -1
4945-4945 E/AudioRecord: start() status -38
Note: the HMS demo apps I've tried works correctly on my Mediapad T5.
Update: After some fixes pointed out by #shirley, ASR seems to be working reliably on a P30Lite. But still facing the same issue on an older Mediapad T5.
According to the logs you provided, the voice of the user is not detected.
The meanings of logs and status codes are as follows:
solution
It is recommended that you add logs to the callback method of the MLAsrListener listener to view the speech recognition process.
You are advised to check mSpeechRecognizer.destroy(). Check whether this method is invoked prematurely and has ended before it starts.
Check whether the device is faulty or whether the microphone of the device is invalid. Replace the device and perform the test.
After reviewing your logs, the following errors were found:
The reason for this error is:The Languagecode for speech recognition exceeds 10.
You can view the code here:
Ensure that the speech recognition Languagecode does not exceed 10.
11203 ,subError code: 3002,errorMessage: Service unavailable
The cause of this error is that the app_id information is not found in the project.
You are advised to check whether the agconnect-services.json file exists in the project, as shown in the following
If the file does not exist, you need to add it to the project. If the file exists, ensure that app_id is correct.
For details, see the following Docs.
Check whether Automatic Speech Recognition fails to be enabled.
If Automatic Speech Recognition fails to be enabled, you can obtain the cause by using the onError(int error, String errorMessage) method of the MLAsrListener class, as shown in the following figure.
You can add the above method to the listener's class:
2.If speech recognition is enabled successfully but the speech recognition result is not obtained:
The MLAsrConstants.FEATURE parameter is set to FUNCTION_ALLINONE. Therefore, you need to obtain the speech recognition result in the onResults(Bundle results) method, as shown in the following figure.
Some models of phones and tablets could have resource limitation issues when using the ML ASR. The symptom of the problem is that the phone/tablet does not respond after the microphone button is clicked, or an error message is displayed indicating that the speech recognition service is not installed on the device. Not only HMS ASR, I tried using native Android SpeechRecognizer to implement speech recognition and the sample app hangs after click on the button on a limited hardware resource phone simulator.
To fix your issue, I would suggest to switch using the HMS ML Kit ASR to using HMS ML Kit Real-Time Transcription (RTT). RTT provides similar features as ASR for speech recognition and convert the speech to text. For more details, see the HMS ML Kit RTT document at ML Kit-Real-Time Transcription (huawei.com).
The code for RTT is similar to ASR, you need to provide a SpeechRecognitionListener class or anonymous class to implements MLSpeechRealTimeTranscriptionListener. And there is sample code at the document link too. I tested the sample code on my Huawei phone Mate 30 Pro and it is working just fine.
I have a MusicService for MediaPlayback, wich uses a MediaPlayer with the settings:
player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
and those Listeners are set:
OnPreparedListener, OnCompletionListener, OnErrorListener, OnSeekCompleteListener
The MediaPlayer is used for mp3-Playback. When one Song is finished, onCompletion is called. Then PlayNext is called, wich resets the MediaPlayer, then sets the Datasource with the URI of the next Track. Uri is loaded with:
Uri trackUri = ContentUris
.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songId);
Then the Player is prepared, then the player is started. This is working fine, but only sometimes, when the Device is locked and played about 1-3 Songs while it was locked and the next Song should start, the Player doesn't prepare until i hit the power button. Figured out, that if I don't hit the PowerButton it takes the Player about 2 Minutes to prepare.
I've Logged everything now and made a few custom outputs with Log.e(...).
This is put out before prepare() (prepareAsync() delivers the same result) is called:
E/MusicService: Preparing now...
This is put out, when onPrepared is called:
E/MusicService: Player prepared.
So this is the full Device-Output after "Preparing now..." is put out:
04-02 13:54:55.506 12517-12517/at.htlleonding.musync E/MusicService: Preparing now.
04-02 13:54:55.525 811-888/? E/libsuspend: Error writing to /sys/power/state: Device or resource busy
04-02 13:54:55.544 246-14756/? D/offload_visualizer: thread exit
04-02 13:54:55.546 246-14754/? V/offload_effect_bundle: offload_effects_bundle_hal_stop_output output 1879 pcm_id 9
04-02 13:54:55.546 246-14754/? D/hardware_info: hw_info_append_hw_type : device_name = speaker
04-02 13:54:55.549 246-14752/? E/audio_hw_primary: offload_thread_loop: Compress handle is NULL
04-02 13:54:55.549 246-924/? D/audio_hw_primary: adev_close_output_stream: enter:stream_handle(0xb5bfa640)
04-02 13:54:55.549 246-924/? D/audio_hw_primary: out_standby: enter: stream (0xb5bfa640) usecase(3: compress-offload-playback)
04-02 13:54:55.555 246-924/? W/AudioFlinger: moveEffectChain_l() effect chain for session 0 not on source thread 0xb59fa000
04-02 13:54:55.611 246-15030/? I/FFmpegExtractor: android-source:0xb1834060
04-02 13:54:55.820 811-888/? E/libsuspend: Error writing to /sys/power/state: Device or resource busy
04-02 13:54:55.972 246-15030/? I/FFMPEG: [mp3 # 0xae2f4400] Skipping 0 bytes of junk at 2177007.
... Then theres no output until i hit the PowerButton. Then the Song is prepared.
If someone is interested in the full output after I hit the PowerButton until "Player prepared" is called, I created a Gist here.
Sidenote:
While the App is used, a few Album-Covers are displayed in some Fragments. They are loaded with Picasso, so I don't need to worry about memory caching. Some ImageViews are filled without Picasso (for example the ImageViews that hold the drawables of my PlayerControls). Maybe there are Problems with the memory/resources?
I might have found the answer in another thread where some faced the same problem while streaming music here.
My final solution is to use a WakeLock, wich I require before preparing a Song and release again onPrepared and onError and onDestroy of my Service. It's important to release it again to save Battery. Make sure to check if the WakeLock is held before releasing it.
I create my WakeLock like this in onCreate of my Service:
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MusicService");
aquire:
wakeLock.acquire();
release:
if(wakeLock.isHeld())
wakeLock.release();
Tested the Playback now for about 10 Minutes and didn't stop until now. I don't know if this is a very battery-saving solution
I have a problem with video not been playing. I am using SurfaceView with MediaPlayer.
Those are my actions:
MediaPlayer.setDataSource(url)
MediaPlayer.prepareAsync();
MediaPlayer.start() // From setOnPreparedListener listener
MediaPlayer.reset()
Go back to 1 with different url
About 3 out of 5 videos do not start playing. I see nothing in the logs except some times when video does start it show me this:
W/MediaPlayer﹕ info/warning (1, 903)
Can't find 903 in the documentations.
I want to play mp3 radio stream using Android MediaPlayer via http protocol. From time to time [1 playing of 5 - that is actually quite often] I receive PVMFFailure like this:
02-23 02:05:23.503: ERROR/PlayerDriver(91): Command PLAYER_INIT completed with an error or info PVMFFailure
02-23 02:05:23.503: ERROR/MediaPlayer(2111): error (1, -1)
02-23 02:05:23.533: ERROR/MediaPlayer(2111): stop called in state 0
02-23 02:05:23.533: ERROR/MediaPlayer(2111): error (-38, 0)
There is no solution here or on google, so please if you do know how to debug this, please help :)
Here is my code for player:
//start
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(address);
player.prepare();
player.start();
//stop
player.stop();
player.reset();
I`m using Android 2.2 API. Thanks!
You should use player.prepareAsync(); instead player.prepare();.
Some users of our app are reporting that ringtones fail to load as expected. I've included a typical logcat output from one of these users below. It seems that the request for the default ringtone is returning null ("Failed to open ringtone content://settings/system/ringtone"). I've asked these users to change ringtones, and sometimes this resolves the issue, but in other cases (HTC Hero in both the ones I know of) changing the ringtone does not fix the problem. I have been unable to reproduce this problem on any of our test devices. Any thoughts as to what might cause this, how to debug, or how to resolve?
D/Ringtone( 743): create mAudio
D/MediaPlayer( 743): setDataSource(44, 0, 576460752303423487) pid = 743
E/Mp3HwPlayer( 56): ID:24, new Mp3HwPlayer
E/MediaPlayerService( 56): getPlayerType(): Has ID3 info, size = 1024
E/MediaPlayerService( 56): getPlayerType(): Input buffer is MPA format
E/Mp3HwPlayer( 56): ID:24, destory Mp3HwPlayer
D/MediaPlayerService( 56): Create Player playertype=6
D/MediaPlayerService( 56): new PVPlayer for MP3
D/PVPlayer( 56): setDataSource(25, 0, 180143)
D/MediaPlayerService( 56): MediaPlayerService::Client::setDataSource exit
D/MediaPlayerService( 56): returned from setDataSource() in MediaPlayerService::create
D/MediaPlayerService( 56): [23] prepareAsync
I/PlayerDriver( 56): OpenCore disable firewall payload
W/MediaPlayer( 743): info/warning (1, 26)
E/PlayerDriver( 56): Command PLAYER_PREPARE completed with an error or info PVMFErrResource
E/MediaPlayer( 743): error (1, -17)
E/RingtoneManager( 743): Failed to open ringtone content://settings/system/ringtone
E/RingtoneManager( 743): unable to find a usable ringtone
E/IncomingRinger( 743): Couldn't find a ringtone for URI: content://settings/system/ringtone
It looks like the ringtone is stored on a SD card, and you haven't permission to read SD data, so it returns null. I got similar problem with my mediaPlayer.