Android的可视化实现崩溃

Android的可视化实现崩溃

本文介绍了Android的可视化实现崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现Android的可视化类。我已经看过了其他的麻烦线程,但我仍然得到一个致命的错误。这一切看起来对我来说,它应该正常工作,无论出于何种原因,AVD不断崩溃。

首先,我使用的权限:

 <使用-权限的Andr​​oid:名称=android.permission.RECORD_AUDIO/>
<使用-权限的Andr​​oid:名称=android.permission.MODIFY_AUDIO_SETTINGS/>
 

下面是我的logcat:

  12-21 15:10:46.983:D / dalvikvm(642):不晚启用CheckJNI(已上)
12-21 15:10:48.823:D / MediaPlayer的(642):可能在客户端无法打开文件,试图服务器端
12-21 15:10:53.603:E / AudioEffect(642):设定():AudioFlinger无法创建效果,状态:-22
12-21 15:10:53.603:E /展示台,JNI(642):展示台initCheck失败-4
12-21 15:10:53.613:E /展示台-JAVA(642):错误code -4初始化展示台时。
12-21 15:10:53.613:E /视觉EX(642):无法初始化可视化引擎,错误:-4
12-21 15:10:53.964:D / gralloc_goldfish(642):没有GPU的仿真模拟器检测。
 

和我的code:

 包MediaPlayer.namespace;

进口java.io.IOException异常;

进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.media.MediaRecorder;
进口android.media.audiofx.Visualizer;
进口android.net.Uri;
进口android.os.Bundle;
进口android.util.Log;

