Coder Social home page Coder Social logo

Comments (10)

SteveEdson avatar SteveEdson commented on July 17, 2024

If I open that URL, I get a 403 forbidden error. I suspect that this isn't working because the credentials you used when you copied the URL don't match the one's on the device.

It looks like Soundcloud require you to be authenticated before you can play the stream. Take a look at the Soundcloud docs https://developers.soundcloud.com/docs/api/guide#authentication.

from ffmpegmediaplayer.

machard avatar machard commented on July 17, 2024

It's why i said "i don't know how long it will last", they eventually expire :)

You can get a soundcloud stream url with this link
http://api.tracktl.com/api/provider/-1/streaming_url/1717385?userID=3&juketoken=2a74c227f90376cb29ca420bf489d696c5eb54d4f2d7427ec0cd61a8101d32e9

thank you

from ffmpegmediaplayer.

SteveEdson avatar SteveEdson commented on July 17, 2024

Are you able to provide an example of your code? Have you also added listeners for error states etc? They might get triggered and provide more details about why the stream isn't playing.

from ffmpegmediaplayer.

machard avatar machard commented on July 17, 2024

yes i'll get back to you shortly with that

from ffmpegmediaplayer.

machard avatar machard commented on July 17, 2024

Hi,

So, here is the code :

package tl.track.application.cordova.streaming;


import java.io.IOException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

import wseemann.media.FFmpegMediaPlayer;
import android.media.AudioManager;
import android.util.Log;


public class Streaming extends CordovaPlugin {
    public static final String TAG = "Streaming";


    private static final Integer STATE_PLAYING = 0;
    private static final Integer STATE_BUFFERING = 4;
    private static final Integer STATE_PAUSED = 1;
    private static final Integer STATE_STOPPED = 3;
    private static final Integer STATE_ERROR = 5;


    private FFmpegMediaPlayer mPlayer = new FFmpegMediaPlayer();
    private FFmpegMediaPlayer mPreloadingPlayer;

    private String mCurrentUrl;
    private String mPreloadingUrl;

    private Boolean isInitialized = false;
    private Boolean isPreloadingInitialized = false;

    /**
     * Constructor.
     */
    public Streaming() {
    }

