Code Monkey home page Code Monkey logo

Comments (2)

RinDoesJava avatar RinDoesJava commented on August 21, 2024
func Play(session *discordgo.Session, guildID, voiceChannelID, mediaURL string) error {
	VoiceConnection, err := session.ChannelVoiceJoin(guildID, voiceChannelID, false, false)
	if err != nil {
		fmt.Println(err)
		return err
	}
	defer VoiceConnection.Disconnect()

	encodingSession, err := dca.EncodeFile(mediaURL, encodeOptionsPresetHigh)
	if err != nil {
		fmt.Println(err)
		return err
	}
	defer encodingSession.Cleanup()

	VoiceConnection.Speaking(true)

	//Create channel for done signal
	Channels[guildID] = make(chan error)

	streamingSession := dca.NewStream(encodingSession, VoiceConnection, Channels[guildID])

	//Wait for stream to finish
	err = <-Channels[guildID]
	if err != nil {
		return err
	}

	VoiceConnection.Speaking(false)

	_, err = streamingSession.Finished()
	if err != nil {
		fmt.Println(err)
	}

	encodingSession.Stop()
	encodingSession.Cleanup()

	if err != nil || err != ErrorStoppedManually {
		fmt.Println(err)
		return err
	}

	return nil
}

This is my play function by the way. "mediaURL" is the formatted url that's given by ytdl.

from dca.

RinDoesJava avatar RinDoesJava commented on August 21, 2024

Alright turns out this method just didn't want to work for me.
I'm going to close this issue now, but I shall show what can be done to remedy this!

Since I have a function where I get metadata about a youtube video through ytdl for ease of access your code may differ. If your code is built similarly to mine, with the exception of having to get a streamURL through format (Example in the ReadMe), you'll want to do these steps:

First: add "net/http" to your imports.
Second: define a function that gets the formatted url.

videoInfo, err := ytdl.GetVideoInfo(url)
	if err != nil {
		return nil, err
	}

	format := videoInfo.Formats.Extremes(ytdl.FormatAudioBitrateKey, true)[0]

	videoURL, err := videoInfo.GetDownloadURL(format)
	if err != nil {
		return nil, err
	}

resp, err := http.Get(videoURL.String())
	if err != nil {
		return nil, err
	}

Third: define a body from resp

body := resp.Body

Fourth: Replace the encodingSession with something like this:

encodingSession, err := dca.EncodeMem(body, dca.StdEncodeOptions)
	if err != nil {
		return err
	}

This worked for me, and I hope anyone who sees this may find this method to work aswell!

Huge credit to the people on the Discord Gophers discord server for sharing the knowledge.
Especially: diamondburned#4507

from dca.

Related Issues (18)

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.