公共类MediaPlayerActivity扩展活动
{
展台的视觉;
INT formattedVizData [];
字节rawWaveFor​​m [];
INT CONT = 0xFF的;

公共无效的onCreate(包savedInstanceState)
{
    //开始
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);


    ///启动媒体播放器和放大器;可视化工具。
    的MediaPlayer媒体播放器=新的MediaPlayer();
    媒体播放器= MediaPlayer.create(这一点,Uri.parse(http://vprbbc.streamguys.net:80/vprbbc24.mp3));
    mediaPlayer.start();
    //创建可视化对象,并将其连接到我们的媒体播放器。
    尝试{
        视觉=新的可视化工具(mediaPlayer.getAudioSessionId()); //此行引起异常
        visual.setEnabled(真正的);
        visual.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
        }
        赶上(例外前)
        {
            Log.e(视觉防爆,ex.getMessage());
        }
    }
}
 

解决方案

我有同样的问题,所以要尽量添加  在AndroidManifest.xml文件权限

如果您wan`t看到奥迪波此外,您HOULD显示它在画布或东西...

工作的例子:

  // android.permission.MODIFY_AUDIO_SETTINGS的音频设置,也
//android.permission.INTERNET互联网流媒体

包com.janilemy;

进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Rect;
进口android.media.AudioManager;
进口android.media.MediaPlayer;
进口android.media.audiofx.Visualizer;
进口android.os.Bundle;
进口android.view.Gravity;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.LinearLayout;
进口android.widget.SeekBar;
进口android.widget.TextView;

进口java.io.IOException异常;

公共类AudioFxDemoActivity延伸活动{

    //这里是你的网址定义
字符串URL =htt​​p://vprbbc.streamguys.net/vprbbc24.mp3;

    //常量vizualizator  - 高度50dip
私有静态最终浮动VISUALIZER_HEIGHT_DIP = 50F;

    //你的MediaPlayer
MediaPlayer的熔点;

// Vizualization
私人展示台mVisualizer;

    私人的LinearLayout mLinearLayout;
    私人VisualizerView mVisualizerView;
    私人TextView的mStatusTextView;


/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    //信息的TextView
    mStatusTextView =新的TextView(本);

    //创建新的LinearLayout(因为main.xml中为空)
    mLinearLayout =新的LinearLayout(本);
    mLinearLayout.setOrientation(LinearLayout.VERTICAL);
    mLinearLayout.addView(mStatusTextView);

    //设置的内容,以便我们创造新的布局
    的setContentView(mLinearLayout);

    //启动媒体播放器 - 像正常
    熔点为新的MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    尝试 {
        mp.setDataSource(URL);我们的网址定义//设置数据源
    }赶上(抛出:IllegalArgumentException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IllegalStateException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

    尝试{//告诉玩家去prepare状态
        MP prepare()。
    }赶上(IllegalStateException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
            //启动流/播放器
    mp.start();

    //设置你的Vizualizer  - 调用方法
    setupVisualizerFxAndUI();

            //使vizualizer
            mVisualizer.setEnabled(真正的);

            //信息文本
    mStatusTextView.setText(播放声音......);
}

    //我们的方法,设置Vizualizer
私人无效setupVisualizerFxAndUI(){
    //创建一个VisualizerView(定义如下),这将呈现简化的音频
    //波形到画布。

    //你需要有一些东西在哪里显示音频波形 - 在这种情况下,画布
    mVisualizerView =新VisualizerView(本);
    mVisualizerView.setLayoutParams(新ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            (中间体)(VISUALIZER_HEIGHT_DIP * getResources()getDisplayMetrics()密度。)));
    mLinearLayout.addView(mVisualizerView);

    //创建可视化对象,并将其连接到我们的媒体播放器。
    //你需要android.permission.RECORD_AUDIO为在AndroidManifest.xml中
    mVisualizer =新的可视化工具(mp.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(新Visualizer.OnDataCaptureListener(){
        公共无效onWaveFor​​mDataCapture(可视化可视化工具,byte []的字节数,
                INT采样速率){
            mVisualizerView.updateVisualizer(字节);
        }

        公共无效onFftDataCapture(可视化可视化工具,byte []的字节,INT采样速率){}
    },Visualizer.getMaxCaptureRate()/ 2,真,假);
}

@覆盖
保护无效的onPause(){
    super.onPause();

    如果(isFinishing()&安培;&安培;熔点=空!){
        mVisualizer.release();
        mEqualizer.release();
        mp.release();
        熔点=无效;
    }
}

/ **
 *绘制一条从接收到的波形数据的简单类
 * {@link Visualizer.OnDataCaptureListener#onWaveFor​​mDataCapture}
 * /
类VisualizerView扩展视图{
    私人字节[]字节;
    私人浮法[] M点;
    私人矩形mRect =新的矩形();

    民营涂料mForePaint =新的油漆();

    公共VisualizerView(上下文的背景下){
        超(上下文);
        在里面();
    }

    私人无效的init(){
        兆字节= NULL;

        mForePaint.setStrokeWidth(1F);
        mForePaint.setAntiAlias​​(真正的);
        mForePaint.setColor(Color.rgb(0,128,255));
    }

    公共无效updateVisualizer(byte []的字节){
        兆字节=字节;
        无效();
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);

        如果(兆字节== NULL){
            返回;
        }

        如果(M点== NULL || mPoints.length< mBytes.length * 4){
            M点=新的浮动[mBytes.length * 4]。
        }

        mRect.set(0,0,的getWidth(),的getHeight());

        的for(int i = 0; I< mBytes.length  -  1;我++){
            M点[I * 4 = mRect.width()* I /(mBytes.length  -  1);
            M点[I * 4 + 1] = mRect.height()/ 2
                    +((字节)(兆字节[I] + 128))*(mRect.height()/ 2)/ 128;
            M点[I * 4 + 2] = mRect.width()*第(i + 1)/(mBytes.length  -  1);
            M点[I * 4 + 3] = mRect.height()/ 2
                    +((字节)(兆字节[1 + 1] + 128))*(mRect.height()/ 2)/ 128;
        }

        canvas.drawLines(M点,mForePaint);
    }
}
 

}

这是Android的媒体播放器的音频波形可视化的一个完整的工作的例子。我希望这将有助于你和你的解决方案...

贾尼→

I am trying to implement the Android Visualizer class. I have looked up other trouble threads but I am still getting a fatal error. This all looks to me like it should work correctly, for whatever reason the AVD keeps on crashing.

First off, I am using the permissions:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

Here is my logcat:

12-21 15:10:46.983: D/dalvikvm(642): Not late-enabling CheckJNI (already on)
12-21 15:10:48.823: D/MediaPlayer(642): Couldn't open file on client side, trying server     side
12-21 15:10:53.603: E/AudioEffect(642): set(): AudioFlinger could not create effect,    status: -22
12-21 15:10:53.603: E/visualizers-JNI(642): Visualizer initCheck failed -4
12-21 15:10:53.613: E/Visualizer-JAVA(642): Error code -4 when initializing Visualizer.
12-21 15:10:53.613: E/Visual Ex(642): Cannot initialize Visualizer engine, error: -4
12-21 15:10:53.964: D/gralloc_goldfish(642): Emulator without GPU emulation detected.

And my code:

package MediaPlayer.namespace;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.audiofx.Visualizer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class MediaPlayerActivity extends Activity
{
Visualizer      visual;
int             formattedVizData[];
byte            rawWaveForm[];
int             cont = 0xFF;

public void onCreate(Bundle savedInstanceState)
{
    //start
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ///start media player & visualizer.
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
    mediaPlayer.start();
    // Create the Visualizer object and attach it to our media player.
    try{
        visual = new Visualizer(mediaPlayer.getAudioSessionId()); // this line causing Exception
        visual.setEnabled(true);
        visual.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
        }
        catch(Exception ex)
        {
            Log.e("Visual Ex", ex.getMessage());
        }
    }
}
解决方案

I have same problem, so try to add permission in AndroidManifest.xml file

Also if you wan`t to see that audi wave, you hould show it in canvas or something ...

Working example :

//android.permission.MODIFY_AUDIO_SETTINGS for audio settings and also
//android.permission.INTERNET for internet streaming

package com.janilemy;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class AudioFxDemoActivity extends Activity {

    //Here is your URL defined
String url = "http://vprbbc.streamguys.net/vprbbc24.mp3";

    //Constants for vizualizator - HEIGHT 50dip
private static final float VISUALIZER_HEIGHT_DIP = 50f;

    //Your MediaPlayer
MediaPlayer mp;

//Vizualization
private Visualizer mVisualizer;

    private LinearLayout mLinearLayout;
    private VisualizerView mVisualizerView;
    private TextView mStatusTextView;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Info textView
    mStatusTextView = new TextView(this);

    //Create new LinearLayout ( because main.xml is empty )
    mLinearLayout = new LinearLayout(this);
    mLinearLayout.setOrientation(LinearLayout.VERTICAL);
    mLinearLayout.addView(mStatusTextView);

    //set content view to new Layout that we create
    setContentView(mLinearLayout);

    //start media player - like normal
    mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try {
        mp.setDataSource(url); // set data source our URL defined
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {   //tell your player to go to prepare state
        mp.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            //Start your stream / player
    mp.start();

    //setup your Vizualizer - call method
    setupVisualizerFxAndUI();

            //enable vizualizer
            mVisualizer.setEnabled(true);

            //Info text
    mStatusTextView.setText("Playing audio...");
}

    //Our method that sets Vizualizer
private void setupVisualizerFxAndUI() {
    // Create a VisualizerView (defined below), which will render the simplified audio
    // wave form to a Canvas.

    //You need to have something where to show Audio WAVE - in this case Canvas
    mVisualizerView = new VisualizerView(this);
    mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            (int)(VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density)));
    mLinearLayout.addView(mVisualizerView);

    // Create the Visualizer object and attach it to our media player.
    //YOU NEED android.permission.RECORD_AUDIO for that in AndroidManifest.xml
    mVisualizer = new Visualizer(mp.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                int samplingRate) {
            mVisualizerView.updateVisualizer(bytes);
        }

        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
    }, Visualizer.getMaxCaptureRate() / 2, true, false);
}

