Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSurfaceTextureAvailable never called when Texture Views are created in Service

I am trying to create a texture view in Service, and I dont seem to get "onSurfaceTextureAvailable" call back. "A TextureView or a subclass can only be used with hardware acceleration enabled." comes up in the log when I have provided android:hardwareAccelerated="true" for the application, is there any permission to be explicity provided to the service

package com.example.textures;



import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FlyMaadi extends Service implements SurfaceTextureListener {


    private final String TAG = "FlyMaadi";
    private TextureView mTextureView;
    private WindowManager mWindowManager;
    private LayoutInflater minflater;
    private ViewGroup mrel_layout;
    private TextureView mTextureView1;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    public void createSurfaceTexture()
    {
        Log.d(TAG,"Create Surface Texture\n");
        mTextureView = new TextureView(this);

        Log.d(TAG,"Is hardware accelerated:: "+ mTextureView.isHardwareAccelerated());


        mTextureView.setSurfaceTextureListener(this);

        //mTextureView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

        minflater = (LayoutInflater)getSystemService
                (LAYOUT_INFLATER_SERVICE);



        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        FrameLayout mParentView = new FrameLayout(getApplicationContext());


        final WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params1.width = 352;
        params1.height = 288;

        //mWindowManager.addView(mrel_layout, params1);

        mWindowManager.addView(mParentView, params1);


        if(mTextureView.isAvailable())
        {
            Log.d(TAG,"Texture view is already available");
        }
        else
        {
            Log.d(TAG,"Texture view is not available");
        }


        Log.d(TAG,"Adding surface texture:: ");


        mParentView.addView(mTextureView);






    }


    @Override 
    public void onCreate() {
        super.onCreate();

        Log.d(TAG,"onCreate");
        createSurfaceTexture();

    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureAvailable");
    }


    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureDestroyed");
        return false;
    }


    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureSizeChanged");

    }


    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureUpdated");

    }

}

My Androidmanifest xml file is as below

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textures"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"        
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true" 
        >
        <activity
            android:name="com.example.textures.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service            
            android:name="com.example.textures.FlyMaadi"
            />


    </application>

</manifest>
like image 642
user2315052 Avatar asked Jan 21 '14 10:01

user2315052


2 Answers

For "A TextureView or a subclass can only be used with hardware acceleration enabled.", you might want to do replace this line:

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

with

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED

HTH.

like image 140
VJ Vélan Solutions Avatar answered Nov 06 '22 23:11

VJ Vélan Solutions


onSurfaceTextureAvailable will be called once the view is visible on the screen. Till then it will not be called. I see that you are not adding the textureview (and its parent) to the activity window or any of its views. Try passing an active view id or a view to the service and add the textureview into that. Once the view is displayed the called should be called . (BTW handling a view from a service is not recommended)

like image 32
Shrish Avatar answered Nov 06 '22 23:11

Shrish