Accessing the Android media stream for audio visualization

后端 未结 6 1327
谎友^
谎友^ 2020-11-30 20:06

Basically, I want to make an audio visualizer. I know it\'s possible, because my phone came with a few live wallpapers that do it. The problem is, I can\'t seem to figure

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 20:28

    A slightly faster snoop() would be to call Class.getMethod() once, and then to use a custom parseInt() instead of Integer.parseInt()...

    private static Method mSnoop;
    
    //..(http://nadeausoftware.com/node/97)..
    private static int customParseInt( final String s )
    {
        // Check for a sign.
        int num  = 0;
        int sign = -1;
        final int len  = s.length( );
        final char ch  = s.charAt( 0 );
        if ( ch == '-' )
            sign = 1;
        else
            num = '0' - ch;
    
        // Build the number.
        int i = 1;
        while ( i < len )
            num = num*10 + '0' - s.charAt( i++ );
    
        return sign * num;
    } 
    
    private static int snoop(short [] outData, int kind)
    {    
        if ( mSnoop != null )
        {
            try
            {
                return customParseInt( (mSnoop.invoke( MediaPlayer.class , outData, kind)).toString() );
            }
            catch ( Exception e )
            {
                Log.e( TAG, "Failed to MediaPlayer.snoop()!", e );
                return 1;
            }
        }
        else
        {
            try {
                Class c = MediaPlayer.class;
                Method m = c.getMethod("snoop", outData.getClass(), Integer.TYPE);
                m.setAccessible(true);
                mSnoop = m;
                return customParseInt( (m.invoke(c, outData, kind)).toString() ); 
            } 
            catch (Exception e) 
            {
                Log.e( TAG, "Failed to MediaPlayer.snoop()!", e );
                return 1;
            }
        }
    }
    

提交回复
热议问题