You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

定制平板MediaPlayer prepare()调用挂起问题求助

MediaPlayer.prepare() Hangs Indefinitely on Custom Android Tablet (Works on Samsung Devices)

I’ve hit a frustrating compatibility issue with audio streaming on a custom Android tablet (non-Samsung/Google branded). The code works perfectly on Samsung devices, but on this custom hardware, MediaPlayer.prepare() hangs indefinitely—no errors, crashes, or additional log output, the app stays fully responsive, but onPrepared() never gets triggered.

Context & Setup

  • The streaming feature is built into a Service subclass, running on a dedicated background thread.
  • I’ve implemented all critical listeners: OnPreparedListener, OnErrorListener, OnBufferingUpdateListener, OnCompletionListener, with verbose logging for debugging.
  • I’ve tested both synchronous prepare() and asynchronous prepareAsync() methods, with identical hanging behavior.
  • The device uses 4G connectivity, all other web API calls function normally, and local audio playback via VideoView works without issues.
  • Letting the device sit for 30+ minutes doesn’t resolve the hang—no crashes, errors, or log updates, and the app remains interactive.

Relevant Code

MediaPlayer Configuration

private void configureMediaPlayer(String streamUrl) {
    mMediaPlayer = new MediaPlayer();
    AudioAttributes attributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .build();
    mMediaPlayer.setAudioAttributes(attributes);
    Log.d(TAG, "Media player configured");
    playTuneInStation(streamUrl);
}

Stream Playback Logic

private void playStation(String streamUrl) {
    Log.d(TAG, "playStation: " + streamUrl);
    if (mMediaPlayer.isPlaying()) {
        mMediaPlayer.stop();
        mMediaPlayer.reset();
        Log.d(TAG, "Stopped media player.");
    }
    try {
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setDataSource(streamUrl);
        new Handler(getMainLooper()).post(() -> Log.d(TAG, "Main thread: " + Thread.currentThread().getId()));
        Log.d(TAG, "Preparing stream on thread: " + Thread.currentThread().getId() + ".");
        mMediaPlayer.prepare();
        Log.d(TAG, "Waiting for media player to be prepared");
    } catch (IOException e) {
        //TODO: Gracefully inform user of failure
        e.printStackTrace();
    }
}

OnPrepared Implementation

@Override
public void onPrepared(MediaPlayer mp) {
    Log.d(TAG, "Media player prepared. Starting playback.");
    mp.start();
}

Updates

  • Update 1: When loading a local audio file (replacing setDataSource(streamUrl) with setDataSource(this, Uri.parse(filename))), playback works flawlessly.
  • Update 2: Passing an invalid URL triggers OnErrorListener as expected (error: unable to load resource). Passing a truncated valid URL logs repeated errors, retries 10 times, then fails in OnErrorListener.

Debug Logs (When Calling prepare())

05-15 11:02:33.149 252-26756/? D/FslExtractor: FslExtractor::FslExtractor mime=audio/mpeg
05-15 11:02:33.150 252-26756/? D/FslExtractor: FslExtractor::Init BEGIN GetLibraryName lib_mp3_parser_arm11_elinux.3.0.so load parser name lib_mp3_parser_arm11_elinux.3.0.so FslExtractor::CreateParserInterface success
05-15 11:02:33.150 252-26756/? I/FslExtractor: Core parser MP3PARSER_03.01.15 build on Nov 17 2016 13:55:34
05-15 11:02:33.150 252-26756/? D/FslExtractor: createParser2 flag=5e,err=0
05-15 11:02:33.150 252-26756/? I/FslExtractor: mReadMode=0,mNumTracks=1 bSeekable 1
05-15 11:02:33.150 252-26756/? D/FslExtractor: FslExtractor::ParseMediaFormat BEGIN ParseAudio index=0,type=5,subtype=2829696637

The system starts parsing the audio via the custom FslExtractor (manufacturer-specific media parser), but then stalls completely with no further progress.

Based on the logs and behavior, here are the most actionable solutions:

  • Switch to ExoPlayer: The core issue is almost certainly the custom device’s FslExtractor lacking support for your stream’s format/encoding. ExoPlayer uses its own independent set of media extractors that don’t rely on the system’s implementation, which will likely bypass this compatibility gap. It’s also more robust for streaming use cases overall.
  • Analyze the Stream’s Format: Use a tool like ffmpeg to inspect your audio stream’s encoding, bitrate, and container format. The FslExtractor may have limited support for certain MP3 variants (like VBR with non-standard headers, or streams using unusual chunking). Compare this to a stream that works on the custom device (if available) to identify key differences.
  • Capture Network Traffic: Use Wireshark to record the HTTP response from the streaming server on both the custom tablet and a Samsung device. Check for differences in HTTP headers (like Transfer-Encoding, Content-Type) or initial stream data that might confuse the custom extractor.
  • Test with a Custom MediaDataSource: Implement a custom MediaDataSource to manually fetch and feed stream data to MediaPlayer. This can help isolate whether the issue stems from the system’s network handling or the extractor itself.

内容的提问来源于stack exchange,提问作者James B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:51:30