@Override
protected void onPause() {
    super.onPause();

    if (isFinishing() && mp != null) {
        mVisualizer.release();
        mEqualizer.release();
        mp.release();
        mp = null;
    }
}

/**
 * A simple class that draws waveform data received from a
 * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
 */
class VisualizerView extends View {
    private byte[] mBytes;
    private float[] mPoints;
    private Rect mRect = new Rect();

    private Paint mForePaint = new Paint();

    public VisualizerView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mBytes = null;

        mForePaint.setStrokeWidth(1f);
        mForePaint.setAntiAlias(true);
        mForePaint.setColor(Color.rgb(0, 128, 255));
    }

    public void updateVisualizer(byte[] bytes) {
        mBytes = bytes;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBytes == null) {
            return;
        }

        if (mPoints == null || mPoints.length < mBytes.length * 4) {
            mPoints = new float[mBytes.length * 4];
        }

        mRect.set(0, 0, getWidth(), getHeight());

        for (int i = 0; i < mBytes.length - 1; i++) {
            mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
            mPoints[i * 4 + 1] = mRect.height() / 2
                    + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
            mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
            mPoints[i * 4 + 3] = mRect.height() / 2
                    + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
        }

        canvas.drawLines(mPoints, mForePaint);
    }
}

}

This is a full working example of android media player audio wave visualization... I hope it will help you with your solution ...

Jani L

这篇关于Android的可视化实现崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 16:15