简体   繁体   中英

Error streaming file on Android 2.x, working on 4.x

I wrote a class that extends Android mediaplayer, the class is the following

package it.ccevas.ccevasandroid;

import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;

public class MyMediaPlayer extends MediaPlayer {

public MyMediaPlayer(){
    super();

    setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            start();
        }
    });

    setOnErrorListener(new OnErrorListener() {


        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
             if (what == -38){
                 return true;
             }
             return false;
        }
    });
}

public boolean changeSource (String urlfile){
    try{
        reset();
        setAudioStreamType(AudioManager.STREAM_MUSIC);
        setDataSource(urlfile);
        prepare();      
        seekTo(0);
     }  catch (IllegalArgumentException e1) {
        e1.printStackTrace();
     } catch (SecurityException e1) {
        e1.printStackTrace();
     } catch (IllegalStateException e1) {
        e1.printStackTrace();
     } catch (IOException e1) {
        e1.printStackTrace();
    }
    start();
    return true;        
}

}

It works fine on Android 4.x

But on Android 2.x it works only with files stored on SD and is not working with remote files eg " http://www.something.com/myfile.mp3 "

Permission on manifest are right:

<uses-permission android:name="android.permission.INTERNET"/>

This is the log.cat :

05-03 10:15:06.699: E/MediaPlayer(414): Attempt to call getDuration without a valid mediaplayer
05-03 10:15:06.699: E/MediaPlayer(414): error (-38, 0)
05-03 10:15:06.699: E/MediaPlayer(414): error (1, -2147483648)
05-03 10:15:06.789: E/MediaPlayer(414): Error (-38,0)
05-03 10:15:57.991: E/MediaPlayer(414): Error (1,-2147483648)

您尝试使用新的网址,而不是“ http://www.something.com/myfile.mp3 ”,因为该网址不是从服务器准备的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM