Sailfish OS下的gstreamer没有输出声音

Posted

技术标签:

【中文标题】Sailfish OS下的gstreamer没有输出声音【英文标题】:Gstreamer under Sailfish OS no output sound 【发布时间】:2015-01-20 07:39:58 【问题描述】:

我正在为 Sailfish OS 开发音频播放器,并尝试通过 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

【问题讨论】:

【参考方案1】:

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

gst-launch 在资源策略配置中被静态设置为“播放器”,因此无需任何额外工作即可运行。

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

如果您正在开发纯 C 应用程序,请检查 https://github.com/nemomobile/libaudioresource 或为 Qt 应用程序检查 https://github.com/nemomobile/libaudioresource-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;

感谢 Juho Hämäläinen 的解答!

【讨论】:

以上是关于Sailfish OS下的gstreamer没有输出声音的主要内容,如果未能解决你的问题,请参考以下文章

发布前报告“问题:本机崩溃”

intellij构建多模块项目

Ubuntu 宣布放弃 Unity 桌面环境,明年回归 GNOME;中国也要拥有自己的 Sailfish 移动系统了|资讯

安装 Gstreamer 后没有这样的元素 filesrc

为什么os.listdir没有给出任何东西?

gstreamer:没有可用于“audio/x-wav”类型的解码器/找不到合适的插件