Prev Previous Post   Next Post Next
Old 11-06-2009, 09:02 AM
meetooman's Avatar
meetooman meetooman is offline
Join Date: Oct 2009
Posts: 488
Page Discover libmtp programming

Libmtp is the software library used to handle MTP devices on Linux. When you experience connection problem using your MTP player on Linux, especially with media managers like Amarok, Banshee and the like, you often begin trouble-shooting with the command-line tools coming with libmtp. Their name are mtp-detect, mtp-tracks, mtp-files, mtp-getplaylist, etc. If they are not installed with the libmtp library on your system, you will get them by installing the libmtp-tools or mtp-tools package of your favorite distribution.

What is interesting is that the source code of these simple programs is available and is quite easy to understand even for a hobby programmer. It makes you want to write your own MTP manager program.

Starting to explore the libmtp API is quite simple. Just install the libmtp-devel package of your distribution and open the HTML documentation at /usr/share/doc/packages/libmtp8/html/index.html (your path can vary). In the "Examples" tab you will get the source code of the small command-line programs suite.

Your own C test program will include the libmtp.h headers file and the MTP device variable declaration and few libtmp API calls. To compile your mtp-cli-test.c program you can type
gcc mtp-cli-test.c -lmtp -o mtp-cli-test
You will quickly be able to execute this simple 4 steps sequence on a single connected device:
1 - Initialize the library
2 - Connect to your MTP device
3 - Perform some queries
4 - Release the connection
#include <libmtp.h>
LIBMTP_mtpdevice_t  *mtpdev;


mtpdev = LIBMTP_Get_First_Device();

char *manufacturername = LIBMTP_Get_Manufacturername(mtpdev);
char *modelname = LIBMTP_Get_Modelname(mtpdev);
char *deviceversion = LIBMTP_Get_Deviceversion(mtpdev);

/* Print or save the results as you like. */

The documentation is very good, with great example loops to explore the device content in depth, but will also teach you how to transfer media.

Have fun hacking with your MTP player!
Reply With Quote

Advertisement [Remove Advertisement]


libmtp, linux, mtp, programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 11:12 PM.