媒体播放器在onPrepare上失败

时间:2016-09-09 05:45:04

标签: android firebase android-mediaplayer media

我查找了有关错误Prepare failed.: status=0x1的所有相关问题。我使用的代码是:

MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(ChatActivity.this, Uri.parse(url));
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.setVolume(1, 1);
                    mediaPlayer.prepare();
                    mediaPlayer.start();

该网址是Firebase中音频的实际链接。有趣的是,我在一个全新的Android Proyect中编写了这个完全相同的代码,只有一个触发此代码的按钮,它完美运行,但在之前的应用程序中它并没有。我在两个proyects中都有清单中的Internet权限。你们知道发生了什么吗? 这里是对媒体播放器不起作用的proyect的登录:

09-08 23:36:17.535 3265-3369/techluthiers.meetstream E/MediaHTTPConnectionEx: MediaHTTPConnectionEx contructor
09-08 23:36:17.536 3265-3281/techluthiers.meetstream E/MediaHTTPConnectionEx: connect
09-08 23:36:17.537 3265-3281/techluthiers.meetstream E/MediaHTTPConnectionEx: disconnecting
09-08 23:36:17.537 3265-3281/techluthiers.meetstream E/MediaHTTPConnectionEx: disconnected
09-08 23:36:17.545 3265-3514/techluthiers.meetstream E/MediaHTTPConnectionEx: getMIMEType
09-08 23:36:17.545 3265-3514/techluthiers.meetstream E/MediaHTTPConnectionEx: [seekToEx] offset:0/mCurrentOffset:-1
09-08 23:36:17.547 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: proxy null port 0
09-08 23:36:17.548 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [seekToEx] HTTP header =>User-Agent: Player/LG Player 1.0 for Android 5.0.1 (stagefright alternative)
09-08 23:36:17.548 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [seekToEx] include Range: bytes= 0-
09-08 23:36:17.548 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [seekToEx] HTTP header =>Range: bytes=0-
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response code] STATUS CODE:200
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] null: [HTTP/1.1 200 OK]
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Accept-Ranges: [bytes]
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Alt-Svc: [quic=":443"; ma=2592000; v="36,35,34,33,32"]
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Cache-Control: [private, max-age=0]
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Content-Length: [0]
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Content-Type: [audio/mp3]
09-08 23:36:17.802 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Date: [Fri, 09 Sep 2016 05:36:18 GMT]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] ETag: ["df9b0a0ba77691dd6a24b39bd0ca9a30"]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Expires: [Fri, 09 Sep 2016 05:36:18 GMT]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Last-Modified: [Thu, 08 Sep 2016 21:18:04 GMT]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] Server: [UploadServer]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] X-Android-Received-Millis: [1473399377793]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] X-Android-Response-Source: [CONDITIONAL_CACHE 304]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] X-Android-Sent-Millis: [1473399377554]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-generation: [1473369484922000]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-hash: [crc32c=MkV9EA==, md5=35sKC6d2kd1qJLOb0MqaMA==]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-meta-firebaseStorageDownloadTokens: [68848b58-1dac-4796-9377-0c40bf8a4018]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-metageneration: [1]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-storage-class: [STANDARD]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-stored-content-encoding: [identity]
09-08 23:36:17.803 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] x-goog-stored-content-length: [62815]
09-08 23:36:17.804 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [response header] X-GUploader-UploadID: [AEnB2Urmyp8htgAAuaqc63aoFuQgTDFzSmKow6zvFADzpJxdpQ0F44sFzsHwV-nkjBvA44a9psp4zqw4IpiKrIhRD_9nf-NfIQ]
09-08 23:36:17.804 3265-3514/techluthiers.meetstream D/MediaHTTPConnectionEx: [ResponseMessage]:OK
09-08 23:36:17.804 3265-3514/techluthiers.meetstream I/MediaHTTPConnectionEx: [200 OK] mTotalSize: 0
09-08 23:36:17.866 3265-3281/techluthiers.meetstream E/MediaHTTPConnectionEx: getUri

正如您在最后三行中看到的那样,我为文件的总大小获取值0,而另一个proyet它有这一行: 09-08 23:32:54.159 1862-1879/com.example.master.mypplication I/MediaHTTPConnectionEx: [200 OK] mTotalSize: 62815

我注意到如果我在用户插入Firebase之前将媒体播放器放在最开始就可以工作,那么当我在用户登录后将相同的逻辑放入onClick按钮时在媒体播放器中工作,但只有一次。难道Firebase会在第一次阻止MediaHTTPConnectionEx吗?这是一种非常奇怪的行为

3 个答案:

答案 0 :(得分:1)

尝试使用ExoPlayer:https://google.github.io/ExoPlayer/guide.html。它为配置HTTP连接提供了更大的灵活性(参见http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer/upstream/UriDataSource.html),这可能很有用。

答案 1 :(得分:0)

如果您有字符串网址,请删除mediaPlayer.setDataSource(ChatActivity.this, Uri.parse("url"));并添加简单的mediaPlayer.setDataSource(ChatActivity.this, Uri.parse(url));

答案 2 :(得分:0)

您应该考虑异步准备MediaPlayer以避免延迟接口。这个流程适合我,

 MediaPlayer mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try{
    mMediaPlayer.setDataSource(audioUrl);
    mMediaPlayer.prepareAsync();
}
catch(Exception e){}

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
//Play the audio
}

});