LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2018, 11:17 AM   #1
vampireixon
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Rep: Reputation: Disabled
Unhappy ca_context_cancel() doesn't stop sound


Cound some body help?
I have that problem.
I start a sound calling:
ca_context_play (mContext, 2, CA_PROP_MEDIA_FILENAME, "MySound.wav", NULL);

If I call:
ca_context_playing(mContext,2,&_isPlaying);
_isPlaying is 1.

Then I call:
ca_context_cancel(mContext,2);
and the sound doesn't stops. But if I call again:
ca_context_playing(mContext,2,&_isPlaying);
_isPlaying is 0.

Thanks.
 
Old 04-12-2018, 01:09 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 779

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Please surround your code with code tags: [code]place code here[/code]

Does the following work?
Code:
#include <glib.h>
#include <canberra-gtk.h>
#include <stdio.h>

int main () {
    ca_context * contx;
    ca_context_create (&contx);
    ca_context_play(contx, 1, CA_PROP_MEDIA_FILENAME, 
            "/usr/share/sounds/alsa/Front_Center.wav", NULL);
    int isPlaying;
    ca_context_playing(contx, 1, &isPlaying);
    printf("%d\n", isPlaying);
    g_usleep (750000);
    ca_context_cancel(contx, 1);
    g_usleep(1000000); // delay to make sure cancel works v. program termination
    return 0;
}
Assuming you have that wav file, a pleasing female voice should say "front" in lieu of "front center".

If it does work we probably need to see more code in order to help.
 
Old 04-13-2018, 04:18 AM   #3
vampireixon
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi norobro. Thank you very much for your fast response.
I've tested your code and it don't works.
I've added this line:
Code:
 
    ca_context_create (&contx);
<-- your code
Code:
   int ret;
   if ((ret = ca_context_set_driver(contx, "alsa"))){
	printf((char*)"error 0x%2.2x ",ret);
   }
<-- line added

with this line the sound was played, but it didn't stop until the program is finnished. So
Code:
ca_context_cancel(contx, 1);
does noting.

This is my whole code:
Code:
#include <glib.h>
#include <canberra-gtk.h>
#include <stdio.h>

int main () {
	int ret;
    int isPlaying = 0;
    ca_context * contx;
    ca_context_create (&contx);
	if ((ret = ca_context_set_driver(contx, "alsa"))){
		printf((char*)"Error 0x%2.2X",ret);
	}
    ret = ca_context_play(contx, 1, CA_PROP_MEDIA_FILENAME,
            "/home/SND/TEST.WAV", NULL);
    if (ret) isPlaying = 1;
    ca_context_playing(contx, 1, &isPlaying);
    printf("%d\n", isPlaying);
    usleep (750000);
    ca_context_cancel(contx, 1);
    usleep(1000000); // delay to make sure cancel works v. program termination
    return 0;    // with a breakpoint in this line sound never stops
}
 
Old 04-13-2018, 07:04 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,129

Rep: Reputation: 690Reputation: 690Reputation: 690Reputation: 690Reputation: 690Reputation: 690
Maybe check return value for ca_context_cancel()
Code:
...
ret = ca_context_cancel(contx, 1);
fprintf(stderr, "cancel: %s\n", ca_strerror(ret));
...
Edit, also you don't destroy the context object at the end of the program...
Code:
...
usleep(1000000); // or sleep(1); 
ret = ca_context_destroy(contx);
fprintf(stderr, "destroy: %s\n", ca_strerror(ret));

Last edited by keefaz; 04-13-2018 at 07:12 AM.
 
1 members found this post helpful.
Old 04-13-2018, 09:39 AM   #5
vampireixon
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks again for your help.
new code.
Code:
int main () {
	int ret;
    int isPlaying = 0;
    ca_context * contx;
    ca_context_create (&contx);
	if ((ret = ca_context_set_driver(contx, "alsa"))){
		printf((char*)"Error 0x%2.2X",ret);
	}
    ret = ca_context_play(contx, 1, CA_PROP_MEDIA_FILENAME,
            "/usr/share/sounds/alsa/Front_Center.wav", NULL);
    if (ret) isPlaying = 1;
    ret = ca_context_playing(contx, 1, &isPlaying);
    printf("IsPlaying:%d   ret:%d\n", isPlaying,ret);
    usleep (750000);
    ca_context_cancel(contx, 1);
    ret = ca_context_playing(contx, 1, &isPlaying);
    printf("IsPlaying:%d   ret:%d\n", isPlaying,ret);
    usleep(1000000); // delay to make sure cancel works v. program termination
    return 0;
}
output console:
IsPlaying:1 ret:0
IsPlaying:0 ret:0

But Sound doesn't stop

If you wanna test like me should :
-add these libpaths:
/usr/lib
/usr/lib/x86_64-linux-gnu
-add these libraries:
canberra
-and add these includes:
/usr/include
/usr/include/glib-2.0
/usr/lib/x86_64-linux-gnu/glib-2.0/include
/usr/include/gtk-3.0
/usr/include/pango-1.0
/usr/include/cairo
/usr/include/gdk-pixbuf-2.0
/usr/include/atk-1.0
 
Old 04-13-2018, 11:18 AM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 779

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
What distribution are you running?

Have you tried setting the driver to "pulse"? On Ubuntu 18.04 ca_context_cancel does not work with the "alsa" driver but does work with "pulse".
 
1 members found this post helpful.
Old 04-13-2018, 11:23 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,129

Rep: Reputation: 690Reputation: 690Reputation: 690Reputation: 690Reputation: 690Reputation: 690
@vampireixon, Could you test with the code I posted?
 
Old 04-13-2018, 12:32 PM   #8
vampireixon
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi norobro.
I've checked it and you are rigth. With pulse it works fine. Thx
Keefaz.
If you look at my last code you'll see your 1st piece of code and the console output.
I can't use the 2nd one piece because if I have any other sound playing it will be stopped too. I only want to stop one sound.
Thx any way for your help.

All of you are so kind helping people.

Last edited by vampireixon; 04-13-2018 at 12:41 PM. Reason: I've done more tests and them worked fine
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Periodic ticking sound in GNOME that doesn't stop unless I mute. gregorian Linux - Newbie 2 12-28-2009 12:30 AM
Why doesn't chmod +t stop others from deleting my files in Ubuntu HGeneAnthony Linux - General 4 10-27-2006 04:04 PM
'amavisd stop' Command doesn't work vanden Linux - Software 1 01-03-2006 02:12 PM
DVD Drive doesn't stop spinning Silver925 Linux - General 9 06-09-2005 11:10 AM
fan doesn't stop murshed Linux - Hardware 0 12-25-2003 09:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration