Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support play/pause on media already playing #93

Open
daaru00 opened this issue Jan 18, 2020 · 7 comments
Open

Support play/pause on media already playing #93

daaru00 opened this issue Jan 18, 2020 · 7 comments

Comments

@daaru00
Copy link

daaru00 commented Jan 18, 2020

Hello,

is it possible to support play and pause command on current playing media without launching a new video?

Something like this:

client.connect(host, function() {
    // some method to load current player
    client.loadCurrentPlayer(function(err, player) {
       player.pause()
       // ...
       player.play()
    })
})

or directly operating on client object (like stop/volume/mute/unmute action):

client.connect(host, function() {
    client.pause()
    // ...
    client.play()
})
@EricHier
Copy link

Any updates on this? Would be awesome to use! :)

@pedromsilvapt
Copy link

Hi. AFAIK it is possible, I've used a version of that in one of my projects. The code should be something like this, if I remember correctly.

import { Client, DefaultMediaReceiver } from 'castv2-client';

const IP_ADDR = ...;

const client = new Client();

client.on( 'status', status => {
    if ( status && status.applications instanceof Array ) {
        const app = status.applications.find( app => app.appId === DefaultMediaReceiver.APP_ID );

        if ( app != null ) {
            client.join( app, DefaultMediaReceiver, ( err, player ) => {

            } );
        } else {
            client.launch( DefaultMediaReceiver, ( err, player ) => {

            } );
        }
    }
} );

client.connect( IP_ADDR );

I wrote this code off the top of my head, so there might be dragons (more like bugs 😄) in there. Let me know if it works for you.

@EricHier
Copy link

Thank you @pedromsilvapt - I've tried that code. :) When I run that code, the Spotify casting stops and the Default Media Reciever takes over. The code exits with a TypeError: Cannot read property 'mediaSessionId' of null. Do you have an idea of what could be a solution?

@pedromsilvapt
Copy link

Hi @EricHier. Yeah, sorry, I assumed you were using the default application, which is why in the find method, I was looking for a session like this app.appId === DefaultMediaReceiver.APP_ID. You will have to look for the ID of the Chromecast Spotify app (you can do it by making a console.log( status ) while the spotify app is open.

One caveat is, I'm not sure the DefaultMediaReceiver can control the spotify app, never tried it. Maybe the commands are the same (play, pause, etc...).

@EricHier
Copy link

Ah okay, I see. The sessions I get with getSessions looks like this:

[{ appId: 'CC32E753',
    displayName: 'Spotify',
    iconUrl:
     'https://lh3.googleusercontent.com/HOX9yqNu6y87Chb1lHYqhKVTQW43oFAFFe2ojx94yCLh0yMzgygTrM0RweAexApRWqq6UahgrWYimVgK',
    isIdleScreen: false,
    launchedFromCloud: false,
    namespaces: [ [Object], [Object], [Object], [Object], [Object] ],
    sessionId: 'e91fc450-998e-4d1c-b3b6-9cc95c578654',
    statusText: 'Casting: I Want To Break Free - Single Remix',
    transportId: 'e91fc450-998e-4d1c-b3b6-9cc95c578654' }]

Afterwards I tried to run

client.join(sessions[0], DefaultMediaReceiver, (err, player) => {
    player.pause();
});

and get the TypeError: Cannot read property 'mediaSessionId' of null.

Does this mean that the protocol is not the same? Or do I miss something?

@pedromsilvapt
Copy link

Yes I suppose. That particular error seems in my view to be triggered by the status reponse message, which does not contain the mediaSessionId that the DefaultMediaReceiver needs to call the pause method. I've never worked with the Spotify App, so I can't be of much help with that. Sorry 😢

@EricHier
Copy link

EricHier commented Jun 2, 2021

Okay. :) I highly appreciate your help - so awesome! ^^ I am not that experienced with this module and you helped me to get a realistic evaluation of the issue. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants