Starting Vitamio In A Service From Cordova Plugin

北战南征 提交于 2020-01-07 01:25:07

问题


After about a week...I thinks it's time to ask the SO community :)

Now I already have a working(work in progress) plugin that already does this.

java file that extends CordovaPlugin:

Intent objIntent = new Intent(cordovaObj.getActivity().getApplicationContext(), MY_SERVICE.class);

//pass the url to the service
objIntent.putExtra("mediaUrl", url);

//Start the service
cordovaObj.getActivity().getApplicationContext().startService(objIntent);

Then in the onCreate method of the service I instatiate androids native MediaPlayer class and onStartCommand() of the service I start the player. I also have a stop method as well. Once I got it working I wanted to try and exchange the andriod mediaplayer class with that of Vitamio.

I did the following https://github.com/yixia/VitamioBundle/wiki/Getting-Started

They had four easy instructions the 4th one being.. "Now you can use Vitamio Media API same as Android Media API"...??..um..no

Have all files in the right places and the manifes has proper declarations of activities and services but after compiling my app crashes on lauch.

Step 3 is my issue I can't call

if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))

In an onCreate method in my service class I get and error. I tried placing this in my MainActivity's onCreate but got crash on start up

    LAUNCH SUCCESS
E/AndroidRuntime(23282): java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.urassociation.app/biz.urassociation.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaInterfaceImpl.setActivityResultRequestCode(int)' on a null object reference
E/AndroidRuntime(23282): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaInterfaceImpl.setActivityResultRequestCode(int)' on a null object reference
E/AndroidRuntime(23282):    at org.apache.cordova.CordovaActivity.startActivityForResult(CordovaActivity.java:331)

The main issue is if use this in an activity won't I be forced to use an interface?

I just want to stream audio in the background while the user can navigate my app and use BroadcastReceiver intents to stop, pause, seekTo...etc from the plugin to the service or return position or player states.

Can anyone help??

Basically I want to use vitamio without a widow, widget, player interface etc I will controll this from cordova plugin. How can I use vitamio in such a way...any help would be great.

The Vitamio-Cor dova-Plugin works very well and plays HLS streams that don't normally work properly using native MediaPlayer...but I just want to play audio ...I commented out all the interface code but I still get an invisible window that prevents me from using my app until I tap the back button. I don't want to just use ndroid:theme="@android:style/Theme.NoDisplay" and modify the plugin the player should run as a service...that would be the proper way for an audio player anyway. Thanks again for the help I've really tried the best I can with what I know but at last I ask for your help.


回答1:


Wow. Can't believe I missed this in the logs.

I did not declare the necessary string resources that are used by vitamio's initialization methods :/ ..located in res/values/strings.xml ...everything working normally now.

So in short if you are using Cordova do this..

  1. Add the vitamio init to your MainActivity should look like this

    import io.vov.vitamio.LibsChecker; //don't forget to import this!

      public class MainActivity extends CordovaActivity
      {
          @Override
          public void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              if (!LibsChecker.checkVitamioLibs(this))
              return;
                  // Set by <content src="index.html" /> in config.xml
                  loadUrl(launchUrl);
           }
      }
    
  2. Ensure that you have all string references in your plugin.xml like so

        <config-file target="res/values/strings.xml" parent="/resources">
        <string name="vitamio_library_app_name">VitamioLibrary</string>
        <string name="vitamio_init_decoders">Initializing decoders…</string>
        <string name="permission_group_tools_label">Vitamio tools</string>
        <string name="permission_group_tools_description">Access Vitamio package and resources.</string>
        <string name="permission_receive_messages_label">Receive Vitamio messages</string>
        <string name="permission_receive_messages_description">Receive all broadcasts from Vitamio service.</string>
        <string name="permission_write_providers_label">Write Vitamio providers</string>
        <string name="permission_write_providers_description">Delete, update or create new items in Vitamio providers.</string>
        <string name="VideoView_error_title">Cannot play video</string>
        <string name="VideoView_error_text_invalid_progressive_playback">Sorry, this video is not valid for streaming to this device.</string>
        <string name="VideoView_error_text_unknown">Sorry, this video cannot be played.</string>
        <string name="VideoView_error_button">OK</string>
        <string name="mediacontroller_play_pause">Play/Pause</string>
    </config-file>
    
  3. In your android manifest...

    <activity android:name="io.vov.vitamio.activity.InitActivity" android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden" android:launchMode="singleTop" android:theme="@android:style/Theme.NoTitleBar" android:windowSoftInputMode="stateAlwaysHidden"/>
    
    <activity android:configChanges="orientation|keyboardHidden|navigation" android:launchMode="singleTop" android:name="io.vov.vitamio.activity.InitActivity" android:theme="@android:style/Theme.NoDisplay" android:windowSoftInputMode="stateAlwaysHidden" />
    

You should now be able to use the vitamio Media class like normal. At some point I'm going to write a tutorial on this to save people from going crazy :}

btw obviously make sure you have the vitamio lib installed as well you could look at https://github.com/nchutchind/Vitamio-Cordova-Plugin to see how he did it...thats what I did. Great plugin btw.



来源:https://stackoverflow.com/questions/35122171/starting-vitamio-in-a-service-from-cordova-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!