C++ 旗鱼操作系统下的Gstreamer无输出声音

C++ 旗鱼操作系统下的Gstreamer无输出声音,c++,linux,audio,gstreamer,sailfish-os,C++,Linux,Audio,Gstreamer,Sailfish Os,我正在为Sailfish操作系统开发一个音频播放器,并试图通过gstreamer播放文件,但问题是:并没有声音 我通过控制台检查了gstream: gst-launch-0.10 filesrc location=/path/to/file.ogg!德克宾! 自动音频接收器 它工作得很好 我测试了将音频文件转换为音频文件: int main (int argc, char *argv[]) { GstElement *pipeline; GstBus *b

我正在为Sailfish操作系统开发一个音频播放器,并试图通过gstreamer播放文件,但问题是:并没有声音

我通过控制台检查了gstream:

gst-launch-0.10 filesrc location=/path/to/file.ogg!德克宾! 自动音频接收器

它工作得很好

我测试了将音频文件转换为音频文件:

int
main (int   argc,
      char *argv[])
{
      GstElement *pipeline;
      GstBus *bus;
      GstMessage *msg;

      gst_init (&argc, &argv);

      pipeline = gst_parse_launch ("filesrc location=/home/nemo/Music/Ringtones/Myfile.mp3 ! decodebin ! audioconvert ! vorbisenc ! oggmux ! filesink location=test.ogg", NULL);

      gst_element_set_state (pipeline, GST_STATE_PLAYING);

      bus = gst_element_get_bus (pipeline);
      msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));

      if (msg != NULL)
        gst_message_unref (msg);
      gst_object_unref (bus);
      gst_element_set_state (pipeline, GST_STATE_NULL);
      gst_object_unref (pipeline);

      return 0;
}
它工作得很好

但是,当我尝试播放时,没有声音:

pipeline = gst_parse_launch ("filesrc location=/home/nemo/Music/Ringtones/Myfile.mp3 ! decodebin ! audioconvert ! autoaudiosink", NULL);

Gstreamer版本:0.10

问题是因为在使用之前需要获取资源:

gst launch在资源策略配置中被静态设置为“player”,因此可以运行它而无需任何额外工作

但是,当您创建自己的应用程序时,您需要自己获取音频播放资源

检查您是在开发C-only应用程序还是在开发Qt应用程序

例如:

#include <gst/gst.h>
#include <audioresource.h>
#include <glib.h>
#include <unistd.h>
#include <stdio.h>

/*
 * Dependencies glib2-devel, libaudioresource-devel, gstreamer-devel.
 * Compile with:
 * gcc `pkg-config --cflags --libs gstreamer-0.10` `pkg-config --cflags --libs audioresource` `pkg-config --cflags --libs glib-2.0` gst-example.c -o gst-example
 *
 * Check https://github.com/nemomobile/libaudioresource
 */

static GstElement *pipeline;
static int got_reply = 0;

static void on_acquired(audioresource_t *audio_resource, bool acquired, void *user_data)
{
    got_reply = 1;
    printf("on_acquired: %s\n", acquired ? "true" : "false");
    if (acquired) {
        // start playback here
        printf("start playback\n");
        gst_element_set_state (pipeline, GST_STATE_PLAYING);
    } else {
        // stop playback here
    }
}

static void naive_wait()
{
    got_reply = 0;
    while (!got_reply) {
        g_main_context_iteration(NULL, false);
        usleep(1000);
    }
}

int main(int   argc, char *argv[])
{
    audioresource_t *resource;
    void *user_data = NULL;
    char tmp[1024];
    GstBus *bus;
    GstMessage *msg;

    if (argc < 2) {
        printf("audio file argument needed.\n");
        return 1;
    }

    gst_init (&argc, &argv);

    printf("initialize audioresource for media player\n");
    resource = audioresource_init(AUDIO_RESOURCE_MEDIA, on_acquired, user_data);

    snprintf(tmp, 1024, "filesrc location=%s ! decodebin ! audiocovert ! autoaudiosink", argv[1]);
    printf("create pipeline: %s\n", tmp);
    pipeline = gst_parse_launch (tmp, NULL);

    printf("acquire audioresource..\n");
    // When you want to start playback
    audioresource_acquire(resource);

    // Wait for the reply for acquire..
    naive_wait();

    bus = gst_element_get_bus (pipeline);
    msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));

    if (msg != NULL)
        gst_message_unref (msg);
    gst_object_unref (bus);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);

    printf("release audioresource..\n");
    audioresource_release(resource);

    // Wait for release..
    naive_wait();

    // When you close your application
    audioresource_free(resource);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
/*
*依赖项glib2-devel、libaudioresource-devel、gstreamer-devel。
*编译时使用:
*gcc`pkg config--cflags--libs gstreamer-0.10``pkg config--cflags--libs audioresource``pkg config--cflags--libs glib-2.0`gst example.c--o gst example
*
*检查https://github.com/nemomobile/libaudioresource
*/
静态GstElement*管道;
static int get_reply=0;
获取的音频资源上的静态无效(音频资源*音频资源,获取的布尔值,无效*用户数据)
{
got_reply=1;
printf(“获取时:%s\n”,获取时为“真”:“假”);
如果(收购){
//从这里开始播放
printf(“开始播放\n”);
gst元素设置状态(管道、gst状态);
}否则{
//停止播放这里
}
}
静态void naive_wait()
{
得到的答复=0;
而(!得到_回复){
g_main_context_迭代(NULL,false);
usleep(1000);
}
}
int main(int argc,char*argv[])
{
音频资源;
void*user_data=NULL;
char-tmp[1024];
GstBus*总线;
GstMessage*msg;
如果(argc<2){
printf(“需要音频文件参数。\n”);
返回1;
}
gst_init(&argc,&argv);
printf(“为媒体播放器初始化音频资源\n”);
resource=audioresource\u init(音频资源\u媒体,已获取,用户\u数据);
snprintf(tmp,1024,“filesrc位置=%s!decodebin!audiocovert!autoaudiosink”,argv[1]);
printf(“创建管道:%s\n”,tmp);
管道=gst_解析_启动(tmp,空);
printf(“获取音频资源..\n”);
//当您要开始播放时
音频资源(资源);
//等待获取的答复。。
天真的等待;
总线=gst\u元件\u获取\u总线(管道);
msg=gst_总线_定时_pop_过滤(总线,gst_时钟_时间_无,(GstMessageType)(gst_消息_错误| gst_消息_EOS));
如果(msg!=NULL)
gst_消息_unref(msg);
商品及服务税(巴士);
gst\元素\集合\状态(管道,gst\状态\空);
gst_对象_unref(管道);
printf(“发布音频资源..\n”);
音频资源发布(资源);
//等待释放。。
天真的等待;
//当您关闭应用程序时
免费音频资源(资源);
返回0;
}
感谢Juho Hämäläinen的回答