florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
739 stars 336 forks source link

Control player using media buttons from physical devices such as bluetooth headset, car etc #97

Closed manojvsp12 closed 4 years ago

manojvsp12 commented 4 years ago

*Describe the feature you'd like If this feature is already present please let me know.

I'd like to control AssetsAudioPlayer using media button presses from devices such as bluetooth headset, car etc.

Controlling such as play/pause/previous/next.

Thanks in advance.

florent37 commented 4 years ago

I don't know how to add this, can you find some sources (swift) about it

the player i'm using : AVPlayer on ios

florent37 commented 4 years ago

if it's using MPRemoteCommandCenter, it's already implemented

https://stackoverflow.com/questions/49024682/how-can-i-control-my-headset-for-my-music-player

looking at the Music.swift code :

func setupMediaPlayerNotificationView(currentSongDuration: Any) {
        UIApplication.shared.beginReceivingRemoteControlEvents()
        let commandCenter = MPRemoteCommandCenter.shared()
        //commandCenter.playCommand.isEnabled = self.playing

        self.setupNotificationView(currentSongDuration: currentSongDuration)

        self.deinitMediaPlayerNotifEvent()
        // Add handler for Play Command
        commandCenter.playCommand.isEnabled = true
        self.targets["play"] = commandCenter.playCommand.addTarget { [unowned self] event in
            self.play();
            return .success
        }

        // Add handler for Pause Command
        commandCenter.pauseCommand.isEnabled = true
        self.targets["pause"] = commandCenter.pauseCommand.addTarget { [unowned self] event in
            self.pause();
            return .success
        }

        // Add handler for Pause Command
        commandCenter.previousTrackCommand.isEnabled = true
        self.targets["prev"] = commandCenter.previousTrackCommand.addTarget { [unowned self] event in
            self.channel.invokeMethod(Music.METHOD_PREV, arguments: [])

            return .success
        }

        // Add handler for Pause Command
        commandCenter.nextTrackCommand.isEnabled = true
        self.targets["next"] = commandCenter.nextTrackCommand.addTarget { [unowned self] event in
            self.channel.invokeMethod(Music.METHOD_NEXT, arguments: [])

            return .success
        }
    }
florent37 commented 4 years ago

I cannot try this because it's not available on simulator & I don't have any device to try

manojvsp12 commented 4 years ago

Is this support provided in Android?

florent37 commented 4 years ago

I did not try it, I'll check tomorrow

florent37 commented 4 years ago

I implemented it today on android, I'll push it soon before this I had to try it on ios !

florent37 commented 4 years ago

the code above is already implemented on ios, I'll let user try & create an issue if this does not work

florent37 commented 4 years ago

I pushed it in 1.6.0, can you try ?

you have to set showNotification: true, when you open your media to make it work

manojvsp12 commented 4 years ago

Android: Tried in bluetooth headset and Car with version 1.6.0. Not working.

florent37 commented 4 years ago

What's you Android version ?

manojvsp12 commented 4 years ago

Android 10 Device: Oneplus 6

manojvsp12 commented 4 years ago

Also I am using livestream for playback

florent37 commented 4 years ago

Ok Its the same device as me, I'll try again , it worked with my JBL Bluetooth headset (play pause)

manojvsp12 commented 4 years ago

I tried with oneplus bullets wireless, boat bluetooth headset and connected to volkswagen car media player

florent37 commented 4 years ago

I tried again with another device, an external bluetooth speaker, & my oneplus 6

It's the sample main.dart, can you pull the project from the github & launch the sample, then click on "all as playlist" then try the bluetooth actions ?

florent37 commented 4 years ago

I made a small fix on bluetooth play/pause (for livestream) in 1.6.0+2 maybe this will fix you issue, btw try the main.dart

you did not tell me if you opened with notification

 _assetsAudioPlayer.open(Playlist(audios: audios), showNotification: true, autoStart: true);
manojvsp12 commented 4 years ago

Works on my boat bluetooth headset, play/pause works when I click play/pause in my oneplus bullets wireless but magnetic control not working and not able to control from car media

florent37 commented 4 years ago

it's wierd, maybe I have to add some code to make it works everywhere, but don't know what

manojvsp12 commented 4 years ago

Noticed something. When I am running my app in debug mode (flutter run), I am able to control app using media buttons (check my previous comment) but when I build as apk for release (flutter build apk --release), install apk manually and tried to control, I am unable to control.

florent37 commented 4 years ago

Oh looks wired, I'll check this !

florent37 commented 4 years ago

Thanks to have noticed this

florent37 commented 4 years ago

I've created https://github.com/florent37/Flutter-AssetsAudioPlayer/issues/129 for the debug/release issue

and linked to #117 which is surely the same bug, will try to find a fix soon, but it's hard to try

florent37 commented 4 years ago

I pushed some code around bluetooth can you try the 1.6.3+1 ?