    /**
     * Sets the context of the Command. This can then be used to do things like
     * get file paths associated with the Activity.
     *
     * @param cordova The context of the main Activity.
     * @param webView The CordovaWebView Cordova is running in.
     */
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        Log.d(TAG, "streaming ok");
    }

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action            The action to execute.
     * @param args              JSONArry of arguments for the plugin.
     * @param callbackContext   The callback id used when calling back into JavaScript.
     * @return                  True if the action was valid, false if not.
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if(action.equals("getDuration")) {
            callbackContext.success(this.getDuration());
        } else if(action.equals("getPosition")) {
            callbackContext.success(this.getPosition());
        } else if(action.equals("play")) {
            this.play();
        } else if(action.equals("pause")) {
            this.pause();
        } else if(action.equals("stop")) {
            this.stop();
        } else if(action.equals("playNewFile")) {
            this.playNewFile(args.getString(0));
        } else if(action.equals("preload")) {
            this.preload(args.getString(0));
        } else {
            return false;
        }
        return true;
    }

    //--------------------------------------------------------------------------
    // LOCAL METHODS
    //--------------------------------------------------------------------------

    private Integer getDuration() {
        if(isInitialized) {
            return mPlayer.getDuration()/1000;
        }
        return 0;
    }
    private Integer getPosition() {
        if(isInitialized) {
            return mPlayer.getCurrentPosition()/1000;
        }
        return 0;
    }

    private void playNewFile(String url) {
        mPlayer.reset();
        isInitialized = false;

        try {
            mCurrentUrl = url;

            if(mPreloadingPlayer != null && mCurrentUrl.equals(mPreloadingUrl)) {
                mPlayer.release();
                mPlayer = mPreloadingPlayer;
                mPreloadingPlayer = null;
                if(isPreloadingInitialized) {
                    isInitialized=true;
                    play();
                }
            } else {
                mPlayer.setDataSource(url);
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mPlayer.setOnPreparedListener(preparedListener);
                mPlayer.prepareAsync();
                sendStatus(STATE_BUFFERING, url);
            }
        } catch (IllegalArgumentException e) {
            sendStatus(STATE_ERROR, url);
        } catch (SecurityException e) {
            sendStatus(STATE_ERROR, url);
        } catch (IllegalStateException e) {
            sendStatus(STATE_ERROR, url);
        } catch (IOException e) {
            sendStatus(STATE_ERROR, url);
        }
    }

    private void preload(String url) {
        if(mPreloadingPlayer != null) {
            mPreloadingPlayer.reset();
        } else {
            mPreloadingPlayer = new FFmpegMediaPlayer();
        }
        isPreloadingInitialized = false;

        try {
            mPreloadingPlayer.setDataSource(url);
            mPreloadingUrl = url;
            mPreloadingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPreloadingPlayer.setOnPreparedListener(preparedListener);
            mPreloadingPlayer.prepareAsync();
        } catch (IllegalArgumentException e) {
            mPreloadingPlayer.release();
            mPreloadingPlayer = null;
        } catch (SecurityException e) {
            mPreloadingPlayer.release();
            mPreloadingPlayer = null;
        } catch (IllegalStateException e) {
            mPreloadingPlayer.release();
            mPreloadingPlayer = null;
        } catch (IOException e) {
            mPreloadingPlayer.release();
            mPreloadingPlayer = null;
        }
    }

    private void play() {
        Log.d(TAG, "streaming play asked");
        if(isInitialized) {
            mPlayer.start();
            sendStatus(STATE_PLAYING, mCurrentUrl);
        }
    }

    private void pause() {
        Log.d(TAG, "streaming pause asked");
        if(isInitialized) {
            mPlayer.pause();
            sendStatus(STATE_PAUSED, mCurrentUrl);
        }
    }

    private void stop() {
        Log.d(TAG, "streaming stop asked");
        if(isInitialized) {
            mPlayer.stop();
            isInitialized = false;
            sendStatus(STATE_STOPPED, mCurrentUrl);
        }
    }

    private void sendStatus(Integer state, String url) {
        webView.sendJavascript("cordova.require('org.apache.cordova.streaming.streaming').onStatus(" + String.valueOf(state) + ", '" + url +"');" );
    }

    FFmpegMediaPlayer.OnPreparedListener preparedListener = new FFmpegMediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(FFmpegMediaPlayer mp) {
            Log.d(TAG, "streaming prep");
            mp.setOnCompletionListener(listener);
            mp.setOnErrorListener(errorListener);
            mp.setOnInfoListener(infoListener);
            if(mp == mPlayer) {
                isInitialized = true;
                play();
            } else if(mp == mPreloadingPlayer) {
                isPreloadingInitialized = true;
            }

        }
    };

    FFmpegMediaPlayer.OnCompletionListener listener = new FFmpegMediaPlayer.OnCompletionListener() {
        public void onCompletion(FFmpegMediaPlayer mp) {
            Log.d(TAG, "streaming over");
            if(mp == mPlayer) {
                stop();
            }
        }
    };

    FFmpegMediaPlayer.OnInfoListener infoListener = new FFmpegMediaPlayer.OnInfoListener() {
        public boolean onInfo(FFmpegMediaPlayer mp, int what, int extra) {
            Log.d(TAG, "streaming info");
            if(mp == mPlayer) {
                switch(what) {
                case FFmpegMediaPlayer.MEDIA_INFO_BUFFERING_START:
                    sendStatus(STATE_BUFFERING, mCurrentUrl);
                    break;
                case FFmpegMediaPlayer.MEDIA_INFO_BUFFERING_END:
                    sendStatus(STATE_PLAYING, mCurrentUrl);
                    break;
                }
            }
            return true;
        }
    };

    FFmpegMediaPlayer.OnErrorListener errorListener = new FFmpegMediaPlayer.OnErrorListener() {
        public boolean onError(FFmpegMediaPlayer mp, int what, int extra) {
            Log.d(TAG, "streaming err");
            if(mp == mPlayer) {
                sendStatus(STATE_ERROR, mCurrentUrl);
            }
            return false;
       }
    };

}

