19

注意:当我发布这个问题时,我不在我工作的公司工作,因此,即使可能会有一些很好的答案,我实际上也不会测试它们,因为我没有理由 (除了促进社区;这可能会让我有一天这样做)

但是,如果该问题的某些答案被许多其他社区成员推广,我可能会选择它作为我最初发布该问题多年后遇到的问题的正确答案。

同时,我希望一些答案可以帮助你们中的一些人也遇到这个问题。是的,堆栈溢出!


我们的应用程序必须从在线来源流式传输音乐(我无权透露该来源)。

为什么在 S3 上开始流式传输需要超过 2 分钟?

我已经能够弄清楚 Media Player 对象由于MEDIA_ERROR_UNKOWN而进入错误状态- 太好了。对我帮助不大。因此,在使用 OnErrorListener 处理 onError 时,我重置了媒体播放器对象,然后调用 startPlaying 来完成其余的工作 - 设置数据源等。

成员:

private ProgressBar playSeekBar;
private ImageView ivPlay;
private ImageView ivPause;
private ImageView ivBuffer;
private MediaPlayer mPlayer;
private ImageView ivInfo;
private AudioManager audio;

初始化媒体播放器(和可视化器 - 这不是本问题的问题)

private void initialMediaPlayerAndVisualizer() {
      Log.d(TAG, "Initial Media Player and Visualizer");
    
      playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
      playSeekBar.setMax(100);
      playSeekBar.setVisibility(View.GONE);
                          
      mPlayer = new MediaPlayer();   
      
      Log.d(TAG, "Create onErrorListener");
      MediaPlayer.OnErrorListener errorListener = new MediaPlayer.OnErrorListener() {
        
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d(TAG, "OnError - Error code: "+what+" Extra code: "+extra);
            
            switch(what){
            case -1004:
                Log.d("Streaming Media", "MEDIA_ERROR_IO");
                break;
            case -1007:
                Log.d("Streaming Media", "MEDIA_ERROR_MALFORMED");
                break;
            case 200:
                Log.d("Streaming Media", "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK");
                break;
            case 100:
                Log.d("Streaming Media", "MEDIA_ERROR_SERVER_DIED");
                break;
            case -110:
                Log.d("Streaming Media", "MEDIA_ERROR_TIMED_OUT");
                break;
            case 1:
                Log.d("Streaming Media", "MEDIA_ERROR_UNKNOWN");
                break;
            case -1010:
                Log.d("Streaming Media", "MEDIA_ERROR_UNSUPPORTED");
                break;
            
            }

            switch(extra){
            case 800:
                Log.d("Streaming Media", "MEDIA_INFO_BAD_INTERLEAVING");
                break;
            case 702:
                Log.d("Streaming Media", "MEDIA_INFO_BUFFERING_END");
                break;
            case 701:
                Log.d("Streaming Media", "MEDIA_INFO_METADATA_UPDATE");
                break;
            case 802:
                Log.d("Streaming Media", "MEDIA_INFO_METADATA_UPDATE");
                break;
            case 801:
                Log.d("Streaming Media", "MEDIA_INFO_NOT_SEEKABLE");
                break;
            case 1:
                Log.d("Streaming Media", "MEDIA_INFO_UNKNOWN");
                break;
            case 3:
                Log.d("Streaming Media", "MEDIA_INFO_VIDEO_RENDERING_START");
                break;
            case 700 :
                Log.d("Streaming Media", "MEDIA_INFO_VIDEO_TRACK_LAGGING");
                break;
            
            }
            
            Log.d("Streaming Media", "Reset media player");
            mPlayer.reset();
            // We need to link the visualizer view to the media player so that it displays something
              mVisualizerManager = new VisualizerManager(context); //(VisualizerView) findViewById(R.id.visualizerView);
              
              //Send the visualizerContainer to the Renderer
              visualizerRenderer = new VisualizerRenderer(arrayVisualizer);
              mVisualizerManager.addRenderer(visualizerRenderer);
              
              try {
                startPlaying();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return true;
        }
    };
    
    Log.d(TAG, "Set error listener on Media Player object");          
     mPlayer.setOnErrorListener(errorListener);
     
      // We need to link the visualizer view to the media player so that it displays something
      mVisualizerManager = new VisualizerManager(context); //(VisualizerView) findViewById(R.id.visualizerView);
      
      //Send the visualizerContainer to the Renderer
      visualizerRenderer = new VisualizerRenderer(arrayVisualizer);
      mVisualizerManager.addRenderer(visualizerRenderer);
      
      try {
        startPlaying();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

开始播放:

private void startPlaying () throws IllegalStateException, IOException, UnsupportedOperationException
  {
    
    if(mPlayer != null){
        
          ivPause.setVisibility(View.INVISIBLE);
          ivPlay.setVisibility(View.INVISIBLE);
          ivBuffer.setVisibility(View.VISIBLE);
        
        mVisualizerManager.link(mPlayer);
        
        mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
        
        try {
            mPlayer.setDataSource(theSource);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "Illegal Argument Exception: "+e);
            e.printStackTrace();
        } catch (IllegalStateException e) {
            Log.d(TAG, "Illegal State Exception: "+e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "IO Exception: "+e);
            e.printStackTrace();
        }
      
      mPlayer.prepareAsync();

      mPlayer.setOnPreparedListener(new OnPreparedListener() {
          public void onPrepared(MediaPlayer mp) {
              ivBuffer.setVisibility(View.INVISIBLE);
              
              bars.setVisibility(View.INVISIBLE);
              visualizerContainer.setVisibility(View.VISIBLE);
              ivInfo.setImageResource(R.drawable.img_radio_info_online);
              ivPause.setVisibility(View.VISIBLE);
              ivPlay.setVisibility(View.INVISIBLE);
              
              //Mute the video if the phone is muted.
              if ((audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT) ||  (audio.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE)) {
                  mp.setVolume(0, 0);
                  
                  Toast toast = Toast.makeText(context, R.string.device_muted, Toast.LENGTH_LONG);
                  toast.show();
              }
                
              mPlayer.start();
          }
      });
      
    }else{
        Log.d(TAG, "Media player is null.");
        initialMediaPlayerAndVisualizer();
    }
    
  
  
}

别玩了:

private void stopPlaying()
      {
      bars.setVisibility(View.VISIBLE);
      visualizerContainer.setVisibility(View.INVISIBLE);

      ivInfo.setImageResource(R.drawable.img_radio_info_offline);
      
      ivPlay.setVisibility(View.VISIBLE);
      ivPause.setVisibility(View.INVISIBLE);
      
      mPlayer.stop();
      
      mVisualizerManager.release();
      mPlayer.release();
      
      mPlayer = null;
}

一点日志:

05-21 16:26:23.600: I/Buffering(3921): 0

05-21 16:26:23.600: I/MediaPlayer(3921): Info (703,156)
05-21 16:26:23.600: I/MediaPlayer(3921): Info (701,0)
05-21 16:26:23.610: D/VisualizerRenderer(3921): Render columns
05-21 16:26:23.715: V/MediaPlayer(3921): message received msg=100, ext1=1, ext2=-110
05-21 16:26:23.715: E/MediaPlayer(3921): error (1, -110)
05-21 16:26:23.715: V/MediaPlayer(3921): callback application
05-21 16:26:23.715: V/MediaPlayer(3921): back from callback
05-21 16:26:23.725: D/VisualizerRenderer(3921): Render columns
05-21 16:26:23.725: E/MediaPlayer(3921): Error (1,-110)
05-21 16:26:23.725: D/MAIN(3921): OnError - Error code: 1 Extra code: -110
05-21 16:26:23.725: D/Streaming Media(3921): MEDIA_ERROR_UNKNOWN
05-21 16:26:23.725: D/Streaming Media(3921): Reset media player
05-21 16:26:23.725: V/MediaPlayer-JNI(3921): reset
05-21 16:26:23.725: V/MediaPlayer(3921): reset
05-21 16:26:23.730: D/VisualizerRenderer(3921): Divisions: 9
05-21 16:26:23.730: D/VisualizerManager(3921): Added te renderer
05-21 16:26:23.730: V/MediaPlayer-JNI(3921): get_session_id()
05-21 16:26:23.735: D/VisualizerManager(3921): Media player and visualizer linked
05-21 16:26:23.735: D/VisualizerManager(3921): Set capture listener
05-21 16:26:23.735: D/VisualizerManager(3921): Set on visualizer complete listener
05-21 16:26:23.740: V/MediaPlayer(3921): setVideoSurfaceTexture
05-21 16:26:23.740: V/MediaPlayer(3921): prepareAsync
05-21 16:26:25.285: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:25.285: V/MediaPlayer(3921): buffering 0
05-21 16:26:25.285: V/MediaPlayer(3921): callback application
05-21 16:26:25.285: V/MediaPlayer(3921): back from callback
05-21 16:26:25.330: I/Buffering(3921): 0
05-21 16:26:25.390: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:25.390: V/MediaPlayer(3921): buffering 0
05-21 16:26:25.390: V/MediaPlayer(3921): callback application
05-21 16:26:25.390: V/MediaPlayer(3921): back from callback
05-21 16:26:25.425: I/Buffering(3921): 0
05-21 16:26:25.490: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:25.490: V/MediaPlayer(3921): buffering 0
05-21 16:26:25.490: V/MediaPlayer(3921): callback application
05-21 16:26:25.490: V/MediaPlayer(3921): back from callback
...

05-21 16:26:52.585: I/Buffering(3921): 0
05-21 16:26:53.570: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:53.570: V/MediaPlayer(3921): buffering 0
05-21 16:26:53.570: V/MediaPlayer(3921): callback application
05-21 16:26:53.570: V/MediaPlayer(3921): back from callback
05-21 16:26:53.585: I/Buffering(3921): 0
05-21 16:26:54.570: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:54.570: V/MediaPlayer(3921): buffering 0
05-21 16:26:54.570: V/MediaPlayer(3921): callback application
05-21 16:26:54.570: V/MediaPlayer(3921): back from callback
05-21 16:26:54.570: V/MediaPlayer(3921): message received msg=200, ext1=702, ext2=0
05-21 16:26:54.570: W/MediaPlayer(3921): info/warning (702, 0)
05-21 16:26:54.570: V/MediaPlayer(3921): callback application
05-21 16:26:54.570: V/MediaPlayer(3921): back from callback
05-21 16:26:54.590: I/Buffering(3921): 0
05-21 16:26:54.590: I/MediaPlayer(3921): Info (702,0)
05-21 16:26:55.575: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:55.575: V/MediaPlayer(3921): buffering 0
05-21 16:26:55.575: V/MediaPlayer(3921): callback application
05-21 16:26:55.575: V/MediaPlayer(3921): back from callback
05-21 16:26:55.575: I/Buffering(3921): 0
05-21 16:26:56.575: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:56.575: V/MediaPlayer(3921): buffering 0
05-21 16:26:56.575: V/MediaPlayer(3921): callback application
05-21 16:26:56.575: V/MediaPlayer(3921): back from callback
05-21 16:26:56.585: I/Buffering(3921): 0
05-21 16:26:57.575: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:57.575: V/MediaPlayer(3921): buffering 0
05-21 16:26:57.575: V/MediaPlayer(3921): callback application
05-21 16:26:57.575: V/MediaPlayer(3921): back from callback
05-21 16:26:57.600: I/Buffering(3921): 0
05-21 16:26:57.930: V/MediaPlayer-JNI(3921): stop
05-21 16:26:57.930: V/MediaPlayer(3921): stop
05-21 16:26:57.930: D/VisualizerManager(3921): Released the visualizer
05-21 16:26:57.930: V/MediaPlayer-JNI(3921): release
05-21 16:26:57.930: V/MediaPlayer(3921): setListener
05-21 16:26:57.930: V/MediaPlayer(3921): disconnect
05-21 16:26:57.935: V/MediaPlayer(3921): destructor
05-21 16:26:57.935: V/MediaPlayer(3921): disconnect
4

4 回答 4

17

这个问题的答案原来是安装在运行 Android 4.1.2 的三星 S III 设备上的 Android 固件的问题。

这似乎与流的来源有关,因为某些来源最终会在设备上播放,但我们需要的来源从未播放过。

如果您可以从其他来源获取流,它应该可以工作。

因此,如果您正在为特定公司/目的开发应用程序并且对流的源有一定的控制权,或者可以与控制流源的人进行通信,请让他们将流的源更改为某种东西这适用于运行 Android 4.1.2 的三星 S III。

除此之外,所有能解决这个问题的都是固件升级。

于 2013-08-22T10:44:05.940 回答
7

对于每个在这个问题上苦苦挣扎的人,这里的解决方案是:

Android MediaPlayer 需要很长时间来准备和缓冲

编辑:以前的解决方案不是很完整,因为有时可能会在播放器暂停和恢复时听到播放器“口吃”。

一个更优雅的 100% java 答案涉及使用 MediaCodec 实例将 mp3 转换为 PCM 数据以馈送到 AudioTrack 实例。

我在这里发布了完整的源代码和解释:http ://www.piterwilson.com/blog/2014/03/15/mediacodec-mediaextractor-and-audiotrack-to-the-rescue/

于 2014-02-07T09:42:24.557 回答
3

我通过使用库
compile 'com.devbrackets.android:exomedia:3.0.1'解决了这个问题

于 2016-08-19T07:42:36.597 回答
0

一些在旧三星手机上获得 -1004 的明确解决方案:

就我而言,我使用的是三星 Galaxy Pocket,并且在我们的 CDN 上有一个 mp3 文件,它无法播放,即

http://域/路径/音频文件

但是,将文件下载到手机时,它会在本机播放器中播放。不过,不在我的应用程序中,也不在浏览器中。 事实证明,媒体播放器服务需要一个文件扩展名来提示媒体类型,并且无法根据文件头自动检测。

我通过给文件位置一个实际的扩展名来解决它。

http://domain/path/audiofile.mp3

解决方案

  1. 给文件一个扩展名(如上)或
  2. 以编程方式手动下载文件,在 sdcard 上为其提供正确的扩展名,然后从 sdcard 作为源播放。
于 2017-02-14T11:22:05.490 回答