[PYTHON] Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP

Introduction

A memo on how to control music playback on a smartphone connected to Raspberry pi via Bluetooth with AVRCP

environment

Preparation

https://gist.github.com/oleq/24e09112b07464acbda1 http://blog.bnikka.com/raspberrypi/raspberrypibluetooth.html Refer to the above URL and turn the Raspberry Pi into a Bluetooth audio (A2DP) receiver so that you can play music from your smartphone. (* It seems that AVRCP cannot be used unless it is connected with A2DP, but maybe there is a way to use it only with AVRCP?)

Try to control with AVRCP

Demo with dbus-python

BT_MediaPlayer.py


#!/usr/bin/python

import dbus

SERVICE_NAME = "org.bluez"
ADAPTER_INTERFACE = SERVICE_NAME + ".MediaPlayer1"
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object(SERVICE_NAME, "/"),
                    "org.freedesktop.DBus.ObjectManager")
objects = manager.GetManagedObjects()

if __name__ == '__main__':
    for path, ifaces in objects.iteritems():
        adapter = ifaces.get(ADAPTER_INTERFACE)
        if adapter is None:
            continue
        print path
        player = bus.get_object('org.bluez',path)
        BT_Media_iface = dbus.Interface(player, dbus_interface=ADAPTER_INTERFACE)
        break

    while 1:
        s = raw_input()
        if s == 'quit': 
            break
        if s == 'play':
            BT_Media_iface.Play()
        if s == 'pause':
            BT_Media_iface.Pause()
        if s == 'stop':
            BT_Media_iface.Stop()
        if s == 'next':
            BT_Media_iface.Next()
        if s == 'pre':
            BT_Media_iface.Previous()
        if s == 'show':
            track =  adapter.get('Track')
            print 'Title: ' + track.get('Title') 
            print 'Artist: ' + track.get('Artist')
            print 'Album: ' + track.get('Album')
            print 'Genre: ' + track.get('Genre')
            print 'NumberOfTracks: ' + str(track.get('NumberOfTracks'))
            print 'TrackNumber: ' + str(track.get('TrackNumber'))
            print 'Duration: ' + str(track.get('Duration'))

When you execute the above code, it will be in the input waiting state, so enter the command The commands that can be used are as follows

--quit: Program end --show: Display song information --play: Play --pause: Pause --stop: stop --next: Next song --pre: Previous song

Hopefully the path of the connected smartphone will be displayed and you can control the music playback with various commands Below is an example of executing the show command BT_MediaPlayer_show.png

reference

http://qiita.com/eggman/items/339a9c9b338634ac27a5 https://www.raspberrypi.org/forums/viewtopic.php?t=111486&p=766936

Recommended Posts

Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Control the motor with a motor driver using python on Raspberry Pi 3!
A memo when connecting bluetooth from a smartphone / PC to Raspberry Pi 4
Use python on Raspberry Pi 3 to light the LED with switch control!
Connect to MySQL with Python on Raspberry Pi
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Record temperature and humidity with systemd on Raspberry Pi
Easy IoT to start with Raspberry Pi and MESH
Control brushless motors with GPIOs on Raspberry Pi Zero
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Install pyenv on Raspberry Pi and version control Python
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing
Easy introduction to home hack with Raspberry Pi and discord.py
Create a web surveillance camera with Raspberry Pi and OpenCV
Production of temperature control system with Raspberry Pi and ESP32 (1)
A story about trying to use cron on a Raspberry Pi and getting stuck in space
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
MQTT on Raspberry Pi and Mac
I made a web server with Raspberry Pi to watch anime
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
How to play music (wav / mp3) files on Raspberry Pi python
Using a webcam with Raspberry Pi
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Connect to VPN with your smartphone and turn off / on the server
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Pet monitoring with Rekognition and Raspberry pi
[Raspberry Pi] Add a thermometer and a hygrometer
Build a Tensorflow environment with Raspberry Pi [2020]
A memo with Python2.7 and Python3 on CentOS
Programming normally with Node-RED programming on Raspberry Pi 3
Working with sensors on Mathematica on Raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
How to install NumPy on Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
I have a question. Unable to make protocol buffers on raspberry pi 3 modelB.
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
Build a flask app made with tensorflow and dlib to work on centos7
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Build a distributed environment with Raspberry PI series (Part 3: Install and configure dnsmasq)
Build a Django environment on Raspberry Pi (MySQL)
Try using a QR code on a Raspberry Pi
Enjoy electronic work with GPIO on Raspberry Pi
MQTT RC car with Arduino and Raspberry Pi
Power on / off your PC with raspberry pi
Play with your Ubuntu desktop on your Raspberry Pi 4
Get temperature and humidity with DHT11 and Raspberry Pi