Streamer Library V1.1

XverhelstX

Well-Known Member
Licensed User
Longtime User
Streamer Library V1.2 New Version

Hello everyone,

My third library is here:

Streamer Library.
With this library, you are able to stream audio files (only audio files supported yet.)

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim newStream As Streamer
   Dim lblDuration As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)

activity.LoadLayout("Layout")
End Sub

Sub btnStart_Click
Dim THREE_GPP As Int

newStream.StartAudioStream("http://www.freewebs.com/thebirdsmedia/faint.mp3")
'http://www.freewebs.com/thebirdsmedia/faint.mp3
End Sub

Sub btnStop_Click
newStream.StopAudioStream()
End Sub

Sub btnIsStreaming_Click
If newStream.IsStreaming = True Then
Msgbox("test", "It's Streaming!")
Else
Msgbox("test", "Aw to bad!")
End If
End Sub

Sub btnPause_click
Dim test As Int
newStream.PauseStream()
lblDuration.text = newStream.AudioDuration
End Sub

I do think there are quite a few bugs that I cannot seem to solve like the IsStreaming feature as it seems it cannot find the mediaplayer.

I will post some code later.

XverhelstX
 

Attachments

  • Streamer.zip
    3.2 KB · Views: 966
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
I tested on Emulator with Android 1.6, there're errors:

B4X:
newStream.StopAudioStream()
=> java.lang.NullPointerException


B4X:
lblDuration.text = newStream.AudioDuration
=> It returns -1

Could you please fix it? Thank you.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
I cannot seem to solve this.
If anyone can, here is the source code:

B4X:
package xtremelyvirtualstudios.xvs.Streamer;



import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.util.Log;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@ShortName("Streamer")
@Permissions(values = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.RECORD_AUDIO"})
@Author("XverhelstX")
@Version(1.1f)

public class Streamer {
   
    MediaRecorder mRecorder = null;
    MediaPlayer mPlayer = null;
    private boolean mIsPrepared;
    

       
    /**
     * Starts the audio stream. Can take a while to load and can lagg.
     * for links:  please make sure it ends with audiotypes: 
     * e.g: http://www.freewebs.com/thebirdsmedia/faint.mp3
     */
   
   public void StartAudioStream(String URL) {
       
      
        try {
          MediaPlayer mPlayer = new MediaPlayer();
          mIsPrepared = false;
         mPlayer.setDataSource(URL);
         mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
         mPlayer.prepare();
         mPlayer.start();
        } catch (Exception e) {
         Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
        }
       }

    /**
     * Stops the current stream.
     * 
     */
   public void StopAudioStream() {
      
        mPlayer.stop();
      }
   
   
   public boolean IsStreaming(){
   
      if (mPlayer != null && mIsPrepared) {
            return mPlayer.isPlaying();
        }
      return false;
        
    }
   
    public void PauseStream() {
        if (mPlayer != null && mIsPrepared) {
            if (mPlayer.isPlaying()) {
               mPlayer.pause();
            }
        }
        
    }
    
    public int getAudioDuration() {
        if (mPlayer != null && mIsPrepared) {
            return mPlayer.getDuration();
        }
        return -1;
    }
    
    public boolean canSeekForward() {
        return true;
      }

      public boolean canSeekBackward() {
        return true;
      }

      public boolean canPause() {
        return true;
      }
            
}
 

agraham

Expert
Licensed User
Longtime User
B4X:
public void StartAudioStream(String URL) {      
  try {
    [COLOR="Red"]MediaPlayer[/COLOR] mPlayer = new MediaPlayer();
    mIsPrepared = false;
    mPlayer.setDataSource(URL);
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mPlayer.prepare();
    mPlayer.start();
  } catch (Exception e) {
    Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
  }
}
You are declaring a local variable named mPlayer that is hiding your global variable of the same name so the global variable is never assigned - hence the null pointer exception when you access it. I am a bit surprised you don't get a compiler warning but I haven't tried it for myself.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
I changed it to this:
B4X:
package xtremelyvirtualstudios.xvs.Streamer;



import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.util.Log;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@ShortName("Streamer")
@Permissions(values = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.RECORD_AUDIO"})
@Author("XverhelstX")
@Version(1.2f)

public class Streamer {
   
    MediaRecorder mRecorder = null;
    MediaPlayer mPlayer = new MediaPlayer();
    private boolean mIsPrepared;
    

       
    /**
     * Starts the audio stream. Can take a while to load and can lagg.
     * for links:  please make sure it ends with audiotypes: 
     * e.g: http://www.freewebs.com/thebirdsmedia/faint.mp3
     */
   
   public void StartAudioStream(String URL) {
       
      
        try {
          mIsPrepared = false;
         mPlayer.setDataSource(URL);
         mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
         mPlayer.prepare();
         mPlayer.start();
        } catch (Exception e) {
         Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
        }
       }

    /**
     * Stops the current stream.
     * 
     */
   public void StopAudioStream() {
      
        mPlayer.stop();
      }
   
   
   public boolean IsStreaming(){
   
      if (mPlayer != null && mIsPrepared) {
            return mPlayer.isPlaying();
        }
      return false;
        
    }
   
    public void PauseStream() {
        if (mPlayer != null && mIsPrepared) {
            if (mPlayer.isPlaying()) {
               mPlayer.pause();
            }
        }
        
    }
    
    public int getAudioDuration() {
        if (mPlayer != null && mIsPrepared) {
            return mPlayer.getDuration();
        }
        return -1;
    }
    
    public boolean canSeekForward() {
        return true;
      }

      public boolean canSeekBackward() {
        return true;
      }

      public boolean canPause() {
        return true;
      }
            
}

stops works now, but pause still declares -1 and isStreaming shows message not streaming.

XverhelstX
 

ZJP

Active Member
Licensed User
Longtime User
Hi,

The error has disappeared. But to resume a stream after a stop, we must restart the application.

JP
 

FMFREAK

Member
Licensed User
Longtime User
Hi,

The error has disappeared. But to resume a stream after a stop, we must restart the application.

JP

Nope I have done this by the following part:

B4X:
newStream.StopAudioStream
Dim newStream As Streamer

Create a new newStream after stopping the first one
 

mcmanu

Active Member
Licensed User
Longtime User
Hi

When you stop the first one and start the second one, then you can not stream the first one again, or is it possible? :)
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Thomas,

Could you kindly tell me when to use the Audio duration of this library. Also you have given an option for can seekforwrd or seekbehind. Kindly let me know how to use this also.

Regards.
SK
 
Top