Android Question Mediaplayerstream initalize so slow ?

mesutaslan

Member
Licensed User
Longtime User
Hi there,
I am using mediaplayerstream for my android radio apps. Its working fine but ,
device waiting so long for load stream media

here is the codes :

B4X:
Sub Process_Globals
    Dim mp As MediaPlayerStream
    Dim vol As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        mp.Initialize("mp")
    End If
    mp.Load("http://radyo.normworks.com:8005/")
    vol = 0.5
End Sub

Sub Activity_Resume
    mp.play
End Sub
 

mesutaslan

Member
Licensed User
Longtime User
Android Platform : Platform-13

Device is Motorola XOOM
cpu : 1 ghz dual core
connection : wifi (32 mbit)
loading time : 45-60 sec.

Device is Xpreria ARC Chinese version
cpu : 500 mhz one core
connection : wifi (4 mbit)
loading time : 50-120 sec.

Device is Emulator (PC windows 7 64 bit)
cpu : arm (PC CPU is i5 2.4 ghz)
ram : 512 (PC RAM is 4gb)
connection : wifi (4 mbit)
loading time : 5-7 sec

I guess its not about depends on my connection and how the device buffers the data.

on same devices same radio station with same connection but written in java (eclipse) version loads in 1-3 sec.

any idea ?
 
Upvote 0

mesutaslan

Member
Licensed User
Longtime User
Here is the player part of code. Code has more other sections but the player loading and play part is this.

B4X:
private MediaPlayer player;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initalize();
}
private void initialize(){
    player = new MediaPlayer();
    try {
        player.reset();
        player.setDataSource("http://radyo.normworks.com:8005/");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            Log.i("Buffering", "" + percent);
        }
    });
    player.prepareAsync();
    player.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            player.start();
        }
    });
}


may be its about this method :

B4X:
player.prepareAsync();
 
Upvote 0

mesutaslan

Member
Licensed User
Longtime User
You should start playing in StreamReady event (and also move the Load call to inside the If FirstTime block).

okey i already made with that way. i dont copy codes from app to this page, i wrote them again on page (for practising :p)


i made some experiments today. first of all i am sure that b4a is slower on load with mediaplayer stream then native java app.
(there is a interesting point : in emulator both of them have nearly same time about 3 sec)


with same station
eclipse : 4-10 sec
b4a 10-20 sec

there is a radio player app in market and it loads same station with same url but in 2 sec. (i wonder how it can do it ?)


can we decrease the loading time of mediaplayerstream with some tweaks.
 
Upvote 0

mesutaslan

Member
Licensed User
Longtime User
Here is both eclipse and b4a testing codes. I try to make them equal conditions :

b4a :

B4X:
Sub Process_Globals

    Dim mp As MediaPlayerStream
    Dim starttime As Long
    Dim endtime As Long
    Dim fark As Long
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    starttime = DateTime.Now
    If FirstTime Then
        mp.Initialize("mp")
        mp.Load("http://radyo.normworks.com:8005/")
    End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        mp.Release
    End If
End Sub

Sub mp_StreamReady
    endtime = DateTime.Now
    mp.Play
    fark = endtime - starttime
    Dim m As String = "B4A" & CRLF & "Start : " & starttime & CRLF & "End : " & endtime & CRLF & "Fark : " & fark
    ToastMessageShow (m,True)
End Sub

Sub mp_StreamError (ErrorCode As String, ExtraData As Int)
    ToastMessageShow(ErrorCode & CRLF & ExtraData,True)
End Sub

eclipse:

B4X:
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;




public class MainActivity extends Activity {

    private MediaPlayer player;
    private long starttime;
    private long endtime;
    private long fark;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        initalize();
    }

    private void initalize(){
        starttime = System.currentTimeMillis();
        player = new MediaPlayer();
        try{
            player.reset();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource("http://radyo.normworks.com:8005/");
        }catch(Exception e){
            Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
        }
       
        player.prepareAsync();
        player.setOnPreparedListener(new OnPreparedListener(){
            public void onPrepared(MediaPlayer mp){
                endtime = System.currentTimeMillis();
                player.start();               
                fark = endtime - starttime;
                String m = "Eclipse\nStart : " + starttime + "\nEnd : " + endtime + "\nFark : " + fark ;
                Toast.makeText(getBaseContext(),m,Toast.LENGTH_LONG).show();
            }
        });
    }
   
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
        super.onDestroy();
    }

}
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic4android doesn't load the media. It just calls the same Java methods that your code calls.

i dont copy codes from app to this page, i wrote them again on page (for practising :p)
This makes it more difficult to help as we cannot guess your real code.


I've tried this code and it takes 5.5 seconds to start playing:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  starttime = DateTime.Now
  If FirstTime Then
  mp.Initialize("mp")
  mp.Load("http://radyo.normworks.com:8005/")
  End If
End Sub

Sub mp_StreamReady
  mp.Play
   Log(DateTime.Now - starttime)
End Sub
 
Upvote 0
Top