Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
burakk
Posts: 149
Registered: ‎04-23-2011
My Device: BlackBerry 9700 Bold
My Carrier: Turkcell
Accepted Solution

How to Set Media Player's Volume in QML

Hi,

 

I'd like to set the volume of my media player object in QML. According to the documentation:

http://developer.blackberry.com/native/reference/cascades/bb__multimedia__mediaplayer.html

 

MediaPlayer class has a setVolume method, but I am getting:

 

TypeError: Result of expression 'myPlayer.setVolume' [undefined] is not a function.

 

How can I set the volume in QML?

 

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: How to Set Media Player's Volume in QML

[ Edited ]

without any code we cant really see where you may be going wrong, 

 

make sure you have included the neccesary libraries in your .pro

LIBS += -lbbmultimedia

 

also this needs to be at the top of every qml page where you interact with the mediaPlayer

import bb.multimedia 1.0

 

are you trying to adjust the volume like this

 

Container {
    property double newVolume
    
    onNewVolumeChanged: {
       myPlayer.setVolume(newVolume)
    }
    Button {
       text: "Increase Volume"
       onClicked: {
           newVolume = newVolume + 1
       }
     }
     Button {
       text: "Decrease Volume"
       onClicked: {
           newVolume = newVolume - 1
       }
     }

    attachedObjects: [
        MediaPlayer {
             id: myPlayer
        }
    ]
 
}

 by looking over the documentation this should work if it doesn't you may want to file a ticket and/or try adjusting the volume in c++ (somethings only work properly in c++)

Please use plain text.
Developer
burakk
Posts: 149
Registered: ‎04-23-2011
My Device: BlackBerry 9700 Bold
My Carrier: Turkcell

Re: How to Set Media Player's Volume in QML

[ Edited ]

Well, thank you for your input, but

 

I have the library already included in the .pro file, and also has the import directive. At the end, the media player is playing music.

 

I also tried in C++, it does not work.

 

 

Here's the QML file I tested again:

 

import bb.cascades 1.0
import bb.multimedia 1.0

Page {
    
    property double newVolume
    
    onNewVolumeChanged: {
        myPlayer.setVolume(newVolume)  
    }
    
    onCreationCompleted: {
        myPlayer.play();
    }
    
    Container {
           
        Button {
            text: "Increase Volume"
            onClicked: {
                newVolume = newVolume + 1
            }
        }
        Button {
            text: "Decrease Volume"
            onClicked: {
                newVolume = newVolume - 1
            }
        } 
    }
    
    attachedObjects: [
        MediaPlayer {
            id: myPlayer
            sourceUrl: "http://some.streamingservice.com"
        }
    ]
}

 

Getting "TypeError: Result of expression 'myPlayer.setVolume' [undefined] is not a function." all the time.

 

 

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: How to Set Media Player's Volume in QML

[ Edited ]

Definatley shouldn't be doing this according to the docs, if you file a ticket for a bug it will help them track it down and get it fixed

 

https://www.blackberry.com/jira/secure/Dashboard.jspa

 

Also try to include any os versions you've noticed this happening on and let them know that you experience the issue in both c++ and QML

Please use plain text.
Developer
burakk
Posts: 149
Registered: ‎04-23-2011
My Device: BlackBerry 9700 Bold
My Carrier: Turkcell

Re: How to Set Media Player's Volume in QML

Hasn't anyone tried to set the volume programmatically?

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: How to Set Media Player's Volume in QML

Please use plain text.
Developer
burakk
Posts: 149
Registered: ‎04-23-2011
My Device: BlackBerry 9700 Bold
My Carrier: Turkcell

Re: How to Set Media Player's Volume in QML

[ Edited ]

Yes, I saw that.

 

In addition to 

audiomixer_set_output_level(AUDIOMIXER_OUTPUT_SPEAKER, val);
mediacontroller_volume_set_level(val);

also works. But since I need more than one MediaPlayer instances, I need a way to control their volumes seperately. Calling a simple setVolume() on each in the QML would be the simplest for me.

 

I have to find a way to call the above mentioned core API methods for both media players, but I don't know how to get "handles" to them.

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: How to Set Media Player's Volume in QML

[ Edited ]

i know if you set an object name for your QML media player you can then reference it in c++

 

MediaPlayer *mediaPlayer bb::cascades::Application::instance()->findChild<MediaPlayer*>("objectName");

 I'm not sure if this will let you interact with the C api but I think it might

 

If it doesn't your next best bet would be to initate the media players in c++ and then adjust the media source from c++ or use qml to update a c++ value that changes the media source

Please use plain text.
Developer
burakk
Posts: 149
Registered: ‎04-23-2011
My Device: BlackBerry 9700 Bold
My Carrier: Turkcell

Re: How to Set Media Player's Volume in QML

Links to any samples or references would be helpful for me. How to call C API methods on different MediaPlayer instances....

Please use plain text.
BlackBerry Development Advisor
sabdelsayed
Posts: 249
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10
My Carrier: Bell

Re: How to Set Media Player's Volume in QML

[ Edited ]

Hi burakk,

 

Here are the steps you need to follow to set the volume programatically in QML:

1) Define a signal myVolumeChanged to the main QML control

2) Define a slot that handles the above signal once it's fired by setting the volume of the MediaPlayer with the new volume

3) Fire this signal in the onClicked slots of the two volume buttons

 

I tried the above using the nowplaying Cascades sample app and have been able to achieve what you are trying to do, i.e. setting the volume of the MediaPlayer using two QML volume buttons. Here is the code I added to the main.qml file of sample app for your reference:

 

import bb.cascades 1.2
import bb.multimedia 1.0

Page {
    property double newVolume: 4
    signal myVolumeChanged()
    
    onMyVolumeChanged: {
        player.setVolume(newVolume);
    }
    
    Container {
        layout: DockLayout {}

        ImageView {
            ...
        }

        Label {
            ...
        }


        //! [1]
        ProgressBar {
               ....
        }
        //! [1]
        
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            
            Button {
                text: "Increase Volume"
                onClicked: {                	
                    newVolume = newVolume + 1
                    myVolumeChanged();                    
                }
            }
            Button {
                text: "Decrease Volume"
                onClicked: {                	
                    newVolume = newVolume - 1
                    myVolumeChanged();
                }
            } 
        }
    }

    attachedObjects: [
        //! [2]
        MediaPlayer {
            id: player

            sourceUrl: "music/song.mp3"
        },
        //! [2]

        //! [3]
        NowPlayingConnection {
            id: nowPlaying
            ...
            
        }
        //! [3]
    ]
}

 

 

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Please use plain text.