Here is the FmpegMediaPlayer output :

09-23 10:42:41.783: V/FFmpegMediaPlayer-JNI(25685): reset
09-23 10:42:41.784: V/FFmpegMediaPlayer(25685): reset
09-23 10:42:41.785: V/FFmpegMediaPlayer-JNI(25685): setDataSource: path https://soundcloud.hs.llnwd.net/jQhWjKA8beVZ.128.mp3?AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1411462109&Signature=bx4feftwBftwcZgk9uJhiFnOs8I%3D&e=1411462109&h=5c40c03b2add89a72d435ef140bd0548
09-23 10:42:41.786: V/FFmpegMediaPlayer(25685): setDataSource(https://soundcloud.hs.llnwd.net/jQhWjKA8beVZ.128.mp3?AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1411462109&Signature=bx4feftwBftwcZgk9uJhiFnOs8I%3D&e=1411462109&h=5c40c03b2add89a72d435ef140bd0548)
09-23 10:42:41.790: V/FFmpegMediaPlayer-JNI(25685): setAudioStreamType: 3
09-23 10:42:41.791: V/FFmpegMediaPlayer(25685): MediaPlayer::setAudioStreamType
09-23 10:42:41.792: V/FFmpegMediaPlayer(25685): prepareAsync

And my code output :

09-23 10:57:14.412: D/Streaming(31764): streaming ok

from ffmpegmediaplayer.

SteveEdson avatar SteveEdson commented on July 17, 2024

Looks ok from here, have you tried using the latest version of the library, including the precompiled libs. I noticed it was updated yesterday and includes a few fixes, including better error handling for server error responses. It might help with your issue.

from ffmpegmediaplayer.

machard avatar machard commented on July 17, 2024

Indeed, you're right there is new output :

09-23 11:47:21.544: V/FFmpegMediaPlayer(6723): prepareAsync
09-23 11:47:21.544: V/FFmpegMediaPlayer(6723): MediaPlayer::setAudioStreamType
09-23 11:47:21.572: V/FFmpegMediaPlayer(6723): message received msg=100, ext1=1, ext2=0
09-23 11:47:21.572: E/FFmpegMediaPlayer(6723): error (1, 0)
09-23 11:47:21.572: V/FFmpegMediaPlayer(6723): callback application
09-23 11:47:21.572: V/FFmpegMediaPlayer-JNI(6723): notify: 100
09-23 11:47:21.586: V/FFmpegMediaPlayer(6723): back from callback
09-23 11:47:21.726: E/FFmpegMediaPlayer(6723): Error (1,0)

I've moved setOnErrorListener(errorListener); to playNewFile and preload and my error handler is called.

But i believe it should not as soundcloud's url are fine..

Thank you for your help

from ffmpegmediaplayer.

SteveEdson avatar SteveEdson commented on July 17, 2024

I'm not sure what this error code represents, maybe @wseemann could explain?

from ffmpegmediaplayer.

wseemann avatar wseemann commented on July 17, 2024

https support isn't enabled by default in the current prebuilt-libs. If you want to play a URL over https use these: https://github.com/wseemann/FFmpegMediaPlayer/blob/master/fmp-library/prebuilt-libs-with-https.tar.gz. I just tested using the URL you supplied and it worked.

from ffmpegmediaplayer.

machard avatar machard commented on July 17, 2024

problem solved, thank you !

from ffmpegmediaplayer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.